Download cover when importing from inventaire
This commit is contained in:
@@ -5,10 +5,10 @@ import (
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/fileutils"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/inventaire"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/openlibrary"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -51,6 +51,14 @@ func saveInventaireBookToDb(ac appcontext.AppContext, inventaireEdition inventai
|
||||
Author: *author,
|
||||
AddedBy: *user,
|
||||
}
|
||||
|
||||
if inventaireEdition.Image != "" {
|
||||
cover, err := fileutils.DownloadFile(ac, inventaireEdition.Image)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
book.Cover = cover
|
||||
}
|
||||
err = ac.Db.Save(&book).Error
|
||||
return &book, err
|
||||
}
|
||||
@@ -74,33 +82,3 @@ func fetchOrCreateInventaireAuthor(ac appcontext.AppContext, inventaireAuthor *i
|
||||
return &author, nil
|
||||
}
|
||||
}
|
||||
|
||||
func createAuthorFromOpenLibrary(ac appcontext.AppContext, openlibraryAuthorId string) (*model.Author, error) {
|
||||
authorFromOL, err := openlibrary.CallOpenLibraryAuthor(openlibraryAuthorId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var author model.Author
|
||||
res := ac.Db.Where("name = ?", authorFromOL.Name).First(&author)
|
||||
err = res.Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
author = model.Author{
|
||||
Name: authorFromOL.Name,
|
||||
Description: authorFromOL.Description,
|
||||
OpenLibraryId: openlibraryAuthorId,
|
||||
}
|
||||
err = ac.Db.Save(&author).Error
|
||||
return &author, err
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
//if the author already exists, only fill the open library id
|
||||
author.OpenLibraryId = openlibraryAuthorId
|
||||
ac.Db.Save(&author)
|
||||
}
|
||||
|
||||
return &author, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user