From b0ed20ce81e32cca495b63efd0e5fc31222df4e2 Mon Sep 17 00:00:00 2001 From: Dumindu Madunuwan Date: Sat, 27 Jun 2026 14:18:43 +0800 Subject: [PATCH] format struct-tag --- app/book/handler.go | 16 ++++++++-------- model/book.go | 45 ++++++++------------------------------------- openapi-v3.yaml | 14 +++++++++----- 3 files changed, 25 insertions(+), 50 deletions(-) diff --git a/app/book/handler.go b/app/book/handler.go index 7302df7..92f64e3 100644 --- a/app/book/handler.go +++ b/app/book/handler.go @@ -42,7 +42,7 @@ func New(logger *l.Logger, validator *validator.Validate, db *gorm.DB) *API { // @produce json // @Param page query int64 false "Page number for pagination" default(1) minimum(1) // @Param pageSize query int64 false "Number of items per page" default(10) minimum(1) maximum(100) -// @success 200 {array} model.BookDTO +// @success 200 {array} model.Book // @failure 500 {object} e.Error // @router /books [get] func (a *API) List(w http.ResponseWriter, r *http.Request) { @@ -62,7 +62,7 @@ func (a *API) List(w http.ResponseWriter, r *http.Request) { return } - if err := json.MarshalWrite(w, books.ToDTO()); err != nil { + if err := json.MarshalWrite(w, books); err != nil { a.logger.Error().Str(l.KeyReqID, reqID).Err(err).Msg("") e.ServerError(w, e.RespJSONEncodeFailure) return @@ -77,7 +77,7 @@ func (a *API) List(w http.ResponseWriter, r *http.Request) { // @accept json // @produce json // @param body body form.BookForm true "Book form" -// @success 201 {object} model.BookDTO +// @success 201 {object} model.Book // @failure 400 {object} e.Error // @failure 422 {object} e.Errors // @failure 500 {object} e.Error @@ -101,7 +101,7 @@ func (a *API) Create(w http.ResponseWriter, r *http.Request) { } w.WriteHeader(http.StatusCreated) - if err := json.MarshalWrite(w, book.ToDTO()); err != nil { + if err := json.MarshalWrite(w, book); err != nil { a.logger.Error().Str(l.KeyReqID, reqID).Err(err).Msg("") e.ServerError(w, e.RespJSONEncodeFailure) return @@ -118,7 +118,7 @@ func (a *API) Create(w http.ResponseWriter, r *http.Request) { // @accept json // @produce json // @param id path string true "Book ID" -// @success 200 {object} model.BookDTO +// @success 200 {object} model.Book // @failure 400 {object} e.Error // @failure 404 // @failure 500 {object} e.Error @@ -145,7 +145,7 @@ func (a *API) Read(w http.ResponseWriter, r *http.Request) { return } - if err := json.MarshalWrite(w, book.ToDTO()); err != nil { + if err := json.MarshalWrite(w, book); err != nil { a.logger.Error().Str(l.KeyReqID, reqID).Err(err).Msg("") e.ServerError(w, e.RespJSONEncodeFailure) return @@ -161,7 +161,7 @@ func (a *API) Read(w http.ResponseWriter, r *http.Request) { // @produce json // @param id path string true "Book ID" // @param body body form.BookForm true "Book form" -// @success 200 {object} model.BookDTO +// @success 200 {object} model.Book // @failure 400 {object} e.Error // @failure 404 // @failure 422 {object} e.Errors @@ -196,7 +196,7 @@ func (a *API) Update(w http.ResponseWriter, r *http.Request) { return } - if err := json.MarshalWrite(w, book.ToDTO()); err != nil { + if err := json.MarshalWrite(w, book); err != nil { a.logger.Error().Str(l.KeyReqID, reqID).Err(err).Msg("") e.ServerError(w, e.RespJSONEncodeFailure) return diff --git a/model/book.go b/model/book.go index 4e99e85..54c3bc9 100644 --- a/model/book.go +++ b/model/book.go @@ -9,42 +9,13 @@ import ( type ( Books []*Book Book struct { - ID uuid.UUID `gorm:"primarykey"` - Title string - Author string - PublishedDate time.Time - ImageURL string - Description string - CreatedAt time.Time `gorm:"autoCreateTime"` - UpdatedAt time.Time `gorm:"autoUpdateTime"` + ID uuid.UUID `json:"id" gorm:"primarykey"` + Title string `json:"title"` + Author string `json:"author"` + PublishedDate time.Time `json:"published_date,format:'2006-01-02'"` + ImageURL string `json:"image_url"` + Description string `json:"description"` + CreatedAt time.Time `json:"created_at,format:RFC3339" gorm:"autoCreateTime"` + UpdatedAt time.Time `json:"updated_at,format:RFC3339" gorm:"autoUpdateTime"` } ) - -type BookDTO struct { - ID string `json:"id"` - Title string `json:"title"` - Author string `json:"author"` - PublishedDate string `json:"published_date"` - ImageURL string `json:"image_url"` - Description string `json:"description"` -} - -func (b *Book) ToDTO() *BookDTO { - return &BookDTO{ - ID: b.ID.String(), - Title: b.Title, - Author: b.Author, - PublishedDate: b.PublishedDate.Format("2006-01-02"), - ImageURL: b.ImageURL, - Description: b.Description, - } -} - -func (bs Books) ToDTO() []*BookDTO { - resp := make([]*BookDTO, len(bs)) - for i, v := range bs { - resp[i] = v.ToDTO() - } - - return resp -} diff --git a/openapi-v3.yaml b/openapi-v3.yaml index b5867b4..cba107e 100644 --- a/openapi-v3.yaml +++ b/openapi-v3.yaml @@ -19,10 +19,12 @@ components: - published_date - title type: object - model.BookDTO: + model.Book: properties: author: type: string + created_at: + type: string description: type: string id: @@ -33,6 +35,8 @@ components: type: string title: type: string + updated_at: + type: string type: object myapp_pkg_errors.Error: properties: @@ -92,7 +96,7 @@ paths: application/json: schema: items: - $ref: '#/components/schemas/model.BookDTO' + $ref: '#/components/schemas/model.Book' type: array description: OK "500": @@ -122,7 +126,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/model.BookDTO' + $ref: '#/components/schemas/model.Book' description: Created "400": content: @@ -203,7 +207,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/model.BookDTO' + $ref: '#/components/schemas/model.Book' description: OK "400": content: @@ -247,7 +251,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/model.BookDTO' + $ref: '#/components/schemas/model.Book' description: OK "400": content: