Add pagination for booksearch
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
||||
@@ -19,33 +20,96 @@ type bookSearchGet struct {
|
||||
}
|
||||
|
||||
func TestSearchBook_MultipleBooks(t *testing.T) {
|
||||
books := testSearchBook(t, "san")
|
||||
books := testSearchBook(t, "san", "", "")
|
||||
|
||||
assert.Equal(t, 2, len(books))
|
||||
}
|
||||
|
||||
func TestSearchBook_AllFields(t *testing.T) {
|
||||
books := testSearchBook(t, "de san")
|
||||
books := testSearchBook(t, "de san", "", "")
|
||||
assert.Equal(t, 1, len(books))
|
||||
assert.Equal(t,
|
||||
[]bookSearchGet{{Title: "De sang-froid", Author: "Truman Capote", Id: 18}},
|
||||
books)
|
||||
}
|
||||
|
||||
func testSearchBook(t *testing.T, searchterm string) []bookSearchGet {
|
||||
func TestSearchBook_Limit(t *testing.T) {
|
||||
books := testSearchBook(t, "a", "10", "")
|
||||
assert.Equal(t, 10, len(books))
|
||||
}
|
||||
|
||||
func TestSearchBook_Offset(t *testing.T) {
|
||||
books := testSearchBook(t, "sa", "", "2")
|
||||
assert.Equal(t, 3, len(books))
|
||||
}
|
||||
|
||||
func testSearchBook(t *testing.T, searchterm string, limit string, offset string) []bookSearchGet {
|
||||
router := testutils.TestSetup()
|
||||
|
||||
u, err := url.Parse("/search/" + searchterm)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if limit != "" {
|
||||
q := u.Query()
|
||||
q.Set("limit", limit)
|
||||
u.RawQuery = q.Encode()
|
||||
}
|
||||
if offset != "" {
|
||||
q := u.Query()
|
||||
q.Set("offset", offset)
|
||||
u.RawQuery = q.Encode()
|
||||
}
|
||||
|
||||
token := testutils.ConnectDemoUser(router)
|
||||
req, _ := http.NewRequest("GET", "/search/"+searchterm, nil)
|
||||
req, _ := http.NewRequest("GET", u.String(), nil)
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
var books []bookSearchGet
|
||||
err := json.Unmarshal(w.Body.Bytes(), &books)
|
||||
err = json.Unmarshal(w.Body.Bytes(), &books)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, 200, w.Code)
|
||||
return books
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -36,14 +36,28 @@ type BookSearchGet struct {
|
||||
CoverPath string `json:"coverPath"`
|
||||
}
|
||||
|
||||
func FetchBookSearchGet(db *gorm.DB, searchterm string) ([]BookSearchGet, error) {
|
||||
func FetchBookSearchGet(db *gorm.DB, searchterm string, limit int, offset int) ([]BookSearchGet, error) {
|
||||
var books []BookSearchGet
|
||||
query := fetchBookSearchQuery(db, searchterm)
|
||||
query = query.Limit(limit)
|
||||
query = query.Offset(offset)
|
||||
res := query.Find(&books)
|
||||
return books, res.Error
|
||||
}
|
||||
|
||||
func FetchBookSearchGetCount(db *gorm.DB, searchterm string) (int64, error) {
|
||||
query := fetchBookSearchQuery(db, searchterm)
|
||||
var count int64
|
||||
res := query.Count(&count)
|
||||
return count, res.Error
|
||||
}
|
||||
|
||||
func fetchBookSearchQuery(db *gorm.DB, searchterm string) *gorm.DB {
|
||||
query := db.Model(&model.Book{})
|
||||
query = query.Select("books.title, books.author, books.id," + selectStaticFilesPath())
|
||||
query = query.Joins("left join static_files on (static_files.id = books.cover_id)")
|
||||
query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(searchterm)+"%")
|
||||
res := query.Find(&books)
|
||||
return books, res.Error
|
||||
return query
|
||||
}
|
||||
|
||||
type BookUserGet struct {
|
||||
|
||||
@@ -6,14 +6,36 @@ import (
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetSearchBooksHandler(ac appcontext.AppContext) {
|
||||
searchterm := ac.C.Param("searchterm")
|
||||
books, err := query.FetchBookSearchGet(ac.Db, searchterm)
|
||||
|
||||
limit, err := ac.GetQueryLimit()
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
offset, err := ac.GetQueryOffset()
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
books, err := query.FetchBookSearchGet(ac.Db, searchterm, limit, offset)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, books)
|
||||
}
|
||||
|
||||
func GetSearchBooksCountHandler(ac appcontext.AppContext) {
|
||||
searchterm := ac.C.Param("searchterm")
|
||||
count, err := query.FetchBookSearchGetCount(ac.Db, searchterm)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, gin.H{"count": count})
|
||||
}
|
||||
|
||||
@@ -39,6 +39,9 @@ func Setup(config *config.Config) *gin.Engine {
|
||||
r.GET("/search/:searchterm", func(c *gin.Context) {
|
||||
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) {
|
||||
routes.GetBookHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user