Skip to content

Commit 808d400

Browse files
committed
chore: notes and update tests
1 parent 3ba65fc commit 808d400

6 files changed

Lines changed: 142 additions & 23 deletions

File tree

docs/api/v1/openapi.yaml

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@ components:
2222
example: thing1
2323
description:
2424
type: string
25-
example: This this a thing
25+
example: This this a thing
2626

2727
paths:
2828
/items:
2929
get:
30-
tags:
30+
tags:
3131
- Items Object
3232
summary: Returns a list of items.
3333
description: Returns a list of all Items currently in the database.
3434
responses:
35-
'200': # status code
35+
"200": # status code
3636
description: A JSON array of user names
3737
content:
3838
application/json:
3939
schema:
4040
type: array
4141
items:
42-
$ref: '#/components/schemas/items_object'
42+
$ref: "#/components/schemas/items_object"
4343

4444
post:
45-
tags:
45+
tags:
4646
- Items Object
4747
summary: Creates an item
4848
description: Sends a json object to the server and returns with a new entry with new UUID.
@@ -58,17 +58,17 @@ paths:
5858
type: string
5959
example: This this a thing
6060
responses:
61-
'201':
61+
"201":
6262
description: Status Created successfully.
6363
content:
6464
application/json:
6565
schema:
66-
$ref: '#/components/schemas/items_object'
66+
$ref: "#/components/schemas/items_object"
6767

6868
/items/{itemKey}:
6969
get:
70-
tags:
71-
- Items Object
70+
tags:
71+
- Items Object
7272
summary: Get specific item.
7373
description: Retrieves information for a specific key. If not provided, then the result is root GET request.
7474
parameters:
@@ -80,18 +80,18 @@ paths:
8080
format: uuid
8181
description: Key of object to be retrieved.
8282
responses:
83-
'200': # status code
83+
"200": # status code
8484
description: A JSON object describing the Item.
8585
content:
8686
application/json:
8787
schema:
88-
$ref: '#/components/schemas/items_object'
89-
'404':
88+
$ref: "#/components/schemas/items_object"
89+
"404":
9090
description: Object not found
9191
content:
9292

9393
delete:
94-
tags:
94+
tags:
9595
- Items Object
9696
summary: Deletes the item from database
9797
description: Sends a key to delete from the database.
@@ -104,10 +104,9 @@ paths:
104104
format: uuid
105105
description: The object to be deleted.
106106
responses:
107-
'204': # status code
107+
"204": # status code
108108
description: No Content
109109
content:
110-
'400':
110+
"400":
111111
description: Bad request. Likely the key is not a UUID.
112112
content:
113-

