Add existing book fields in "create book" form
This commit is contained in:
@@ -2,11 +2,13 @@ package routes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"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"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -28,28 +30,34 @@ func PostBookHandler(ac appcontext.AppContext) {
|
||||
return
|
||||
}
|
||||
|
||||
err = saveBookToDb(ac, book, &user)
|
||||
id, err := saveBookToDb(ac, book, &user)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.String(200, "Success")
|
||||
ac.C.JSON(http.StatusOK, gin.H{"id": id})
|
||||
}
|
||||
|
||||
func saveBookToDb(ac appcontext.AppContext, b dto.BookPostCreate, user *model.User) error {
|
||||
func saveBookToDb(ac appcontext.AppContext, b dto.BookPostCreate, user *model.User) (uint, error) {
|
||||
author, err := fetchOrCreateAuthor(ac, b.Author)
|
||||
if err != nil {
|
||||
return err
|
||||
return 0, err
|
||||
}
|
||||
book := model.Book{
|
||||
Title: b.Title,
|
||||
AuthorID: author.ID,
|
||||
AddedBy: *user,
|
||||
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
|
||||
}
|
||||
return ac.Db.Save(&book).Error
|
||||
err = ac.Db.Save(&book).Error
|
||||
return book.ID, err
|
||||
}
|
||||
|
||||
func fetchOrCreateAuthor(ac appcontext.AppContext, name string) (*model.Author, error) {
|
||||
|
||||
Reference in New Issue
Block a user