55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
|
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func GetBookHandler(ac appcontext.AppContext) {
|
|
bookId, err := strconv.ParseUint(ac.C.Param("id"), 10, 64)
|
|
if err != nil {
|
|
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err})
|
|
return
|
|
}
|
|
user, fetchUserErr := ac.GetAuthenticatedUser()
|
|
if fetchUserErr != nil {
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, fetchUserErr)
|
|
return
|
|
}
|
|
err = myvalidator.ValidateId(ac.Db, uint(bookId), &model.Book{})
|
|
if err != nil {
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
return
|
|
}
|
|
book, queryErr := query.FetchBookGet(ac.Db, user.ID, bookId)
|
|
if queryErr != nil {
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
return
|
|
}
|
|
err = fixBookGetDateFields(&book)
|
|
if err != nil {
|
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
return
|
|
}
|
|
ac.C.JSON(http.StatusOK, book)
|
|
}
|
|
|
|
func fixBookGetDateFields(book *query.BookGet) error {
|
|
if book.StartReadDate == "" {
|
|
return nil
|
|
}
|
|
startDate, err := time.Parse(time.RFC3339, book.StartReadDate)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
book.StartReadDate = startDate.Format(time.DateOnly)
|
|
return nil
|
|
}
|