Refactor: make only one API route to update userbooks
This commit is contained in:
@@ -7,21 +7,35 @@ import (
|
||||
"time"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/appcontext"
|
||||
"git.artlef.fr/bibliomane/internal/dto"
|
||||
"git.artlef.fr/bibliomane/internal/model"
|
||||
"git.artlef.fr/bibliomane/internal/myvalidator"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func PutReadUserBookHandler(ac appcontext.AppContext) {
|
||||
data, err := retrieveDataFromContext(ac)
|
||||
func PutUserBookHandler(ac appcontext.AppContext) {
|
||||
bookId64, err := strconv.ParseUint(ac.C.Param("id"), 10, 64)
|
||||
bookId := uint(bookId64)
|
||||
if err != nil {
|
||||
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err})
|
||||
return
|
||||
}
|
||||
bookId := data.BookId
|
||||
user := data.User
|
||||
var read userbookPutRead
|
||||
err = ac.C.ShouldBindJSON(&read)
|
||||
err = myvalidator.ValidateId(ac.Db, bookId, &model.Book{})
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := ac.GetAuthenticatedUser()
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
|
||||
var userBookPut dto.UserBookPutUpdate
|
||||
err = ac.C.ShouldBindJSON(&userBookPut)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
@@ -32,14 +46,53 @@ func PutReadUserBookHandler(ac appcontext.AppContext) {
|
||||
return
|
||||
}
|
||||
|
||||
userbook.Read = read.Read
|
||||
|
||||
if read.EndDate != "" {
|
||||
d, err := parseDate(read.EndDate)
|
||||
if userBookPut.Read != nil {
|
||||
err = updateReadStatus(&userbook, &userBookPut)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if userBookPut.WantRead != nil {
|
||||
userbook.WantRead = *userBookPut.WantRead
|
||||
}
|
||||
if userBookPut.StartDate != nil {
|
||||
d, err := parseDate(*userBookPut.StartDate)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbook.StartReadDate = d
|
||||
}
|
||||
if userBookPut.Rating != nil {
|
||||
err = validateRating(*userBookPut.Rating)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
}
|
||||
updateRating(&userbook, &userBookPut)
|
||||
}
|
||||
ac.Db.Save(&userbook)
|
||||
ac.C.String(http.StatusOK, "Success")
|
||||
}
|
||||
|
||||
func validateRating(rating int) error {
|
||||
//struct used for validation
|
||||
var ratingStruct struct {
|
||||
Rating int `validate:"gte=0,lte=10"`
|
||||
}
|
||||
ratingStruct.Rating = rating
|
||||
validate := validator.New()
|
||||
return validate.Struct(ratingStruct)
|
||||
}
|
||||
|
||||
func updateReadStatus(userbook *model.UserBook, userBookPut *dto.UserBookPutUpdate) error {
|
||||
userbook.Read = *userBookPut.Read
|
||||
|
||||
if userBookPut.EndDate != nil && *userBookPut.EndDate != "" {
|
||||
d, err := parseDate(*userBookPut.EndDate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userbook.EndReadDate = d
|
||||
}
|
||||
|
||||
@@ -52,84 +105,11 @@ func PutReadUserBookHandler(ac appcontext.AppContext) {
|
||||
if !userbook.Read {
|
||||
userbook.EndReadDate = nil
|
||||
}
|
||||
|
||||
ac.Db.Save(&userbook)
|
||||
ac.C.String(http.StatusOK, "Success")
|
||||
return nil
|
||||
}
|
||||
|
||||
func PutWantReadUserBookHandler(ac appcontext.AppContext) {
|
||||
data, err := retrieveDataFromContext(ac)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
bookId := data.BookId
|
||||
user := data.User
|
||||
var wantread userbookPutWantRead
|
||||
err = ac.C.ShouldBindJSON(&wantread)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbook, err := fetchOrCreateUserBook(ac, bookId, &user)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbook.WantRead = wantread.WantRead
|
||||
ac.Db.Save(&userbook)
|
||||
ac.C.String(http.StatusOK, "Success")
|
||||
}
|
||||
|
||||
func PutStartReadUserBookHandler(ac appcontext.AppContext) {
|
||||
data, err := retrieveDataFromContext(ac)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
bookId := data.BookId
|
||||
user := data.User
|
||||
|
||||
var startDateToParse userbookPutStartRead
|
||||
err = ac.C.ShouldBindJSON(&startDateToParse)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbook, err := fetchOrCreateUserBook(ac, bookId, &user)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
|
||||
d, err := parseDate(startDateToParse.StartDate)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbook.StartReadDate = d
|
||||
|
||||
ac.Db.Save(&userbook)
|
||||
ac.C.String(http.StatusOK, "Success")
|
||||
}
|
||||
|
||||
func PutRateUserBookHandler(ac appcontext.AppContext) {
|
||||
data, err := retrieveDataFromContext(ac)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
bookId := data.BookId
|
||||
user := data.User
|
||||
var rating userbookPutRating
|
||||
err = ac.C.ShouldBindJSON(&rating)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbook, err := fetchOrCreateUserBook(ac, bookId, &user)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
userbook.Rating = rating.Rating
|
||||
func updateRating(userbook *model.UserBook, userBookPut *dto.UserBookPutUpdate) {
|
||||
userbook.Rating = *userBookPut.Rating
|
||||
|
||||
//if rated, set to "read" (a rating = 0 means unrated)
|
||||
if userbook.Rating > 0 {
|
||||
@@ -137,30 +117,6 @@ func PutRateUserBookHandler(ac appcontext.AppContext) {
|
||||
//if set to read, remove want read
|
||||
userbook.WantRead = false
|
||||
}
|
||||
ac.Db.Save(&userbook)
|
||||
ac.C.String(http.StatusOK, "Success")
|
||||
}
|
||||
|
||||
type userbookPutRead struct {
|
||||
Read bool `json:"read"`
|
||||
EndDate string `json:"endDate"`
|
||||
}
|
||||
|
||||
type userbookPutWantRead struct {
|
||||
WantRead bool `json:"wantread"`
|
||||
}
|
||||
|
||||
type userbookPutRating struct {
|
||||
Rating int `json:"rating" binding:"min=0,max=10"`
|
||||
}
|
||||
|
||||
type userbookPutStartRead struct {
|
||||
StartDate string `json:"startDate" binding:"required"`
|
||||
}
|
||||
|
||||
type apiCallData struct {
|
||||
BookId uint
|
||||
User model.User
|
||||
}
|
||||
|
||||
func parseDate(dateToParse string) (*time.Time, error) {
|
||||
@@ -173,27 +129,6 @@ func parseDate(dateToParse string) (*time.Time, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func retrieveDataFromContext(ac appcontext.AppContext) (apiCallData, error) {
|
||||
bookId64, err := strconv.ParseUint(ac.C.Param("id"), 10, 64)
|
||||
bookId := uint(bookId64)
|
||||
if err != nil {
|
||||
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err})
|
||||
return apiCallData{}, err
|
||||
}
|
||||
err = myvalidator.ValidateId(ac.Db, bookId, &model.Book{})
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return apiCallData{}, err
|
||||
}
|
||||
|
||||
user, fetchUserErr := ac.GetAuthenticatedUser()
|
||||
if fetchUserErr != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return apiCallData{}, fetchUserErr
|
||||
}
|
||||
return apiCallData{BookId: bookId, User: user}, nil
|
||||
}
|
||||
|
||||
func fetchOrCreateUserBook(ac appcontext.AppContext, bookId uint, user *model.User) (model.UserBook, error) {
|
||||
var userbook model.UserBook
|
||||
res := ac.Db.Where("user_id = ? AND book_id = ?", user.ID, bookId).First(&userbook)
|
||||
|
||||
Reference in New Issue
Block a user