Search API: use a single query with result and count
This commit is contained in:
@@ -5,20 +5,16 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/inventaire"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/openlibrary"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type bookGetSearch struct {
|
||||
Lang string `form:"lang" binding:"max=5"`
|
||||
}
|
||||
|
||||
func GetSearchBooksHandler(ac appcontext.AppContext) {
|
||||
searchterm := ac.C.Param("searchterm")
|
||||
var params bookGetSearch
|
||||
|
||||
var params dto.BookSearchGetParam
|
||||
err := ac.C.ShouldBind(¶ms)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
@@ -45,28 +41,33 @@ func GetSearchBooksHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
var returnedBooks []query.BookSearchGet
|
||||
var returnedBooks dto.BookSearchGet
|
||||
if len(books) > 0 {
|
||||
returnedBooks = books
|
||||
count, err := query.FetchBookSearchGetCount(ac.Db, user.ID, searchterm)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
returnedBooks = dto.BookSearchGet{Count: count, Books: books}
|
||||
} else {
|
||||
queryResult, err := inventaire.CallInventaireSearch(searchterm, params.Lang, limit, offset)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
returnedBooks = InventaireBooksToBookSearchGet(queryResult.Results)
|
||||
returnedBooks = InventaireBooksToBookSearchGet(queryResult)
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, returnedBooks)
|
||||
}
|
||||
|
||||
func InventaireBooksToBookSearchGet(inventairebooks []inventaire.InventaireSearchBook) []query.BookSearchGet {
|
||||
var books []query.BookSearchGet
|
||||
for _, b := range inventairebooks {
|
||||
func InventaireBooksToBookSearchGet(results inventaire.InventaireSearchResult) dto.BookSearchGet {
|
||||
var books []dto.BookSearchGetBook
|
||||
for _, b := range results.Results {
|
||||
coverPath := ""
|
||||
if b.Image != "" && strings.HasPrefix(b.Image, "/") {
|
||||
coverPath = inventaire.GetBaseInventaireUrl() + b.Image
|
||||
}
|
||||
bookSearchGet := query.BookSearchGet{
|
||||
bookSearchGetBook := dto.BookSearchGetBook{
|
||||
ID: 0,
|
||||
Title: b.Label,
|
||||
Author: "",
|
||||
@@ -77,34 +78,7 @@ func InventaireBooksToBookSearchGet(inventairebooks []inventaire.InventaireSearc
|
||||
WantRead: false,
|
||||
CoverPath: coverPath,
|
||||
}
|
||||
books = append(books, bookSearchGet)
|
||||
books = append(books, bookSearchGetBook)
|
||||
}
|
||||
return books
|
||||
}
|
||||
|
||||
func GetSearchBooksCountHandler(ac appcontext.AppContext) {
|
||||
searchterm := ac.C.Param("searchterm")
|
||||
|
||||
user, fetchUserErr := ac.GetAuthenticatedUser()
|
||||
if fetchUserErr != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, fetchUserErr)
|
||||
return
|
||||
}
|
||||
count, err := query.FetchBookSearchGetCount(ac.Db, user.ID, searchterm)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
var finalCount int64
|
||||
if count > 0 {
|
||||
finalCount = count
|
||||
} else {
|
||||
queryResult, err := openlibrary.CallOpenLibrarySearch(searchterm, 0, 0)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
finalCount = int64(queryResult.NumFound)
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, gin.H{"count": finalCount})
|
||||
return dto.BookSearchGet{Count: results.Total, Books: books}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user