Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions app/book/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
45 changes: 8 additions & 37 deletions model/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 9 additions & 5 deletions openapi-v3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -33,6 +35,8 @@ components:
type: string
title:
type: string
updated_at:
type: string
type: object
myapp_pkg_errors.Error:
properties:
Expand Down Expand Up @@ -92,7 +96,7 @@ paths:
application/json:
schema:
items:
$ref: '#/components/schemas/model.BookDTO'
$ref: '#/components/schemas/model.Book'
type: array
description: OK
"500":
Expand Down Expand Up @@ -122,7 +126,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/model.BookDTO'
$ref: '#/components/schemas/model.Book'
description: Created
"400":
content:
Expand Down Expand Up @@ -203,7 +207,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/model.BookDTO'
$ref: '#/components/schemas/model.Book'
description: OK
"400":
content:
Expand Down Expand Up @@ -247,7 +251,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/model.BookDTO'
$ref: '#/components/schemas/model.Book'
description: OK
"400":
content:
Expand Down