Refactor: merge dto, mapper and routes
This commit is contained in:
@@ -4,14 +4,16 @@ import (
|
||||
"net/http"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/mapper"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
)
|
||||
|
||||
type userBookPostCreate struct {
|
||||
BookID uint `json:"bookId" binding:"required"`
|
||||
}
|
||||
|
||||
func PostUserBookHandler(ac appcontext.AppContext) {
|
||||
var userbook dto.UserBookPostCreate
|
||||
var userbook userBookPostCreate
|
||||
err := ac.C.ShouldBindJSON(&userbook)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
@@ -27,7 +29,7 @@ func PostUserBookHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbookDb := mapper.UserBookWsToDb(userbook, &user)
|
||||
userbookDb := userBookWsToDb(userbook, &user)
|
||||
err = ac.Db.Save(&userbookDb).Error
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
@@ -35,3 +37,10 @@ func PostUserBookHandler(ac appcontext.AppContext) {
|
||||
}
|
||||
ac.C.String(http.StatusOK, "Success")
|
||||
}
|
||||
|
||||
func userBookWsToDb(ub userBookPostCreate, user *model.User) model.UserBook {
|
||||
return model.UserBook{
|
||||
UserID: user.ID,
|
||||
BookID: ub.BookID,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user