Switch from open library API to Inventaire API

This commit is contained in:
2026-01-16 16:12:59 +01:00
parent 1bb841332c
commit a04aff6056
22 changed files with 338 additions and 108 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/inventaire"
"git.artlef.fr/PersonalLibraryManager/internal/model"
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
"git.artlef.fr/PersonalLibraryManager/internal/openlibrary"
@@ -12,7 +13,8 @@ import (
)
type bookPostImport struct {
OpenLibraryId string `json:"openlibraryid" binding:"required,max=50"`
InventaireID string `json:"inventaireid" binding:"required,max=50"`
Lang string `json:"lang" binding:"required,max=5"`
}
func PostImportBookHandler(ac appcontext.AppContext) {
@@ -28,12 +30,12 @@ func PostImportBookHandler(ac appcontext.AppContext) {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
openLibraryBook, err := openlibrary.CallOpenLibraryBook(request.OpenLibraryId)
inventaireBook, err := inventaire.CallInventaireBook(request.InventaireID, request.Lang)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
book, err := saveOpenLibraryBookToDb(ac, request.OpenLibraryId, openLibraryBook, &user)
book, err := saveInventaireBookToDb(ac, inventaireBook, &user)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
@@ -41,33 +43,34 @@ func PostImportBookHandler(ac appcontext.AppContext) {
ac.C.JSON(200, gin.H{"id": book.ID})
}
func saveOpenLibraryBookToDb(ac appcontext.AppContext, openLibraryId string, openLibraryBook openlibrary.OpenLibraryBookResult, user *model.User) (*model.Book, error) {
author, err := fetchOrCreateOpenLibraryAuthor(ac, openLibraryBook.AuthorID)
func saveInventaireBookToDb(ac appcontext.AppContext, inventaireBook inventaire.InventaireBookResult, user *model.User) (*model.Book, error) {
author, err := fetchOrCreateInventaireAuthor(ac, inventaireBook.Author)
if err != nil {
return nil, err
}
book := model.Book{
Title: openLibraryBook.Title,
Summary: openLibraryBook.Description,
OpenLibraryId: openLibraryId,
Author: *author,
AddedBy: *user,
Title: inventaireBook.Title,
Summary: inventaireBook.Description,
InventaireID: inventaireBook.ID,
Author: *author,
AddedBy: *user,
}
err = ac.Db.Save(&book).Error
return &book, err
}
func fetchOrCreateOpenLibraryAuthor(ac appcontext.AppContext, openlibraryAuthorId string) (*model.Author, error) {
func fetchOrCreateInventaireAuthor(ac appcontext.AppContext, inventaireAuthor *inventaire.InventaireAuthorResult) (*model.Author, error) {
var author model.Author
res := ac.Db.Where("open_library_id = ?", openlibraryAuthorId).First(&author)
res := ac.Db.Where("inventaire_id = ?", inventaireAuthor.ID).First(&author)
err := res.Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
newAuthor, err := createAuthorFromOpenLibrary(ac, openlibraryAuthorId)
if err != nil {
return &author, err
newAuthor := model.Author{
Name: inventaireAuthor.Name,
Description: inventaireAuthor.Description,
InventaireID: inventaireAuthor.ID,
}
return newAuthor, nil
return &newAuthor, nil
} else {
return &author, err
}