User book API: use a single query with result and count

This commit is contained in:
2026-01-25 00:42:50 +01:00
parent cd2b8a93bb
commit 8584e95ef7
15 changed files with 63 additions and 161 deletions

View File

@@ -13,6 +13,11 @@ import (
)
type bookUserGet struct {
Count int64 `json:"count"`
Books []bookUserGetBook `json:"books"`
}
type bookUserGetBook struct {
BookId uint `json:"id"`
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
@@ -21,14 +26,14 @@ type bookUserGet struct {
WantRead bool `json:"wantread"`
}
func testGetbooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, url string) []bookUserGet {
func testGetbooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, url string) bookUserGet {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", userToken))
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
var parsedResponse []bookUserGet
var parsedResponse bookUserGet
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
if err != nil {
t.Error(err)