Add language to inventaire search book

This commit is contained in:
2026-01-22 13:37:42 +01:00
parent 8d3569e5d6
commit 7921a83f30
6 changed files with 33 additions and 9 deletions

View File

@@ -30,7 +30,8 @@
function fetchData(searchTerm, authorId) { function fetchData(searchTerm, authorId) {
if (searchTerm != null) { if (searchTerm != null) {
getSearchBooks(data, error, searchTerm, limit, offset.value); let lang = navigator.language.substring(0,2);
getSearchBooks(data, error, searchTerm, lang, limit, offset.value);
getCountSearchBooks(totalBooksData, errorFetchTotal, searchTerm); getCountSearchBooks(totalBooksData, errorFetchTotal, searchTerm);
} else if (authorId != null) { } else if (authorId != null) {
getAuthorBooks(data, error, authorId, limit, offset.value); getAuthorBooks(data, error, authorId, limit, offset.value);

View File

@@ -41,8 +41,8 @@ export function getCountSearchBooks(data, error, searchterm) {
return useFetch(data, error,baseUrl + '/search/' + encodeURIComponent(searchterm) + '/count') return useFetch(data, error,baseUrl + '/search/' + encodeURIComponent(searchterm) + '/count')
} }
export function getSearchBooks(data, error, searchterm, limit, offset) { export function getSearchBooks(data, error, searchterm, lang, limit, offset) {
const queryParams = new URLSearchParams({limit: limit, offset: offset}); const queryParams = new URLSearchParams({lang: lang, limit: limit, offset: offset});
return useFetch(data, error, baseUrl + '/search/' + encodeURIComponent(searchterm) + "?" + queryParams.toString()); return useFetch(data, error, baseUrl + '/search/' + encodeURIComponent(searchterm) + "?" + queryParams.toString());
} }

View File

@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
@@ -21,6 +22,7 @@ func AddQueryParam(u *url.URL, paramName string, paramValue string) {
func FetchAndParseResult[T any](u *url.URL, queryResult *T) error { func FetchAndParseResult[T any](u *url.URL, queryResult *T) error {
resp, err := DoApiQuery(u) resp, err := DoApiQuery(u)
log.Printf("Calling GET %s", u.String())
if err != nil { if err != nil {
return err return err
} }

View File

@@ -27,12 +27,15 @@ func computeInventaireApiUrl(paths ...string) (*url.URL, error) {
return callapiutils.ComputeUrl(baseUrl, paths...) return callapiutils.ComputeUrl(baseUrl, paths...)
} }
func CallInventaireSearch(searchterm string, limit int, offset int) (InventaireSearchResult, error) { func CallInventaireSearch(searchterm string, lang string, limit int, offset int) (InventaireSearchResult, error) {
var queryResult InventaireSearchResult var queryResult InventaireSearchResult
u, err := computeInventaireApiUrl("search") u, err := computeInventaireApiUrl("search")
if err != nil { if err != nil {
return queryResult, err return queryResult, err
} }
if lang != "" {
callapiutils.AddQueryParam(u, "lang", lang)
}
if limit != 0 { if limit != 0 {
callapiutils.AddQueryParamInt(u, "limit", limit) callapiutils.AddQueryParamInt(u, "limit", limit)
} }

View File

@@ -6,8 +6,17 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestCallInventaireSearch_NoParameters(t *testing.T) {
result, err := CallInventaireSearch("salammbo", "", 0, 0)
if err != nil {
t.Error(err)
}
assert.Equal(t, 17, result.Total)
assert.Equal(t, 10, len(result.Results))
}
func TestCallInventaireSearch_NoLimit(t *testing.T) { func TestCallInventaireSearch_NoLimit(t *testing.T) {
result, err := CallInventaireSearch("salammbo", 0, 0) result, err := CallInventaireSearch("salammbo", "fr", 0, 0)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -16,7 +25,7 @@ func TestCallInventaireSearch_NoLimit(t *testing.T) {
} }
func TestCallInventaireSearch_Limit(t *testing.T) { func TestCallInventaireSearch_Limit(t *testing.T) {
result, err := CallInventaireSearch("salammbo", 5, 0) result, err := CallInventaireSearch("salammbo", "fr", 5, 0)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@@ -25,7 +34,7 @@ func TestCallInventaireSearch_Limit(t *testing.T) {
} }
func TestCallInventaireSearch_Offset(t *testing.T) { func TestCallInventaireSearch_Offset(t *testing.T) {
result, err := CallInventaireSearch("salammbo", 0, 15) result, err := CallInventaireSearch("salammbo", "fr", 0, 15)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }

View File

@@ -12,9 +12,18 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
type bookGetSearch struct {
Lang string `form:"lang" binding:"max=5"`
}
func GetSearchBooksHandler(ac appcontext.AppContext) { func GetSearchBooksHandler(ac appcontext.AppContext) {
searchterm := ac.C.Param("searchterm") searchterm := ac.C.Param("searchterm")
var params bookGetSearch
err := ac.C.ShouldBind(&params)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
user, fetchUserErr := ac.GetAuthenticatedUser() user, fetchUserErr := ac.GetAuthenticatedUser()
if fetchUserErr != nil { if fetchUserErr != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, fetchUserErr) myvalidator.ReturnErrorsAsJsonResponse(&ac, fetchUserErr)
@@ -40,7 +49,7 @@ func GetSearchBooksHandler(ac appcontext.AppContext) {
if len(books) > 0 { if len(books) > 0 {
returnedBooks = books returnedBooks = books
} else { } else {
queryResult, err := inventaire.CallInventaireSearch(searchterm, limit, offset) queryResult, err := inventaire.CallInventaireSearch(searchterm, params.Lang, limit, offset)
if err != nil { if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err) myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return return