Skip to content

Commit 7f39a3a

Browse files
committed
Add repository and adapter
1 parent c936971 commit 7f39a3a

File tree

17 files changed

+1171
-651
lines changed

17 files changed

+1171
-651
lines changed

README.md

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# go-sql-rest-api
1+
# go-sql-modular-sample
22

33
#### To run the application
44
```shell
55
go run main.go
66
```
77

88
## Architecture
9-
### Architecture
10-
![Architecture](https://camo.githubusercontent.com/c17d4dfaab39cf7223f7775c9e973bb936e4169e8bd0011659e83cec755c8f26/68747470733a2f2f63646e2d696d616765732d312e6d656469756d2e636f6d2f6d61782f3830302f312a42526b437272622d5f417637395167737142556b48672e706e67)
9+
### Simple Layer Architecture
10+
![Layer Architecture](https://cdn-images-1.medium.com/max/800/1*JDYTlK00yg0IlUjZ9-sp7Q.png)
1111

12-
### 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/fa1158e7f94bf96e09aef42fcead23366839baf71190133d5df10f3006b2e041/68747470733a2f2f63646e2d696d616765732d312e6d656469756d2e636f6d2f6d61782f3830302f312a6d494e3344556569365676316c755a376747727655412e706e67)
12+
### Layer Architecture with full features
13+
![Layer Architecture with standard features: config, health check, logging, middleware log tracing](https://cdn-images-1.medium.com/max/800/1*8UjJSv_tW0xBKFXKZu86MA.png)
1414

1515
#### [core-go/search](https://github.com/core-go/search)
1616
- Build the search model at http handler
@@ -67,7 +67,7 @@ In this sample, search users with these criteria:
6767
- GET: retrieve a representation of the resource
6868
- POST: create a new resource
6969
- PUT: update the resource
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)
70+
- PATCH: perform a partial update of a resource, refer to [core-go/core](https://github.com/core-go/core) and [core-go/sql](https://github.com/core-go/sql)
7171
- DELETE: delete a resource
7272

7373
## API design for health check
@@ -137,46 +137,9 @@ GET /users/wolverine
137137
"dateOfBirth": "1974-11-16T16:59:59.999Z"
138138
}
139139
```
140-
#### *Response:*
141-
- status: configurable; 1: success, 0: duplicate key, 4: error
142-
```json
143-
{
144-
"status": 1,
145-
"value": {
146-
"id": "wolverine",
147-
"username": "james.howlett",
148-
"email": "james.howlett@gmail.com",
149-
"phone": "0987654321",
150-
"dateOfBirth": "1974-11-16T00:00:00+07:00"
151-
}
152-
}
153-
```
154-
#### *Fail case sample:*
155-
- Request:
140+
#### *Response:* 1: success, 0: duplicate key, -1: error
156141
```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-
}
142+
1
180143
```
181144

182145
### Update one user by id
@@ -192,19 +155,9 @@ PUT /users/wolverine
192155
"dateOfBirth": "1974-11-16T16:59:59.999Z"
193156
}
194157
```
195-
#### *Response:*
196-
- status: configurable; 1: success, 0: duplicate key, 2: version error, 4: error
158+
#### *Response:* 1: success, 0: not found, -1: error
197159
```json
198-
{
199-
"status": 1,
200-
"value": {
201-
"id": "wolverine",
202-
"username": "james.howlett",
203-
"email": "james.howlett@gmail.com",
204-
"phone": "0987654321",
205-
"dateOfBirth": "1974-11-16T00:00:00+07:00"
206-
}
207-
}
160+
1
208161
```
209162

210163
### Patch one user by id
@@ -219,16 +172,9 @@ PATCH /users/wolverine
219172
"phone": "0987654321"
220173
}
221174
```
222-
#### *Response:*
223-
- status: configurable; 1: success, 0: duplicate key, 2: version error, 4: error
175+
#### *Response:* 1: success, 0: not found, -1: error
224176
```json
225-
{
226-
"status": 1,
227-
"value": {
228-
"email": "james.howlett@gmail.com",
229-
"phone": "0987654321"
230-
}
231-
}
177+
1
232178
```
233179

234180
#### Problems for patch
@@ -241,19 +187,19 @@ type UserService interface {
241187
```
242188
We must solve 2 problems:
243189
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.
244-
2. At repository layer, from json format, we must convert the json format to database column name
190+
2. At repository layer, from json format, we must convert the json format to database format (in this case, we must convert to column)
245191

246192
#### Solutions for patch
247-
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
193+
At http handler layer, we use [core-go/core](https://github.com/core-go/core), to convert the user struct to map, to make sure we just update the fields we need to update
248194
```go
249-
import server "github.com/core-go/service"
195+
import "github.com/core-go/core"
250196

251197
func (h *UserHandler) Patch(w http.ResponseWriter, r *http.Request) {
252198
var user User
253199
userType := reflect.TypeOf(user)
254-
_, jsonMap := sv.BuildMapField(userType)
255-
body, _ := sv.BuildMapAndStruct(r, &user)
256-
json, er1 := sv.BodyToJson(r, user, body, ids, jsonMap, nil)
200+
_, jsonMap := core.BuildMapField(userType)
201+
body, _ := core.BuildMapAndStruct(r, &user)
202+
json, er1 := core.BodyToJson(r, user, body, ids, jsonMap, nil)
257203

258204
result, er2 := h.service.Patch(r.Context(), json)
259205
if er2 != nil {
@@ -277,7 +223,8 @@ DELETE /users/wolverine
277223
## Common libraries
278224
- [core-go/health](https://github.com/core-go/health): include HealthHandler, HealthChecker, SqlHealthChecker
279225
- [core-go/config](https://github.com/core-go/config): to load the config file, and merge with other environments (SIT, UAT, ENV)
280-
- [core-go/log](https://github.com/core-go/log): log and log middleware
226+
- [core-go/log](https://github.com/core-go/log): logging
227+
- [core-go/middleware](https://github.com/core-go/log): middleware log tracing
281228

282229
### core-go/health
283230
To check if the service is available, refer to [core-go/health](https://github.com/core-go/health)
@@ -352,7 +299,7 @@ func main() {
352299

353300
log.Initialize(conf.Log)
354301
r.Use(m.BuildContext)
355-
logger := m.NewLogger()
302+
logger := m.NewStructuredLogger()
356303
r.Use(m.Logger(conf.MiddleWare, log.InfoFields, logger))
357304
r.Use(m.Recover(log.ErrorMsg))
358305
}

0 commit comments

Comments
 (0)