Skip to content

Commit bc233f5

Browse files
committed
Apply core-go/sql and core-go/service
1 parent 09b7106 commit bc233f5

File tree

12 files changed

+314
-885
lines changed

12 files changed

+314
-885
lines changed

README.md

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ go run main.go
1010
![Architecture](https://camo.githubusercontent.com/c17d4dfaab39cf7223f7775c9e973bb936e4169e8bd0011659e83cec755c8f26/68747470733a2f2f63646e2d696d616765732d312e6d656469756d2e636f6d2f6d61782f3830302f312a42526b437272622d5f417637395167737142556b48672e706e67)
1111

1212
### Architecture with standard features: config, health check, logging, middleware log tracing
13-
![Architecture with standard features: config, health check, logging, middleware log tracing](https://camo.githubusercontent.com/bd77867d332213b6d54d80b19f46c3dd0f1b8e0b9bb155f8ff502d9fc3bdcded/68747470733a2f2f63646e2d696d616765732d312e6d656469756d2e636f6d2f6d61782f3830302f312a476d306479704c7559615077474d38557a727a5637772e706e67)
13+
![Architecture with standard features: config, health check, logging, middleware log tracing](https://camo.githubusercontent.com/fa1158e7f94bf96e09aef42fcead23366839baf71190133d5df10f3006b2e041/68747470733a2f2f63646e2d696d616765732d312e6d656469756d2e636f6d2f6d61782f3830302f312a6d494e3344556569365676316c755a376747727655412e706e67)
14+
1415
#### [core-go/search](https://github.com/core-go/search)
1516
- Build the search model at http handler
1617
- Build dynamic SQL for search
@@ -66,7 +67,7 @@ In this sample, search users with these criteria:
6667
- GET: retrieve a representation of the resource
6768
- POST: create a new resource
6869
- PUT: update the resource
69-
- PATCH: perform a partial update of a resource, refer to [service](https://github.com/core-go/service) and [mongo](https://github.com/core-go/mongo)
70+
- PATCH: perform a partial update of a resource, refer to [service](https://github.com/core-go/service) and [sql](https://github.com/core-go/sql)
7071
- DELETE: delete a resource
7172

7273
## API design for health check
@@ -77,7 +78,7 @@ To check if the service is available.
7778
{
7879
"status": "UP",
7980
"details": {
80-
"mongo": {
81+
"sql": {
8182
"status": "UP"
8283
}
8384
}
@@ -136,9 +137,46 @@ GET /users/wolverine
136137
"dateOfBirth": "1974-11-16T16:59:59.999Z"
137138
}
138139
```
139-
#### *Response:* 1: success, 0: duplicate key, -1: error
140+
#### *Response:*
141+
- status: configurable; 1: success, 0: duplicate key, 4: error
140142
```json
141-
1
143+
{
144+
"status": 1,
145+
"value": {
146+
"id": "wolverine",
147+
"username": "james.howlett",
148+
"email": "[email protected]",
149+
"phone": "0987654321",
150+
"dateOfBirth": "1974-11-16T00:00:00+07:00"
151+
}
152+
}
153+
```
154+
#### *Fail case sample:*
155+
- Request:
156+
```json
157+
{
158+
"id": "wolverine",
159+
"username": "james.howlett",
160+
"email": "james.howlett",
161+
"phone": "0987654321a",
162+
"dateOfBirth": "1974-11-16T16:59:59.999Z"
163+
}
164+
```
165+
- Response: in this below sample, email and phone are not valid
166+
```json
167+
{
168+
"status": 4,
169+
"errors": [
170+
{
171+
"field": "email",
172+
"code": "email"
173+
},
174+
{
175+
"field": "phone",
176+
"code": "phone"
177+
}
178+
]
179+
}
142180
```
143181

144182
### Update one user by id
@@ -154,9 +192,19 @@ PUT /users/wolverine
154192
"dateOfBirth": "1974-11-16T16:59:59.999Z"
155193
}
156194
```
157-
#### *Response:* 1: success, 0: not found, -1: error
195+
#### *Response:*
196+
- status: configurable; 1: success, 0: duplicate key, 2: version error, 4: error
158197
```json
159-
1
198+
{
199+
"status": 1,
200+
"value": {
201+
"id": "wolverine",
202+
"username": "james.howlett",
203+
"email": "[email protected]",
204+
"phone": "0987654321",
205+
"dateOfBirth": "1974-11-16T00:00:00+07:00"
206+
}
207+
}
160208
```
161209

162210
### Patch one user by id
@@ -171,9 +219,16 @@ PATCH /users/wolverine
171219
"phone": "0987654321"
172220
}
173221
```
174-
#### *Response:* 1: success, 0: not found, -1: error
222+
#### *Response:*
223+
- status: configurable; 1: success, 0: duplicate key, 2: version error, 4: error
175224
```json
176-
1
225+
{
226+
"status": 1,
227+
"value": {
228+
"email": "[email protected]",
229+
"phone": "0987654321"
230+
}
231+
}
177232
```
178233

179234
#### Problems for patch
@@ -186,7 +241,7 @@ type UserService interface {
186241
```
187242
We must solve 2 problems:
188243
1. At http handler layer, we must convert the user struct to map, with json format, and make sure the nested data types are passed correctly.
189-
2. At repository layer, from json format, we must convert the json format to database format (in this case, we must convert to bson of Mongo)
244+
2. At repository layer, from json format, we must convert the json format to database column name
190245

191246
#### Solutions for patch
192247
At http handler layer, we use [core-go/service](https://github.com/core-go/service), to convert the user struct to map, to make sure we just update the fields we need to update

configs/config.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ middleware:
2626
map:
2727
username: username
2828

29+
status:
30+
not_found: 0
31+
duplicate_key: 0
32+
success: 1
33+
version_error: 2
34+
validation_error: 4
35+
error: 4
36+
37+
action:
38+
create: create
39+
update: update
40+
patch: patch
41+
delete: delete
42+
2943
client:
3044
endpoint:
3145
url: "http://localhost:8080/users"

0 commit comments

Comments
 (0)