Book form: can now edit an existing book
This commit is contained in:
@@ -4,26 +4,38 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/adapter"
|
||||
"git.artlef.fr/bibliomane/internal/appcontext"
|
||||
"git.artlef.fr/bibliomane/internal/dto"
|
||||
"git.artlef.fr/bibliomane/internal/i18nresource"
|
||||
"git.artlef.fr/bibliomane/internal/model"
|
||||
"git.artlef.fr/bibliomane/internal/myvalidator"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func PostBookHandler(ac appcontext.AppContext) {
|
||||
var book dto.BookPostCreate
|
||||
var book dto.BookFields
|
||||
err := ac.C.ShouldBindJSON(&book)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
err = myvalidator.ValidateId(ac.Db, book.CoverID, &model.StaticFile{})
|
||||
if err != nil {
|
||||
//when creating a book, title is required
|
||||
if book.Title == nil {
|
||||
err = myvalidator.HttpError{
|
||||
StatusCode: http.StatusBadRequest,
|
||||
Err: errors.New(i18nresource.GetTranslatedMessage(&ac, "ValidationRequired")),
|
||||
}
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
if book.CoverID != nil {
|
||||
err = myvalidator.ValidateId(ac.Db, *book.CoverID, &model.StaticFile{})
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
user, fetchUserErr := ac.GetAuthenticatedUser()
|
||||
if fetchUserErr != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
@@ -38,44 +50,15 @@ func PostBookHandler(ac appcontext.AppContext) {
|
||||
ac.C.JSON(http.StatusOK, gin.H{"id": id})
|
||||
}
|
||||
|
||||
func saveBookToDb(ac appcontext.AppContext, b dto.BookPostCreate, user *model.User) (uint, error) {
|
||||
author, err := fetchOrCreateAuthor(ac, b.Author)
|
||||
func saveBookToDb(ac appcontext.AppContext, b dto.BookFields, user *model.User) (uint, error) {
|
||||
book := model.Book{
|
||||
AddedBy: *user,
|
||||
}
|
||||
err := adapter.FillBookDbFromFields(ac, &b, &book)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
book := model.Book{
|
||||
Title: b.Title,
|
||||
AuthorID: author.ID,
|
||||
ISBN: b.ISBN,
|
||||
InventaireID: b.InventaireID,
|
||||
OpenLibraryId: b.OpenLibraryId,
|
||||
SmallDescription: b.ShortDescription,
|
||||
Summary: b.Summary,
|
||||
AddedBy: *user,
|
||||
}
|
||||
if b.CoverID > 0 {
|
||||
book.CoverID = b.CoverID
|
||||
}
|
||||
|
||||
err = ac.Db.Save(&book).Error
|
||||
return book.ID, err
|
||||
}
|
||||
|
||||
func fetchOrCreateAuthor(ac appcontext.AppContext, name string) (*model.Author, error) {
|
||||
var author model.Author
|
||||
res := ac.Db.Where("name = ?", name).First(&author)
|
||||
err := res.Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
author = model.Author{Name: name}
|
||||
err = ac.Db.Save(&author).Error
|
||||
if err != nil {
|
||||
return &author, err
|
||||
}
|
||||
return &author, nil
|
||||
} else {
|
||||
return &author, err
|
||||
}
|
||||
} else {
|
||||
return &author, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user