docs/static/swagger-initializer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ window.onload = function () {
33

44
// the following lines will be replaced by docker/configurator, when it runs in a docker-container
55
window.ui = SwaggerUIBundle({
6-
// url: "http://localhost:5050/api/v1/openapi.yaml",
7-
url: "https://api.plainrandom.com/api/v1/openapi.yaml",
6+
url: "http://localhost:5050/api/v1/openapi.yaml",
7+
// url: "https://api.plainrandom.com/api/v1/openapi.yaml",
88
dom_id: '#swagger-ui',
99
deepLinking: true,
1010
presets: [

go.mod

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,41 @@ require (
88
)
99

1010
require (
11+
github.com/bytedance/sonic v1.11.6 // indirect
12+
github.com/bytedance/sonic/loader v0.1.1 // indirect
13+
github.com/cloudwego/base64x v0.1.4 // indirect
14+
github.com/cloudwego/iasm v0.2.0 // indirect
15+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
16+
github.com/gin-contrib/sse v0.1.0 // indirect
1117
github.com/go-chi/cors v1.2.1 // indirect
18+
github.com/go-playground/locales v0.14.1 // indirect
19+
github.com/go-playground/universal-translator v0.18.1 // indirect
20+
github.com/go-playground/validator/v10 v10.20.0 // indirect
21+
github.com/goccy/go-json v0.10.2 // indirect
22+
github.com/json-iterator/go v1.1.12 // indirect
23+
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
24+
github.com/leodido/go-urn v1.4.0 // indirect
25+
github.com/mattn/go-isatty v0.0.20 // indirect
1226
github.com/mattn/go-sqlite3 v1.14.22 // indirect
27+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
28+
github.com/modern-go/reflect2 v1.0.2 // indirect
29+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
30+
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
31+
github.com/ugorji/go/codec v1.2.12 // indirect
32+
golang.org/x/arch v0.8.0 // indirect
33+
golang.org/x/crypto v0.23.0 // indirect
34+
golang.org/x/net v0.25.0 // indirect
35+
golang.org/x/sys v0.20.0 // indirect
36+
google.golang.org/protobuf v1.34.1 // indirect
37+
gopkg.in/yaml.v3 v3.0.1 // indirect
1338
)
1439

1540
require (
41+
github.com/gin-gonic/gin v1.10.1
1642
github.com/go-chi/chi v1.5.5
1743
github.com/go-chi/chi/v5 v5.1.0
1844
github.com/jinzhu/inflection v1.0.0 // indirect
1945
github.com/jinzhu/now v1.1.5 // indirect
20-
golang.org/x/text v0.14.0 // indirect
46+
golang.org/x/text v0.15.0 // indirect
2147
gorm.io/driver/sqlite v1.5.6
2248
)

go.sum

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,99 @@
1+
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
2+
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
3+
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
4+
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
5+
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
6+
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
7+
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
8+
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
9+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
11+
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
12+
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
13+
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
14+
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
15+
github.com/gin-gonic/gin v1.10.1 h1:T0ujvqyCSqRopADpgPgiTT63DUQVSfojyME59Ei63pQ=
16+
github.com/gin-gonic/gin v1.10.1/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
117
github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE=
218
github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw=
319
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
420
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
521
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4=
622
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
23+
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
24+
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
25+
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
26+
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
27+
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
28+
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
29+
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
30+
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
31+
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
732
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
833
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
934
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
1035
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
1136
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
1237
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
38+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
39+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
40+
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
41+
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
42+
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
43+
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
44+
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
45+
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
46+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
47+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
1348
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
1449
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
50+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
51+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
52+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
53+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
54+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
55+
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
56+
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
57+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
58+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
59+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
60+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
61+
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
62+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
63+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
64+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
65+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
66+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
67+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
68+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
69+
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
70+
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
71+
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
72+
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
73+
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
74+
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
75+
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
76+
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
77+
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
78+
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
79+
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
80+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
81+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
82+
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
83+
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
1584
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
1685
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
86+
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
87+
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
88+
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
89+
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
90+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
91+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
92+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
93+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1794
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
1895
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
1996
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
2097
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
98+
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
99+
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

models/handlers.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,37 @@ func (s *ItemService) HandleGetRoot(w http.ResponseWriter, r *http.Request) {
4545
}
4646

4747
w.Header().Add("Content-Type", "application/json")
48-
w.WriteHeader(http.StatusCreated)
48+
w.WriteHeader(http.StatusOK)
4949
w.Write(j)
5050
}
5151

5252
func (s *ItemService) HandleGetItem(w http.ResponseWriter, r *http.Request) {
53+
// Parses the `id` from the URL.
5354
id := chi.URLParam(r, "id")
5455
UUID, err := uuid.Parse(id)
5556
if err != nil {
5657
log.Printf("error parsing uuid from url: %v", err)
5758
w.WriteHeader(http.StatusNotFound)
5859
return
5960
}
61+
62+
// Try to find the item inside of the database.
6063
item, err := s.Repository.Read(UUID)
6164
if err != nil {
6265
log.Printf("error finding book in db: %v", err)
6366
w.WriteHeader(http.StatusNotFound)
6467
return
6568
}
69+
// Convert database data to JSON for response.
6670
j, err := json.Marshal(item)
6771
if err != nil {
6872
log.Printf("error marshalling books into json %v", err)
6973
w.WriteHeader(http.StatusInternalServerError)
7074
return
7175
}
7276

77+
// Add headers and write the response.
78+
7379
w.Header().Add("Content-Type", "application/json")
7480
w.WriteHeader(http.StatusOK)
7581
w.Write(j)
@@ -80,34 +86,42 @@ func (s *ItemService) HandleImportItem(w http.ResponseWriter, r *http.Request) {
8086

8187
decoder := json.NewDecoder(r.Body)
8288

89+
// Decode the body from the request.
8390
err := decoder.Decode(&item)
8491
if err != nil {
8592
log.Printf("Error with decoding json from body: %v", err)
8693
w.WriteHeader(http.StatusInternalServerError)
8794
return
8895
}
8996

97+
// Creates a new UUID for the object.
9098
item.ID, err = uuid.NewUUID()
9199
if err != nil {
92100
log.Printf("Error when assigning new UUID in database: %v", err)
93101
w.WriteHeader(http.StatusInternalServerError)
94102
return
95103
}
104+
105+
// Creates the item inside of database.
96106
retItem, err := s.Repository.Create(&item)
97107
if err != nil {
98108
log.Printf("Error when creating item in database: %v", err)
99109
w.WriteHeader(http.StatusInternalServerError)
100110
return
101111
}
112+
113+
// Marshal the data into json for return body
102114
j, err := json.Marshal(retItem)
103115
if err != nil {
104116
log.Printf("Error response from database: %v", err)
105117
w.WriteHeader(http.StatusInternalServerError)
106118
return
107119
}
108120

121+
// Add headers and write the response.
109122
w.Header().Add("Content-Type", "application/json")
110123
w.WriteHeader(http.StatusCreated)
124+
111125
w.Write(j)
112126
}
113127

models/handlers_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func MustOpenServer(t *testing.T) *server.Server {
5656
DB: DB,
5757

5858
Server: &http.Server{
59-
Addr: "localhost:5060",
59+
Addr: "https://api.plainrandom.com",
60+
// Addr: "localhost:5060",
6061
},
6162
}
6263

@@ -160,8 +161,8 @@ func TestItemService_HandleImportItem(t *testing.T) {
160161
s.ItemService.HandleImportItem(w, r)
161162

162163
// Check response status code
163-
if w.Code != http.StatusOK {
164-
t.Errorf("Expected status code %v, got %v", http.StatusOK, w.Code)
164+
if w.Code != http.StatusCreated {
165+
t.Errorf("Expected status code %v, got %v", http.StatusCreated, w.Code)
165166
}
166167

167168
var response map[string]string

0 commit comments

Comments
 (0)