Collection: new widget to add book to collection

This commit is contained in:
2026-04-04 23:15:44 +02:00
parent c7abbfe4d4
commit 2552ba8e94
11 changed files with 279 additions and 16 deletions

View File

@@ -38,7 +38,7 @@ func GetSearchBooksHandler(ac appcontext.AppContext) {
return
}
var returnedBooks dto.BookItemsGet
if !params.Inventaire {
if params.Inventaire != dto.ForceInventaireSearch {
books, err := query.FetchBookSearchGet(ac.Db, user.ID, searchterm, limit, offset)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
@@ -51,7 +51,7 @@ func GetSearchBooksHandler(ac appcontext.AppContext) {
}
returnedBooks = dto.BookItemsGet{Count: count, Inventaire: false, Books: books}
}
if params.Inventaire || len(returnedBooks.Books) == 0 {
if (params.Inventaire == dto.InventaireIfNothingFound && len(returnedBooks.Books) == 0) || (params.Inventaire == dto.ForceInventaireSearch) {
returnedBooksPtr, err := searchInInventaireAPI(ac.Config.InventaireUrl, searchterm, limit, offset, params)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)

View File

@@ -0,0 +1,62 @@
package routes
import (
"errors"
"net/http"
"strconv"
"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"
)
func PostCollectionBookHandler(ac appcontext.AppContext) {
collectionId, err := strconv.ParseUint(ac.C.Param("id"), 10, 64)
if err != nil {
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err})
return
}
user, err := ac.GetAuthenticatedUser()
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
var collection model.Collection
err = ac.Db.First(&collection, collectionId).Error
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
if collection.UserID != user.ID {
err := myvalidator.HttpError{
StatusCode: http.StatusUnauthorized,
Err: errors.New(i18nresource.GetTranslatedMessage(&ac, "Unauthorized")),
}
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
var collectionBook dto.CollectionBook
err = ac.C.ShouldBindJSON(&collectionBook)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
var book model.Book
err = ac.Db.First(&book, collectionBook.BookID).Error
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
collection.Books = append(collection.Books, book)
ac.Db.Save(&collection)
ac.C.String(http.StatusOK, "Success")
}