User book 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 BookCard from './BookCard.vue'
|
import BookCard from './BookCard.vue'
|
||||||
import { getMyBooks, getCountMyBooks } from './api.js'
|
import { getMyBooks } from './api.js'
|
||||||
import Pagination from './Pagination.vue'
|
import Pagination from './Pagination.vue'
|
||||||
|
|
||||||
|
|
||||||
@@ -21,18 +21,15 @@ let currentFilterState = ref(FilterStates.READ);
|
|||||||
|
|
||||||
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 totalBooksNumber = computed(() => (typeof(totalBooksData) != 'undefined' &&
|
let totalBooksNumber = computed(() => (typeof(data) != 'undefined' &&
|
||||||
totalBooksData.value != null) ? totalBooksData.value["count"] : 0);
|
data.value != null) ? data.value["count"] : 0);
|
||||||
let pageTotal = computed(() => Math.ceil(totalBooksNumber.value / limit))
|
let pageTotal = computed(() => Math.ceil(totalBooksNumber.value / limit))
|
||||||
|
|
||||||
fetchData();
|
fetchData();
|
||||||
|
|
||||||
function fetchData() {
|
function fetchData() {
|
||||||
let res = getMyBooks(data, error, currentFilterState.value, limit, offset.value);
|
let res = getMyBooks(data, error, currentFilterState.value, limit, offset.value);
|
||||||
let resCount = getCountMyBooks(totalBooksData, errorFetchTotal, currentFilterState.value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function onFilterButtonClick(newstate) {
|
function onFilterButtonClick(newstate) {
|
||||||
@@ -74,7 +71,7 @@ function pageChange(newPageNumber) {
|
|||||||
<div v-if="error">{{$t('bookbrowser.error', {error: error.message})}}</div>
|
<div v-if="error">{{$t('bookbrowser.error', {error: error.message})}}</div>
|
||||||
<div v-else-if="data">
|
<div v-else-if="data">
|
||||||
<div class="books">
|
<div class="books">
|
||||||
<div class="book" v-for="book in data" :key="book.id">
|
<div class="book" v-for="book in data.books" :key="book.id">
|
||||||
<BookCard v-bind="book" />
|
<BookCard v-bind="book" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -28,10 +28,6 @@ function useFetch(data, error, url) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCountMyBooks(data, error, arg) {
|
|
||||||
return useFetch(data, error, baseUrl + '/mybooks/' + arg + "/count");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getMyBooks(data, error, arg, limit, offset) {
|
export function getMyBooks(data, error, arg, limit, offset) {
|
||||||
const queryParams = new URLSearchParams({limit: limit, offset: offset});
|
const queryParams = new URLSearchParams({limit: limit, offset: offset});
|
||||||
return useFetch(data, error, baseUrl + '/mybooks/' + arg + "?" + queryParams.toString());
|
return useFetch(data, error, baseUrl + '/mybooks/' + arg + "?" + queryParams.toString());
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type bookAuthorGetResult struct {
|
type bookAuthorGetResult struct {
|
||||||
Count int64 `json:"count"`
|
Count int64 `json:"count"`
|
||||||
Books []bookAuthorGet `json:"books"`
|
Books []bookAuthorGet `json:"books"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,73 +0,0 @@
|
|||||||
package apitest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
type countResponse struct {
|
|
||||||
Count int
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetReadBooksCountHandler_Demo(t *testing.T) {
|
|
||||||
router := testutils.TestSetup()
|
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", "/mybooks/read/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, 22, c.Count)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetWantReadBooksCountHandler_Demo(t *testing.T) {
|
|
||||||
router := testutils.TestSetup()
|
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", "/mybooks/wantread/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, 2, c.Count)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGetReadingBooksCountHandler_Demo(t *testing.T) {
|
|
||||||
router := testutils.TestSetup()
|
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
|
||||||
|
|
||||||
req, _ := http.NewRequest("GET", "/mybooks/reading/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, 2, c.Count)
|
|
||||||
}
|
|
||||||
@@ -13,55 +13,60 @@ func TestGetReadBooksHandler_Demo(t *testing.T) {
|
|||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
token := testutils.ConnectDemoUser(router)
|
||||||
books := testGetReadBooksHandler(t, router, token, 200, "", "")
|
result := testGetReadBooksHandler(t, router, token, 200, "", "")
|
||||||
assert.Equal(t, 22, len(books))
|
assert.Equal(t, 22, len(result.Books))
|
||||||
|
assert.Equal(t, int64(22), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReadBooksHandler_DemoNoLimit(t *testing.T) {
|
func TestGetReadBooksHandler_DemoNoLimit(t *testing.T) {
|
||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
token := testutils.ConnectDemoUser(router)
|
||||||
books := testGetReadBooksHandler(t, router, token, 200, "100", "")
|
result := testGetReadBooksHandler(t, router, token, 200, "100", "")
|
||||||
assert.Equal(t, 22, len(books))
|
assert.Equal(t, 22, len(result.Books))
|
||||||
|
assert.Equal(t, int64(22), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReadBooksHandler_DemoLowerLimit(t *testing.T) {
|
func TestGetReadBooksHandler_DemoLowerLimit(t *testing.T) {
|
||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
token := testutils.ConnectDemoUser(router)
|
||||||
books := testGetReadBooksHandler(t, router, token, 200, "5", "")
|
result := testGetReadBooksHandler(t, router, token, 200, "5", "")
|
||||||
assert.Equal(t, 5, len(books))
|
assert.Equal(t, 5, len(result.Books))
|
||||||
|
assert.Equal(t, int64(22), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReadBooksHandler_DemoOffset(t *testing.T) {
|
func TestGetReadBooksHandler_DemoOffset(t *testing.T) {
|
||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
token := testutils.ConnectDemoUser(router)
|
||||||
books := testGetReadBooksHandler(t, router, token, 200, "20", "10")
|
result := testGetReadBooksHandler(t, router, token, 200, "20", "10")
|
||||||
assert.Equal(t, 12, len(books))
|
assert.Equal(t, 12, len(result.Books))
|
||||||
|
assert.Equal(t, int64(22), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReadBooksHandler_Demo2(t *testing.T) {
|
func TestGetReadBooksHandler_Demo2(t *testing.T) {
|
||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemo2User(router)
|
token := testutils.ConnectDemo2User(router)
|
||||||
books := testGetReadBooksHandler(t, router, token, 200, "", "")
|
result := testGetReadBooksHandler(t, router, token, 200, "", "")
|
||||||
assert.Equal(t, 3, len(books))
|
assert.Equal(t, 3, len(result.Books))
|
||||||
|
assert.Equal(t, int64(3), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReadBooksHandler_CheckOneBook(t *testing.T) {
|
func TestGetReadBooksHandler_CheckOneBook(t *testing.T) {
|
||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemo2User(router)
|
token := testutils.ConnectDemo2User(router)
|
||||||
books := testGetReadBooksHandler(t, router, token, 200, "100", "")
|
result := testGetReadBooksHandler(t, router, token, 200, "100", "")
|
||||||
var book bookUserGet
|
var book bookUserGetBook
|
||||||
for _, b := range books {
|
for _, b := range result.Books {
|
||||||
if b.Title == "De sang-froid" {
|
if b.Title == "De sang-froid" {
|
||||||
book = b
|
book = b
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert.Equal(t,
|
assert.Equal(t,
|
||||||
bookUserGet{
|
bookUserGetBook{
|
||||||
BookId: 18,
|
BookId: 18,
|
||||||
Title: "De sang-froid",
|
Title: "De sang-froid",
|
||||||
Author: "Truman Capote",
|
Author: "Truman Capote",
|
||||||
@@ -70,7 +75,7 @@ func TestGetReadBooksHandler_CheckOneBook(t *testing.T) {
|
|||||||
}, book)
|
}, book)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGetReadBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, limit string, offset string) []bookUserGet {
|
func testGetReadBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, limit string, offset string) bookUserGet {
|
||||||
u, err := url.Parse("/mybooks/read")
|
u, err := url.Parse("/mybooks/read")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|||||||
@@ -13,19 +13,21 @@ func TestGetReadingBooksHandler_Demo(t *testing.T) {
|
|||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
token := testutils.ConnectDemoUser(router)
|
||||||
books := testGetReadingBooksHandler(t, router, token, 200, "", "")
|
result := testGetReadingBooksHandler(t, router, token, 200, "", "")
|
||||||
assert.Equal(t, 2, len(books))
|
assert.Equal(t, 2, len(result.Books))
|
||||||
|
assert.Equal(t, int64(2), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetReadingBooksHandler_Demo2(t *testing.T) {
|
func TestGetReadingBooksHandler_Demo2(t *testing.T) {
|
||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemo2User(router)
|
token := testutils.ConnectDemo2User(router)
|
||||||
books := testGetReadingBooksHandler(t, router, token, 200, "", "")
|
result := testGetReadingBooksHandler(t, router, token, 200, "", "")
|
||||||
assert.Equal(t, 0, len(books))
|
assert.Equal(t, 0, len(result.Books))
|
||||||
|
assert.Equal(t, int64(0), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGetReadingBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, limit string, offset string) []bookUserGet {
|
func testGetReadingBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int, limit string, offset string) bookUserGet {
|
||||||
u, err := url.Parse("/mybooks/reading")
|
u, err := url.Parse("/mybooks/reading")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|||||||
@@ -13,6 +13,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type bookUserGet struct {
|
type bookUserGet struct {
|
||||||
|
Count int64 `json:"count"`
|
||||||
|
Books []bookUserGetBook `json:"books"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type bookUserGetBook struct {
|
||||||
BookId uint `json:"id"`
|
BookId 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"`
|
||||||
@@ -21,14 +26,14 @@ type bookUserGet struct {
|
|||||||
WantRead bool `json:"wantread"`
|
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, _ := http.NewRequest("GET", url, nil)
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", userToken))
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", userToken))
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
var parsedResponse []bookUserGet
|
var parsedResponse bookUserGet
|
||||||
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
|
err := json.Unmarshal(w.Body.Bytes(), &parsedResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|||||||
@@ -12,18 +12,20 @@ func TestGetWantReadBooksHandler_Demo(t *testing.T) {
|
|||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemoUser(router)
|
token := testutils.ConnectDemoUser(router)
|
||||||
books := testGetWantReadBooksHandler(t, router, token, 200)
|
result := testGetWantReadBooksHandler(t, router, token, 200)
|
||||||
assert.Equal(t, 2, len(books))
|
assert.Equal(t, 2, len(result.Books))
|
||||||
|
assert.Equal(t, int64(2), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetWantReadBooksHandler_Demo2(t *testing.T) {
|
func TestGetWantReadBooksHandler_Demo2(t *testing.T) {
|
||||||
router := testutils.TestSetup()
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
token := testutils.ConnectDemo2User(router)
|
token := testutils.ConnectDemo2User(router)
|
||||||
books := testGetWantReadBooksHandler(t, router, token, 200)
|
result := testGetWantReadBooksHandler(t, router, token, 200)
|
||||||
assert.Equal(t, 0, len(books))
|
assert.Equal(t, 0, len(result.Books))
|
||||||
|
assert.Equal(t, int64(0), result.Count)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGetWantReadBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int) []bookUserGet {
|
func testGetWantReadBooksHandler(t *testing.T, router *gin.Engine, userToken string, expectedCode int) bookUserGet {
|
||||||
return testGetbooksHandler(t, router, userToken, expectedCode, "/mybooks/wantread")
|
return testGetbooksHandler(t, router, userToken, expectedCode, "/mybooks/wantread")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ type BookGet struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type BookUserGet struct {
|
type BookUserGet struct {
|
||||||
|
Count int64 `json:"count"`
|
||||||
|
Books []BookUserGetBook `json:"books"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type BookUserGetBook 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,8 +24,8 @@ func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (dto.BookGet, error)
|
|||||||
return book, res.Error
|
return book, res.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGet, error) {
|
func FetchReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGetBook, error) {
|
||||||
var books []dto.BookUserGet
|
var books []dto.BookUserGetBook
|
||||||
query := fetchReadUserBookQuery(db, userId)
|
query := fetchReadUserBookQuery(db, userId)
|
||||||
query = query.Limit(limit)
|
query = query.Limit(limit)
|
||||||
query = query.Offset(offset)
|
query = query.Offset(offset)
|
||||||
@@ -40,8 +40,8 @@ func FetchReadUserBookCount(db *gorm.DB, userId uint) (int64, error) {
|
|||||||
return count, res.Error
|
return count, res.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchReadingUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGet, error) {
|
func FetchReadingUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGetBook, error) {
|
||||||
var books []dto.BookUserGet
|
var books []dto.BookUserGetBook
|
||||||
query := fetchReadingUserBookQuery(db, userId)
|
query := fetchReadingUserBookQuery(db, userId)
|
||||||
query = query.Limit(limit)
|
query = query.Limit(limit)
|
||||||
query = query.Offset(offset)
|
query = query.Offset(offset)
|
||||||
@@ -67,8 +67,8 @@ func fetchReadingUserBookQuery(db *gorm.DB, userId uint) *gorm.DB {
|
|||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchWantReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGet, error) {
|
func FetchWantReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGetBook, error) {
|
||||||
var books []dto.BookUserGet
|
var books []dto.BookUserGetBook
|
||||||
|
|
||||||
query := fetchWantReadUserBookQuery(db, userId)
|
query := fetchWantReadUserBookQuery(db, userId)
|
||||||
query = query.Limit(limit)
|
query = query.Limit(limit)
|
||||||
|
|||||||
@@ -52,4 +52,3 @@ func GetAuthorBooksHandler(ac appcontext.AppContext) {
|
|||||||
}
|
}
|
||||||
ac.C.JSON(http.StatusOK, dto.BookSearchGet{Books: books, Count: count})
|
ac.C.JSON(http.StatusOK, dto.BookSearchGet{Books: books, Count: count})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMyBooksReadHandler(ac appcontext.AppContext) {
|
func GetMyBooksReadHandler(ac appcontext.AppContext) {
|
||||||
@@ -30,19 +30,10 @@ func GetMyBooksReadHandler(ac appcontext.AppContext) {
|
|||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ac.C.JSON(http.StatusOK, userbooks)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetMyBooksReadCountHandler(ac appcontext.AppContext) {
|
|
||||||
user, err := ac.GetAuthenticatedUser()
|
|
||||||
if err != nil {
|
|
||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
count, err := query.FetchReadUserBookCount(ac.Db, user.ID)
|
count, err := query.FetchReadUserBookCount(ac.Db, user.ID)
|
||||||
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.BookUserGet{Count: count, Books: userbooks})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMyBooksReadingHandler(ac appcontext.AppContext) {
|
func GetMyBooksReadingHandler(ac appcontext.AppContext) {
|
||||||
@@ -30,19 +30,10 @@ func GetMyBooksReadingHandler(ac appcontext.AppContext) {
|
|||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ac.C.JSON(http.StatusOK, userbooks)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetMyBooksReadingCountHandler(ac appcontext.AppContext) {
|
|
||||||
user, err := ac.GetAuthenticatedUser()
|
|
||||||
if err != nil {
|
|
||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
count, err := query.FetchReadingUserBookCount(ac.Db, user.ID)
|
count, err := query.FetchReadingUserBookCount(ac.Db, user.ID)
|
||||||
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.BookUserGet{Count: count, Books: userbooks})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||||
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMyBooksWantReadHandler(ac appcontext.AppContext) {
|
func GetMyBooksWantReadHandler(ac appcontext.AppContext) {
|
||||||
@@ -30,19 +30,10 @@ func GetMyBooksWantReadHandler(ac appcontext.AppContext) {
|
|||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ac.C.JSON(http.StatusOK, userbooks)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetMyBooksWantReadCountHandler(ac appcontext.AppContext) {
|
|
||||||
user, err := ac.GetAuthenticatedUser()
|
|
||||||
if err != nil {
|
|
||||||
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
count, err := query.FetchWantReadUserBookCount(ac.Db, user.ID)
|
count, err := query.FetchWantReadUserBookCount(ac.Db, user.ID)
|
||||||
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.BookUserGet{Count: count, Books: userbooks})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,21 +27,12 @@ func Setup(config *config.Config) *gin.Engine {
|
|||||||
r.GET("/mybooks/read", func(c *gin.Context) {
|
r.GET("/mybooks/read", func(c *gin.Context) {
|
||||||
routes.GetMyBooksReadHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
routes.GetMyBooksReadHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||||
})
|
})
|
||||||
r.GET("/mybooks/read/count", func(c *gin.Context) {
|
|
||||||
routes.GetMyBooksReadCountHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
||||||
})
|
|
||||||
r.GET("/mybooks/reading", func(c *gin.Context) {
|
r.GET("/mybooks/reading", func(c *gin.Context) {
|
||||||
routes.GetMyBooksReadingHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
routes.GetMyBooksReadingHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||||
})
|
})
|
||||||
r.GET("/mybooks/reading/count", func(c *gin.Context) {
|
|
||||||
routes.GetMyBooksReadingCountHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
||||||
})
|
|
||||||
r.GET("/mybooks/wantread", func(c *gin.Context) {
|
r.GET("/mybooks/wantread", func(c *gin.Context) {
|
||||||
routes.GetMyBooksWantReadHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
routes.GetMyBooksWantReadHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||||
})
|
})
|
||||||
r.GET("/mybooks/wantread/count", func(c *gin.Context) {
|
|
||||||
routes.GetMyBooksWantReadCountHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
|
||||||
})
|
|
||||||
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})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user