Add open library API call when search has no result
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/openlibrary"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -33,7 +34,36 @@ func GetSearchBooksHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, books)
|
||||
var returnedBooks []query.BookSearchGet
|
||||
if len(books) > 0 {
|
||||
returnedBooks = books
|
||||
} else {
|
||||
queryResult, err := openlibrary.CallOpenLibrary(searchterm, limit, offset)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
returnedBooks = OpenLibraryBooksToBookSearchGet(queryResult.Books)
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, returnedBooks)
|
||||
}
|
||||
|
||||
func OpenLibraryBooksToBookSearchGet(OLbooks []openlibrary.OpenLibraryBook) []query.BookSearchGet {
|
||||
var books []query.BookSearchGet
|
||||
for _, b := range OLbooks {
|
||||
bookSearchGet := query.BookSearchGet{
|
||||
ID: 0,
|
||||
Title: b.Title,
|
||||
Author: b.Authors[0],
|
||||
OpenLibraryId: b.OpenLibraryId,
|
||||
Rating: 0,
|
||||
Read: false,
|
||||
WantRead: false,
|
||||
CoverPath: "",
|
||||
}
|
||||
books = append(books, bookSearchGet)
|
||||
}
|
||||
return books
|
||||
}
|
||||
|
||||
func GetSearchBooksCountHandler(ac appcontext.AppContext) {
|
||||
@@ -49,5 +79,16 @@ func GetSearchBooksCountHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, gin.H{"count": count})
|
||||
var finalCount int64
|
||||
if count > 0 {
|
||||
finalCount = count
|
||||
} else {
|
||||
queryResult, err := openlibrary.CallOpenLibrary(searchterm, 0, 0)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
finalCount = int64(queryResult.NumFound)
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, gin.H{"count": finalCount})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user