feat: mark book as read
This commit is contained in:
@@ -9,4 +9,5 @@ type UserBook struct {
|
||||
BookID uint
|
||||
Book Book
|
||||
Rating int
|
||||
Read bool
|
||||
}
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type userBookPostCreate struct {
|
||||
type bookPostMarkAsRead struct {
|
||||
BookID uint `json:"bookId" binding:"required"`
|
||||
}
|
||||
|
||||
func PostUserBookHandler(ac appcontext.AppContext) {
|
||||
var userbook userBookPostCreate
|
||||
func PostBookReadHandler(ac appcontext.AppContext) {
|
||||
var userbook bookPostMarkAsRead
|
||||
err := ac.C.ShouldBindJSON(&userbook)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
@@ -29,18 +31,33 @@ func PostUserBookHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbookDb := userBookWsToDb(userbook, &user)
|
||||
err = ac.Db.Save(&userbookDb).Error
|
||||
|
||||
var userbookDb model.UserBook
|
||||
res := ac.Db.Where("user_id = ? AND book_id = ?", user.ID, userbook.BookID).First(&userbookDb)
|
||||
err = res.Error
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
userbookDb = userBookWsToDb(userbook, &user)
|
||||
err = ac.Db.Save(&userbookDb).Error
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
userbookDb.Read = true
|
||||
ac.Db.Save(&userbookDb)
|
||||
}
|
||||
ac.C.String(http.StatusOK, "Success")
|
||||
}
|
||||
|
||||
func userBookWsToDb(ub userBookPostCreate, user *model.User) model.UserBook {
|
||||
func userBookWsToDb(ub bookPostMarkAsRead, user *model.User) model.UserBook {
|
||||
return model.UserBook{
|
||||
UserID: user.ID,
|
||||
BookID: ub.BookID,
|
||||
Read: true,
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ type bookUserGet struct {
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Author string `json:"author" binding:"max=100"`
|
||||
Rating int `json:"rating" binding:"min=0,max=10"`
|
||||
Read bool `json:"read" binding:"boolean"`
|
||||
}
|
||||
|
||||
func GetMyBooksHanderl(ac appcontext.AppContext) {
|
||||
@@ -34,5 +35,6 @@ func userBookDbToWs(b *model.UserBook) bookUserGet {
|
||||
Title: b.Book.Title,
|
||||
Author: b.Book.Author,
|
||||
Rating: b.Rating,
|
||||
Read: b.Read,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,8 @@ func Setup(config *config.Config) *gin.Engine {
|
||||
r.POST("/book", func(c *gin.Context) {
|
||||
routes.PostBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle})
|
||||
})
|
||||
r.POST("/userbook", func(c *gin.Context) {
|
||||
routes.PostUserBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle})
|
||||
r.POST("/book/read", func(c *gin.Context) {
|
||||
routes.PostBookReadHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle})
|
||||
})
|
||||
r.POST("/auth/signup", func(c *gin.Context) {
|
||||
routes.PostSignupHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle})
|
||||
|
||||
Reference in New Issue
Block a user