Author books API: use a single query with result and count
This commit is contained in:
@@ -17,7 +17,6 @@
|
|||||||
|
|
||||||
let data = ref(null);
|
let data = ref(null);
|
||||||
let error = ref(null);
|
let error = ref(null);
|
||||||
let errorFetchTotal = ref(null);
|
|
||||||
|
|
||||||
const pageTotal = computed(() => {
|
const pageTotal = computed(() => {
|
||||||
let countValue = (data.value !== null) ? data.value['count'] : 0;
|
let countValue = (data.value !== null) ? data.value['count'] : 0;
|
||||||
@@ -33,7 +32,6 @@
|
|||||||
getSearchBooks(data, error, searchTerm, lang, limit, offset.value);
|
getSearchBooks(data, error, searchTerm, lang, limit, offset.value);
|
||||||
} 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,10 +51,6 @@ export function getAuthorBooks(data, error, id, limit, offset) {
|
|||||||
return useFetch(data, error, baseUrl + '/author/' + id + "/books" + "?" + queryParams.toString());
|
return useFetch(data, error, baseUrl + '/author/' + id + "/books" + "?" + queryParams.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCountAuthorBooks(data, error, id) {
|
|
||||||
return useFetch(data, error, baseUrl + '/author/' + id + "/books" + '/count');
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getBook(data, error, id) {
|
export function getBook(data, error, id) {
|
||||||
return useFetch(data, error, baseUrl + '/book/' + id);
|
return useFetch(data, error, baseUrl + '/book/' + id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,11 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type bookAuthorGetResult struct {
|
||||||
|
Count int64 `json:"count"`
|
||||||
|
Books []bookAuthorGet `json:"books"`
|
||||||
|
}
|
||||||
|
|
||||||
type bookAuthorGet struct {
|
type bookAuthorGet 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"`
|
||||||
@@ -22,21 +27,24 @@ type bookAuthorGet struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchBookPerAuthor_Ok(t *testing.T) {
|
func TestSearchBookPerAuthor_Ok(t *testing.T) {
|
||||||
books := testFetchBookAuthor(t, 1, "", "")
|
result := testFetchBookAuthor(t, 1, "", "")
|
||||||
assert.Equal(t, 3, len(books))
|
assert.Equal(t, 3, len(result.Books))
|
||||||
|
assert.Equal(t, int64(3), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchBookPerAuthor_Limit(t *testing.T) {
|
func TestSearchBookPerAuthor_Limit(t *testing.T) {
|
||||||
books := testFetchBookAuthor(t, 1, "2", "")
|
result := testFetchBookAuthor(t, 1, "2", "")
|
||||||
assert.Equal(t, 2, len(books))
|
assert.Equal(t, 2, len(result.Books))
|
||||||
|
assert.Equal(t, int64(2), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSearchBookPerAuthor_Offset(t *testing.T) {
|
func TestSearchBookPerAuthor_Offset(t *testing.T) {
|
||||||
books := testFetchBookAuthor(t, 1, "10", "2")
|
result := testFetchBookAuthor(t, 1, "10", "2")
|
||||||
assert.Equal(t, 1, len(books))
|
assert.Equal(t, 1, len(result.Books))
|
||||||
|
assert.Equal(t, int64(1), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testFetchBookAuthor(t *testing.T, authorId uint, limit string, offset string) []bookAuthorGet {
|
func testFetchBookAuthor(t *testing.T, authorId uint, limit string, offset string) bookAuthorGetResult {
|
||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
u, err := url.Parse(fmt.Sprintf("/author/%d/books", authorId))
|
u, err := url.Parse(fmt.Sprintf("/author/%d/books", authorId))
|
||||||
@@ -60,12 +68,12 @@ func testFetchBookAuthor(t *testing.T, authorId uint, limit string, offset strin
|
|||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
var books []bookAuthorGet
|
var result bookAuthorGetResult
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,22 +71,3 @@ func TestGetReadingBooksCountHandler_Demo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
assert.Equal(t, 2, c.Count)
|
assert.Equal(t, 2, c.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetAuthorsBooksCountHandler_Demo(t *testing.T) {
|
|
||||||
router := testutils.TestSetup()
|
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", "/author/1/books/count", nil)
|
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
router.ServeHTTP(w, req)
|
|
||||||
|
|
||||||
var c countResponse
|
|
||||||
err := json.Unmarshal(w.Body.Bytes(), &c)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
assert.Equal(t, 3, c.Count)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||||
@@ -44,30 +45,11 @@ func GetAuthorBooksHandler(ac appcontext.AppContext) {
|
|||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ac.C.JSON(http.StatusOK, books)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAuthorBooksCountHandler(ac appcontext.AppContext) {
|
|
||||||
authorId, err := strconv.ParseUint(ac.C.Param("id"), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
ac.C.JSON(http.StatusBadRequest, gin.H{"error": err})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
err = myvalidator.ValidateId(ac.Db, uint(authorId), &model.Author{})
|
|
||||||
if err != nil {
|
|
||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
user, fetchUserErr := ac.GetAuthenticatedUser()
|
|
||||||
if fetchUserErr != nil {
|
|
||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, fetchUserErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
count, err := query.FetchBookSearchByAuthorGetCount(ac.Db, user.ID, authorId)
|
count, err := query.FetchBookSearchByAuthorGetCount(ac.Db, user.ID, authorId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ac.C.JSON(http.StatusOK, gin.H{"count": count})
|
ac.C.JSON(http.StatusOK, dto.BookSearchGet{Books: books, Count: count})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,9 +72,6 @@ func Setup(config *config.Config) *gin.Engine {
|
|||||||
r.GET("/author/:id/books", func(c *gin.Context) {
|
r.GET("/author/:id/books", func(c *gin.Context) {
|
||||||
routes.GetAuthorBooksHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
routes.GetAuthorBooksHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||||
})
|
})
|
||||||
r.GET("/author/:id/books/count", func(c *gin.Context) {
|
|
||||||
routes.GetAuthorBooksCountHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
||||||
})
|
|
||||||
r.POST("/auth/signup", func(c *gin.Context) {
|
r.POST("/auth/signup", func(c *gin.Context) {
|
||||||
routes.PostSignupHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
routes.PostSignupHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user