Added inventaire import from ISBN
This commit is contained in:
@@ -2,6 +2,7 @@ package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
@@ -51,17 +52,64 @@ func GetSearchBooksHandler(ac appcontext.AppContext) {
|
||||
returnedBooks = dto.BookSearchGet{Count: count, Inventaire: false, Books: books}
|
||||
}
|
||||
if params.Inventaire || len(returnedBooks.Books) == 0 {
|
||||
queryResult, err := inventaire.CallInventaireSearch(searchterm, params.Lang, limit, offset)
|
||||
returnedBooksPtr, err := searchInInventaireAPI(searchterm, limit, offset, params)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
returnedBooks = InventaireBooksToBookSearchGet(queryResult)
|
||||
returnedBooks = *returnedBooksPtr
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, returnedBooks)
|
||||
}
|
||||
|
||||
func InventaireBooksToBookSearchGet(results inventaire.InventaireSearchResult) dto.BookSearchGet {
|
||||
func searchInInventaireAPI(searchterm string, limit int, offset int, params dto.BookSearchGetParam) (*dto.BookSearchGet, error) {
|
||||
|
||||
isIsbn, err := regexp.Match(`\d{10,13}`, []byte(searchterm))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if isIsbn {
|
||||
queryResult, err := inventaire.CallInventaireFromISBN(searchterm, params.Lang)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var bookSearchGet dto.BookSearchGet
|
||||
if queryResult != nil {
|
||||
bookSearchGet = inventaireEditionToBookSearchGet(*queryResult)
|
||||
} else {
|
||||
bookSearchGet = dto.BookSearchGet{Count: 0, Inventaire: true}
|
||||
}
|
||||
return &bookSearchGet, err
|
||||
} else {
|
||||
queryResult, err := inventaire.CallInventaireSearch(searchterm, params.Lang, limit, offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bookSearchGet := inventaireBooksToBookSearchGet(queryResult)
|
||||
return &bookSearchGet, err
|
||||
}
|
||||
}
|
||||
|
||||
func inventaireEditionToBookSearchGet(result inventaire.InventaireEditionDetailedSingleResult) dto.BookSearchGet {
|
||||
var books []dto.BookSearchGetBook
|
||||
bookSearchGetBook := dto.BookSearchGetBook{
|
||||
ID: 0,
|
||||
Title: result.Title,
|
||||
Author: result.Author.Name,
|
||||
Description: result.Description,
|
||||
InventaireID: result.Id,
|
||||
IsInventaireEdition: true,
|
||||
Rating: 0,
|
||||
Read: false,
|
||||
WantRead: false,
|
||||
CoverPath: result.Image,
|
||||
}
|
||||
books = append(books, bookSearchGetBook)
|
||||
return dto.BookSearchGet{Count: 1, Inventaire: true, Books: books}
|
||||
}
|
||||
|
||||
func inventaireBooksToBookSearchGet(results inventaire.InventaireSearchResult) dto.BookSearchGet {
|
||||
var books []dto.BookSearchGetBook
|
||||
for _, b := range results.Results {
|
||||
coverPath := ""
|
||||
|
||||
Reference in New Issue
Block a user