Skip to content

Commit 083fe97

Browse files
authored
format struct-tag
format struct-tag
2 parents 37ac3e5 + b0ed20c commit 083fe97

3 files changed

Lines changed: 25 additions & 50 deletions

File tree

app/book/handler.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func New(logger *l.Logger, validator *validator.Validate, db *gorm.DB) *API {
4242
// @produce json
4343
// @Param page query int64 false "Page number for pagination" default(1) minimum(1)
4444
// @Param pageSize query int64 false "Number of items per page" default(10) minimum(1) maximum(100)
45-
// @success 200 {array} model.BookDTO
45+
// @success 200 {array} model.Book
4646
// @failure 500 {object} e.Error
4747
// @router /books [get]
4848
func (a *API) List(w http.ResponseWriter, r *http.Request) {
@@ -62,7 +62,7 @@ func (a *API) List(w http.ResponseWriter, r *http.Request) {
6262
return
6363
}
6464

65-
if err := json.MarshalWrite(w, books.ToDTO()); err != nil {
65+
if err := json.MarshalWrite(w, books); err != nil {
6666
a.logger.Error().Str(l.KeyReqID, reqID).Err(err).Msg("")
6767
e.ServerError(w, e.RespJSONEncodeFailure)
6868
return
@@ -77,7 +77,7 @@ func (a *API) List(w http.ResponseWriter, r *http.Request) {
7777
// @accept json
7878
// @produce json
7979
// @param body body form.BookForm true "Book form"
80-
// @success 201 {object} model.BookDTO
80+
// @success 201 {object} model.Book
8181
// @failure 400 {object} e.Error
8282
// @failure 422 {object} e.Errors
8383
// @failure 500 {object} e.Error
@@ -101,7 +101,7 @@ func (a *API) Create(w http.ResponseWriter, r *http.Request) {
101101
}
102102

103103
w.WriteHeader(http.StatusCreated)
104-
if err := json.MarshalWrite(w, book.ToDTO()); err != nil {
104+
if err := json.MarshalWrite(w, book); err != nil {
105105
a.logger.Error().Str(l.KeyReqID, reqID).Err(err).Msg("")
106106
e.ServerError(w, e.RespJSONEncodeFailure)
107107
return
@@ -118,7 +118,7 @@ func (a *API) Create(w http.ResponseWriter, r *http.Request) {
118118
// @accept json
119119
// @produce json
120120
// @param id path string true "Book ID"
121-
// @success 200 {object} model.BookDTO
121+
// @success 200 {object} model.Book
122122
// @failure 400 {object} e.Error
123123
// @failure 404
124124
// @failure 500 {object} e.Error
@@ -145,7 +145,7 @@ func (a *API) Read(w http.ResponseWriter, r *http.Request) {
145145
return
146146
}
147147

148-
if err := json.MarshalWrite(w, book.ToDTO()); err != nil {
148+
if err := json.MarshalWrite(w, book); err != nil {
149149
a.logger.Error().Str(l.KeyReqID, reqID).Err(err).Msg("")
150150
e.ServerError(w, e.RespJSONEncodeFailure)
151151
return
@@ -161,7 +161,7 @@ func (a *API) Read(w http.ResponseWriter, r *http.Request) {
161161
// @produce json
162162
// @param id path string true "Book ID"
163163
// @param body body form.BookForm true "Book form"
164-
// @success 200 {object} model.BookDTO
164+
// @success 200 {object} model.Book
165165
// @failure 400 {object} e.Error
166166
// @failure 404
167167
// @failure 422 {object} e.Errors
@@ -196,7 +196,7 @@ func (a *API) Update(w http.ResponseWriter, r *http.Request) {
196196
return
197197
}
198198

199-
if err := json.MarshalWrite(w, book.ToDTO()); err != nil {
199+
if err := json.MarshalWrite(w, book); err != nil {
200200
a.logger.Error().Str(l.KeyReqID, reqID).Err(err).Msg("")
201201
e.ServerError(w, e.RespJSONEncodeFailure)
202202
return

model/book.go

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,13 @@ import (
99
type (
1010
Books []*Book
1111
Book struct {
12-
ID uuid.UUID `gorm:"primarykey"`
13-
Title string
14-
Author string
15-
PublishedDate time.Time
16-
ImageURL string
17-
Description string
18-
CreatedAt time.Time `gorm:"autoCreateTime"`
19-
UpdatedAt time.Time `gorm:"autoUpdateTime"`
12+
ID uuid.UUID `json:"id" gorm:"primarykey"`
13+
Title string `json:"title"`
14+
Author string `json:"author"`
15+
PublishedDate time.Time `json:"published_date,format:'2006-01-02'"`
16+
ImageURL string `json:"image_url"`
17+
Description string `json:"description"`
18+
CreatedAt time.Time `json:"created_at,format:RFC3339" gorm:"autoCreateTime"`
19+
UpdatedAt time.Time `json:"updated_at,format:RFC3339" gorm:"autoUpdateTime"`
2020
}
2121
)
22-
23-
type BookDTO struct {
24-
ID string `json:"id"`
25-
Title string `json:"title"`
26-
Author string `json:"author"`
27-
PublishedDate string `json:"published_date"`
28-
ImageURL string `json:"image_url"`
29-
Description string `json:"description"`
30-
}
31-
32-
func (b *Book) ToDTO() *BookDTO {
33-
return &BookDTO{
34-
ID: b.ID.String(),
35-
Title: b.Title,
36-
Author: b.Author,
37-
PublishedDate: b.PublishedDate.Format("2006-01-02"),
38-
ImageURL: b.ImageURL,
39-
Description: b.Description,
40-
}
41-
}
42-
43-
func (bs Books) ToDTO() []*BookDTO {
44-
resp := make([]*BookDTO, len(bs))
45-
for i, v := range bs {
46-
resp[i] = v.ToDTO()
47-
}
48-
49-
return resp
50-
}

openapi-v3.yaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ components:
1919
- published_date
2020
- title
2121
type: object
22-
model.BookDTO:
22+
model.Book:
2323
properties:
2424
author:
2525
type: string
26+
created_at:
27+
type: string
2628
description:
2729
type: string
2830
id:
@@ -33,6 +35,8 @@ components:
3335
type: string
3436
title:
3537
type: string
38+
updated_at:
39+
type: string
3640
type: object
3741
myapp_pkg_errors.Error:
3842
properties:
@@ -92,7 +96,7 @@ paths:
9296
application/json:
9397
schema:
9498
items:
95-
$ref: '#/components/schemas/model.BookDTO'
99+
$ref: '#/components/schemas/model.Book'
96100
type: array
97101
description: OK
98102
"500":
@@ -122,7 +126,7 @@ paths:
122126
content:
123127
application/json:
124128
schema:
125-
$ref: '#/components/schemas/model.BookDTO'
129+
$ref: '#/components/schemas/model.Book'
126130
description: Created
127131
"400":
128132
content:
@@ -203,7 +207,7 @@ paths:
203207
content:
204208
application/json:
205209
schema:
206-
$ref: '#/components/schemas/model.BookDTO'
210+
$ref: '#/components/schemas/model.Book'
207211
description: OK
208212
"400":
209213
content:
@@ -247,7 +251,7 @@ paths:
247251
content:
248252
application/json:
249253
schema:
250-
$ref: '#/components/schemas/model.BookDTO'
254+
$ref: '#/components/schemas/model.Book'
251255
description: OK
252256
"400":
253257
content:

0 commit comments

Comments
 (0)