Author books API: use a single query with result and count
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||
@@ -44,30 +45,11 @@ func GetAuthorBooksHandler(ac appcontext.AppContext) {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
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)
|
||||
if err != nil {
|
||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||
return
|
||||
}
|
||||
ac.C.JSON(http.StatusOK, gin.H{"count": count})
|
||||
ac.C.JSON(http.StatusOK, dto.BookSearchGet{Books: books, Count: count})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user