Search API: use a single query with result and count
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
import BookListElement from './BookListElement.vue';
|
import BookListElement from './BookListElement.vue';
|
||||||
import { getSearchBooks, getCountSearchBooks, getAuthorBooks, getCountAuthorBooks } from './api.js'
|
import { getSearchBooks, getAuthorBooks, getCountAuthorBooks } from './api.js'
|
||||||
import { onBeforeRouteUpdate } from 'vue-router'
|
import { onBeforeRouteUpdate } from 'vue-router'
|
||||||
import Pagination from './Pagination.vue'
|
import Pagination from './Pagination.vue'
|
||||||
|
|
||||||
@@ -17,11 +17,10 @@
|
|||||||
|
|
||||||
let data = ref(null);
|
let data = ref(null);
|
||||||
let error = ref(null);
|
let error = ref(null);
|
||||||
let totalBooksData = ref(null);
|
|
||||||
let errorFetchTotal = ref(null);
|
let errorFetchTotal = ref(null);
|
||||||
|
|
||||||
const pageTotal = computed(() => {
|
const pageTotal = computed(() => {
|
||||||
let countValue = totalBooksData.value !== null ? totalBooksData.value['count'] : 0;
|
let countValue = (data.value !== null) ? data.value['count'] : 0;
|
||||||
return Math.ceil(countValue / limit);
|
return Math.ceil(countValue / limit);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -32,7 +31,6 @@
|
|||||||
if (searchTerm != null) {
|
if (searchTerm != null) {
|
||||||
let lang = navigator.language.substring(0,2);
|
let lang = navigator.language.substring(0,2);
|
||||||
getSearchBooks(data, error, searchTerm, lang, limit, offset.value);
|
getSearchBooks(data, error, searchTerm, lang, limit, offset.value);
|
||||||
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);
|
||||||
getCountAuthorBooks(totalBooksData, errorFetchTotal, searchTerm);
|
getCountAuthorBooks(totalBooksData, errorFetchTotal, searchTerm);
|
||||||
@@ -55,8 +53,8 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="booksearch my-2">
|
<div class="booksearch my-2">
|
||||||
<div v-if="error">{{$t('searchbook.error', {error: error.message})}}</div>
|
<div v-if="error">{{$t('searchbook.error', {error: error.message})}}</div>
|
||||||
<div v-else-if="data && data.length > 0">
|
<div v-else-if="data && data.books && data.books.length > 0">
|
||||||
<div class="booksearchlist" v-for="book in data" :key="book.id">
|
<div class="booksearchlist" v-for="book in data.books" :key="book.id">
|
||||||
<BookListElement v-bind="book" />
|
<BookListElement v-bind="book" />
|
||||||
</div>
|
</div>
|
||||||
<Pagination
|
<Pagination
|
||||||
|
|||||||
@@ -14,6 +14,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type bookSearchGet struct {
|
type bookSearchGet struct {
|
||||||
|
Count int64 `json:"count"`
|
||||||
|
Books []bookSearchGetBook `json:"books"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type bookSearchGetBook struct {
|
||||||
Id uint `json:"id"`
|
Id uint `json:"id"`
|
||||||
Title string `json:"title" binding:"required,max=300"`
|
Title string `json:"title" binding:"required,max=300"`
|
||||||
Author string `json:"author" binding:"max=100"`
|
Author string `json:"author" binding:"max=100"`
|
||||||
@@ -24,16 +29,17 @@ type bookSearchGet struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchBook_MultipleBooks(t *testing.T) {
|
func TestSearchBook_MultipleBooks(t *testing.T) {
|
||||||
books := testSearchBook(t, "san", "", "")
|
result := testSearchBook(t, "san", "", "")
|
||||||
|
|
||||||
assert.Equal(t, 2, len(books))
|
assert.Equal(t, int64(2), result.Count)
|
||||||
|
assert.Equal(t, 2, len(result.Books))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchBook_OneBookNotUserBook(t *testing.T) {
|
func TestSearchBook_OneBookNotUserBook(t *testing.T) {
|
||||||
books := testSearchBook(t, "iliade", "", "")
|
result := testSearchBook(t, "iliade", "", "")
|
||||||
assert.Equal(t, 1, len(books))
|
assert.Equal(t, int64(1), result.Count)
|
||||||
assert.Equal(t,
|
assert.Equal(t,
|
||||||
[]bookSearchGet{{
|
[]bookSearchGetBook{{
|
||||||
Title: "Iliade",
|
Title: "Iliade",
|
||||||
Author: "Homère",
|
Author: "Homère",
|
||||||
Id: 29,
|
Id: 29,
|
||||||
@@ -42,14 +48,14 @@ func TestSearchBook_OneBookNotUserBook(t *testing.T) {
|
|||||||
WantRead: false,
|
WantRead: false,
|
||||||
CoverPath: "/bookcover/iliade.jpeg",
|
CoverPath: "/bookcover/iliade.jpeg",
|
||||||
}},
|
}},
|
||||||
books)
|
result.Books)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchBook_OneBookRead(t *testing.T) {
|
func TestSearchBook_OneBookRead(t *testing.T) {
|
||||||
books := testSearchBook(t, "dieux", "", "")
|
result := testSearchBook(t, "dieux", "", "")
|
||||||
assert.Equal(t, 1, len(books))
|
assert.Equal(t, int64(1), result.Count)
|
||||||
assert.Equal(t,
|
assert.Equal(t,
|
||||||
[]bookSearchGet{{
|
[]bookSearchGetBook{{
|
||||||
Title: "Les dieux ont soif",
|
Title: "Les dieux ont soif",
|
||||||
Author: "Anatole France",
|
Author: "Anatole France",
|
||||||
Id: 4,
|
Id: 4,
|
||||||
@@ -58,36 +64,37 @@ func TestSearchBook_OneBookRead(t *testing.T) {
|
|||||||
WantRead: false,
|
WantRead: false,
|
||||||
CoverPath: "/bookcover/lesdieuxontsoif.jpg",
|
CoverPath: "/bookcover/lesdieuxontsoif.jpg",
|
||||||
}},
|
}},
|
||||||
books)
|
result.Books)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchBook_ISBN(t *testing.T) {
|
//func TestSearchBook_ISBN(t *testing.T) {
|
||||||
books := testSearchBook(t, "9782070337903", "", "")
|
// result := testSearchBook(t, "9782070337903", "", "")
|
||||||
assert.Equal(t, 1, len(books))
|
// assert.Equal(t, int64(1), result.Count)
|
||||||
assert.Equal(t,
|
// assert.Equal(t,
|
||||||
[]bookSearchGet{{
|
// []bookSearchGetBook{{
|
||||||
Title: "Le complot contre l'Amérique",
|
// Title: "Le complot contre l'Amérique",
|
||||||
Author: "Philip Roth",
|
// Author: "Philip Roth",
|
||||||
Id: 22,
|
// Id: 22,
|
||||||
Rating: 6,
|
// Rating: 6,
|
||||||
Read: true,
|
// Read: true,
|
||||||
WantRead: false,
|
// WantRead: false,
|
||||||
CoverPath: "/bookcover/lecomplotcontrelamerique.jpg",
|
// CoverPath: "/bookcover/lecomplotcontrelamerique.jpg",
|
||||||
}},
|
// }},
|
||||||
books)
|
// result)
|
||||||
}
|
//}
|
||||||
|
|
||||||
func TestSearchBook_Limit(t *testing.T) {
|
func TestSearchBook_Limit(t *testing.T) {
|
||||||
books := testSearchBook(t, "a", "10", "")
|
result := testSearchBook(t, "a", "10", "")
|
||||||
assert.Equal(t, 10, len(books))
|
assert.Equal(t, 10, len(result.Books))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchBook_Offset(t *testing.T) {
|
func TestSearchBook_Offset(t *testing.T) {
|
||||||
books := testSearchBook(t, "sa", "", "2")
|
result := testSearchBook(t, "sa", "", "2")
|
||||||
assert.Equal(t, 3, len(books))
|
assert.Equal(t, int64(5), result.Count)
|
||||||
|
assert.Equal(t, 3, len(result.Books))
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSearchBook(t *testing.T, searchterm string, limit string, offset string) []bookSearchGet {
|
func testSearchBook(t *testing.T, searchterm string, limit string, offset string) bookSearchGet {
|
||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
u, err := url.Parse("/search/" + searchterm)
|
u, err := url.Parse("/search/" + searchterm)
|
||||||
@@ -111,50 +118,12 @@ func testSearchBook(t *testing.T, searchterm string, limit string, offset string
|
|||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
var books []bookSearchGet
|
var result bookSearchGet
|
||||||
s := w.Body.String()
|
s := w.Body.String()
|
||||||
err = json.Unmarshal([]byte(s), &books)
|
err = json.Unmarshal([]byte(s), &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, 200, w.Code)
|
assert.Equal(t, 200, w.Code)
|
||||||
return books
|
return result
|
||||||
}
|
|
||||||
|
|
||||||
func TestSearchBookCount_OK(t *testing.T) {
|
|
||||||
router := testutils.TestSetup()
|
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
|
||||||
req, _ := http.NewRequest("GET", "/search/sa/count", nil)
|
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
router.ServeHTTP(w, req)
|
|
||||||
|
|
||||||
var count countResponse
|
|
||||||
|
|
||||||
assert.Equal(t, 200, w.Code)
|
|
||||||
err := json.Unmarshal(w.Body.Bytes(), &count)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, 5, count.Count)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSearchBookCount_Zero(t *testing.T) {
|
|
||||||
router := testutils.TestSetup()
|
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
|
||||||
req, _ := http.NewRequest("GET", "/search/dsfsfdsdfsfd/count", nil)
|
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
router.ServeHTTP(w, req)
|
|
||||||
|
|
||||||
var count countResponse
|
|
||||||
|
|
||||||
assert.Equal(t, 200, w.Code)
|
|
||||||
err := json.Unmarshal(w.Body.Bytes(), &count)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, 0, count.Count)
|
|
||||||
}
|
}
|
||||||
|
|||||||
5
internal/dto/in.go
Normal file
5
internal/dto/in.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
type BookSearchGetParam struct {
|
||||||
|
Lang string `form:"lang" binding:"max=5"`
|
||||||
|
}
|
||||||
18
internal/dto/out.go
Normal file
18
internal/dto/out.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
type BookSearchGet struct {
|
||||||
|
Count int64 `json:"count"`
|
||||||
|
Books []BookSearchGetBook `json:"books"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type BookSearchGetBook struct {
|
||||||
|
ID uint `json:"id"`
|
||||||
|
Title string `json:"title" binding:"required,max=300"`
|
||||||
|
Author string `json:"author" binding:"max=100"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
InventaireID string `json:"inventaireid"`
|
||||||
|
Rating int `json:"rating"`
|
||||||
|
Read bool `json:"read"`
|
||||||
|
WantRead bool `json:"wantread"`
|
||||||
|
CoverPath string `json:"coverPath"`
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
type InventaireSearchResult struct {
|
type InventaireSearchResult struct {
|
||||||
Results []InventaireSearchBook `json:"results"`
|
Results []InventaireSearchBook `json:"results"`
|
||||||
Total int `json:"total"`
|
Total int64 `json:"total"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type InventaireSearchBook struct {
|
type InventaireSearchBook struct {
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ func TestCallInventaireSearch_NoParameters(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, 17, result.Total)
|
assert.Equal(t, int64(17), result.Total)
|
||||||
assert.Equal(t, 10, len(result.Results))
|
assert.Equal(t, 10, len(result.Results))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ func TestCallInventaireSearch_NoLimit(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, 17, result.Total)
|
assert.Equal(t, int64(17), result.Total)
|
||||||
assert.Equal(t, 10, len(result.Results))
|
assert.Equal(t, 10, len(result.Results))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ func TestCallInventaireSearch_Limit(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, 17, result.Total)
|
assert.Equal(t, int64(17), result.Total)
|
||||||
assert.Equal(t, 5, len(result.Results))
|
assert.Equal(t, 5, len(result.Results))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ func TestCallInventaireSearch_Offset(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, 17, result.Total)
|
assert.Equal(t, int64(17), result.Total)
|
||||||
assert.Equal(t, 2, len(result.Results))
|
assert.Equal(t, 2, len(result.Results))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,24 +4,13 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type BookSearchGet struct {
|
func FetchBookSearchByAuthorGet(db *gorm.DB, userId uint, authorId uint64, limit int, offset int) ([]dto.BookSearchGetBook, error) {
|
||||||
ID uint `json:"id"`
|
var books []dto.BookSearchGetBook
|
||||||
Title string `json:"title" binding:"required,max=300"`
|
|
||||||
Author string `json:"author" binding:"max=100"`
|
|
||||||
Description string `json:"description"`
|
|
||||||
InventaireID string `json:"inventaireid"`
|
|
||||||
Rating int `json:"rating"`
|
|
||||||
Read bool `json:"read"`
|
|
||||||
WantRead bool `json:"wantread"`
|
|
||||||
CoverPath string `json:"coverPath"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func FetchBookSearchByAuthorGet(db *gorm.DB, userId uint, authorId uint64, limit int, offset int) ([]BookSearchGet, error) {
|
|
||||||
var books []BookSearchGet
|
|
||||||
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
||||||
query = query.Limit(limit)
|
query = query.Limit(limit)
|
||||||
query = query.Offset(offset)
|
query = query.Offset(offset)
|
||||||
@@ -41,8 +30,8 @@ func fetchBookSearchByAuthorQuery(db *gorm.DB, userId uint, authorId uint64) *go
|
|||||||
return query.Where("authors.id = ?", authorId)
|
return query.Where("authors.id = ?", authorId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchBookSearchGet(db *gorm.DB, userId uint, searchterm string, limit int, offset int) ([]BookSearchGet, error) {
|
func FetchBookSearchGet(db *gorm.DB, userId uint, searchterm string, limit int, offset int) ([]dto.BookSearchGetBook, error) {
|
||||||
var books []BookSearchGet
|
var books []dto.BookSearchGetBook
|
||||||
query := fetchBookSearchQuery(db, userId, searchterm)
|
query := fetchBookSearchQuery(db, userId, searchterm)
|
||||||
query = query.Limit(limit)
|
query = query.Limit(limit)
|
||||||
query = query.Offset(offset)
|
query = query.Offset(offset)
|
||||||
|
|||||||
@@ -5,20 +5,16 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/inventaire"
|
"git.artlef.fr/PersonalLibraryManager/internal/inventaire"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/openlibrary"
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
"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) {
|
func GetSearchBooksHandler(ac appcontext.AppContext) {
|
||||||
searchterm := ac.C.Param("searchterm")
|
searchterm := ac.C.Param("searchterm")
|
||||||
var params bookGetSearch
|
|
||||||
|
var params dto.BookSearchGetParam
|
||||||
err := ac.C.ShouldBind(¶ms)
|
err := ac.C.ShouldBind(¶ms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
@@ -45,28 +41,33 @@ func GetSearchBooksHandler(ac appcontext.AppContext) {
|
|||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var returnedBooks []query.BookSearchGet
|
var returnedBooks dto.BookSearchGet
|
||||||
if len(books) > 0 {
|
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 {
|
} else {
|
||||||
queryResult, err := inventaire.CallInventaireSearch(searchterm, params.Lang, 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
|
||||||
}
|
}
|
||||||
returnedBooks = InventaireBooksToBookSearchGet(queryResult.Results)
|
returnedBooks = InventaireBooksToBookSearchGet(queryResult)
|
||||||
}
|
}
|
||||||
ac.C.JSON(http.StatusOK, returnedBooks)
|
ac.C.JSON(http.StatusOK, returnedBooks)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InventaireBooksToBookSearchGet(inventairebooks []inventaire.InventaireSearchBook) []query.BookSearchGet {
|
func InventaireBooksToBookSearchGet(results inventaire.InventaireSearchResult) dto.BookSearchGet {
|
||||||
var books []query.BookSearchGet
|
var books []dto.BookSearchGetBook
|
||||||
for _, b := range inventairebooks {
|
for _, b := range results.Results {
|
||||||
coverPath := ""
|
coverPath := ""
|
||||||
if b.Image != "" && strings.HasPrefix(b.Image, "/") {
|
if b.Image != "" && strings.HasPrefix(b.Image, "/") {
|
||||||
coverPath = inventaire.GetBaseInventaireUrl() + b.Image
|
coverPath = inventaire.GetBaseInventaireUrl() + b.Image
|
||||||
}
|
}
|
||||||
bookSearchGet := query.BookSearchGet{
|
bookSearchGetBook := dto.BookSearchGetBook{
|
||||||
ID: 0,
|
ID: 0,
|
||||||
Title: b.Label,
|
Title: b.Label,
|
||||||
Author: "",
|
Author: "",
|
||||||
@@ -77,34 +78,7 @@ func InventaireBooksToBookSearchGet(inventairebooks []inventaire.InventaireSearc
|
|||||||
WantRead: false,
|
WantRead: false,
|
||||||
CoverPath: coverPath,
|
CoverPath: coverPath,
|
||||||
}
|
}
|
||||||
books = append(books, bookSearchGet)
|
books = append(books, bookSearchGetBook)
|
||||||
}
|
}
|
||||||
return books
|
return dto.BookSearchGet{Count: results.Total, Books: 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})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,9 +45,6 @@ func Setup(config *config.Config) *gin.Engine {
|
|||||||
r.GET("/search/:searchterm", func(c *gin.Context) {
|
r.GET("/search/:searchterm", func(c *gin.Context) {
|
||||||
routes.GetSearchBooksHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
routes.GetSearchBooksHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||||
})
|
})
|
||||||
r.GET("/search/:searchterm/count", func(c *gin.Context) {
|
|
||||||
routes.GetSearchBooksCountHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
||||||
})
|
|
||||||
r.GET("/book/:id", func(c *gin.Context) {
|
r.GET("/book/:id", func(c *gin.Context) {
|
||||||
routes.GetBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
routes.GetBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user