Open Library search API
If nothing is found on the server, returns results from open library API. Clicking on a book imports it.
This commit is contained in:
@@ -59,7 +59,7 @@ func saveBookToDb(ac appcontext.AppContext, b bookPostCreate, user *model.User)
|
||||
|
||||
func fetchOrCreateAuthor(ac appcontext.AppContext, name string) (*model.Author, error) {
|
||||
var author model.Author
|
||||
res := ac.Db.Where("name = ?", name)
|
||||
res := ac.Db.Where("name = ?", name).First(&author)
|
||||
err := res.Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
||||
107
internal/routes/bookpostimport.go
Normal file
107
internal/routes/bookpostimport.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"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"
|
||||
)
|
||||
|
||||
type bookPostImport struct {
|
||||
OpenLibraryId string `json:"openlibraryid" binding:"required,max=50"`
|
||||
}
|
||||
|
||||
func PostImportBookHandler(ac appcontext.AppContext) {
|
||||
var request bookPostImport
|
||||
err := ac.C.ShouldBindJSON(&request)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
|
||||
user, fetchUserErr := ac.GetAuthenticatedUser()
|
||||
if fetchUserErr != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
openLibraryBook, err := openlibrary.CallOpenLibraryBook(request.OpenLibraryId)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
book, err := saveOpenLibraryBookToDb(ac, request.OpenLibraryId, openLibraryBook, &user)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
book := model.Book{
|
||||
Title: openLibraryBook.Title,
|
||||
Summary: openLibraryBook.Description,
|
||||
OpenLibraryId: openLibraryId,
|
||||
Author: *author,
|
||||
AddedBy: *user,
|
||||
}
|
||||
err = ac.Db.Save(&book).Error
|
||||
return &book, err
|
||||
}
|
||||
|
||||
func fetchOrCreateOpenLibraryAuthor(ac appcontext.AppContext, openlibraryAuthorId string) (*model.Author, error) {
|
||||
var author model.Author
|
||||
res := ac.Db.Where("open_library_id = ?", openlibraryAuthorId).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
|
||||
}
|
||||
return newAuthor, nil
|
||||
} else {
|
||||
return &author, err
|
||||
}
|
||||
} else {
|
||||
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
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func GetSearchBooksHandler(ac appcontext.AppContext) {
|
||||
if len(books) > 0 {
|
||||
returnedBooks = books
|
||||
} else {
|
||||
queryResult, err := openlibrary.CallOpenLibrary(searchterm, limit, offset)
|
||||
queryResult, err := openlibrary.CallOpenLibrarySearch(searchterm, limit, offset)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
@@ -48,7 +48,7 @@ func GetSearchBooksHandler(ac appcontext.AppContext) {
|
||||
ac.C.JSON(http.StatusOK, returnedBooks)
|
||||
}
|
||||
|
||||
func OpenLibraryBooksToBookSearchGet(OLbooks []openlibrary.OpenLibraryBook) []query.BookSearchGet {
|
||||
func OpenLibraryBooksToBookSearchGet(OLbooks []openlibrary.OpenLibrarySearchBook) []query.BookSearchGet {
|
||||
var books []query.BookSearchGet
|
||||
for _, b := range OLbooks {
|
||||
bookSearchGet := query.BookSearchGet{
|
||||
@@ -83,7 +83,7 @@ func GetSearchBooksCountHandler(ac appcontext.AppContext) {
|
||||
if count > 0 {
|
||||
finalCount = count
|
||||
} else {
|
||||
queryResult, err := openlibrary.CallOpenLibrary(searchterm, 0, 0)
|
||||
queryResult, err := openlibrary.CallOpenLibrarySearch(searchterm, 0, 0)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user