Add author books list
This commit is contained in:
@@ -2,29 +2,38 @@
|
||||
import { ref } from 'vue'
|
||||
import { getAuthor } from './api.js'
|
||||
import { onBeforeRouteUpdate } from 'vue-router'
|
||||
import SearchBook from './SearchBook.vue'
|
||||
|
||||
|
||||
const props = defineProps({
|
||||
id: String
|
||||
});
|
||||
|
||||
let data = ref(null);
|
||||
let error = ref(null);
|
||||
let author = ref(null);
|
||||
let authorfetcherror = ref(null);
|
||||
|
||||
getAuthor(data, error, props.id);
|
||||
getAuthor(author, authorfetcherror, props.id);
|
||||
|
||||
onBeforeRouteUpdate(async (to, from) => {
|
||||
getAuthor(data, error, to.params.id);
|
||||
getAuthor(author, authorfetcherror, to.params.id);
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="error">{{$t('authorform.error', {err: error.message})}}</div>
|
||||
<div v-if="data">
|
||||
<h3 class="title">{{data.name}}</h3>
|
||||
<p v-if="data.description">{{data.description}}</p>
|
||||
<div v-if="authorfetcherror">{{$t('authorform.error', {err: authorfetcherror.message})}}</div>
|
||||
<div v-if="author">
|
||||
<h3 class="title">{{author.name}}</h3>
|
||||
<p v-if="author.description">{{author.description}}</p>
|
||||
</div>
|
||||
<div class="authorbooks mt-5">
|
||||
<SearchBook :author-id="id"/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
|
||||
.authorbooks {
|
||||
border: solid;
|
||||
border-radius: 50px;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script setup>
|
||||
import { ref, computed } from 'vue'
|
||||
import BookListElement from './BookListElement.vue';
|
||||
import { getSearchBooks, getCountSearchBooks } from './api.js'
|
||||
import { getSearchBooks, getCountSearchBooks, getAuthorBooks, getCountAuthorBooks } from './api.js'
|
||||
import { onBeforeRouteUpdate } from 'vue-router'
|
||||
import Pagination from './Pagination.vue'
|
||||
|
||||
@@ -11,7 +11,8 @@
|
||||
const offset = computed(() => (pageNumber.value - 1) * limit);
|
||||
|
||||
const props = defineProps({
|
||||
searchterm: String
|
||||
searchterm: String,
|
||||
authorId: Number
|
||||
});
|
||||
|
||||
let data = ref(null);
|
||||
@@ -24,29 +25,34 @@
|
||||
return Math.ceil(countValue / limit);
|
||||
});
|
||||
|
||||
fetchData(props.searchterm);
|
||||
fetchData(props.searchterm, props.authorId);
|
||||
|
||||
|
||||
function fetchData(searchTerm) {
|
||||
getSearchBooks(data, error, searchTerm, limit, offset.value);
|
||||
getCountSearchBooks(totalBooksData, errorFetchTotal, searchTerm);
|
||||
function fetchData(searchTerm, authorId) {
|
||||
if (searchTerm != null) {
|
||||
getSearchBooks(data, error, searchTerm, limit, offset.value);
|
||||
getCountSearchBooks(totalBooksData, errorFetchTotal, searchTerm);
|
||||
} else if (authorId != null) {
|
||||
getAuthorBooks(data, error, authorId, limit, offset.value);
|
||||
getCountAuthorBooks(totalBooksData, errorFetchTotal, searchTerm);
|
||||
}
|
||||
}
|
||||
|
||||
onBeforeRouteUpdate(async (to, from) => {
|
||||
pageNumber.value = 1;
|
||||
fetchData(to.params.searchterm);
|
||||
fetchData(to.params.searchterm, props.authorId);
|
||||
})
|
||||
|
||||
function pageChange(newPageNumber) {
|
||||
pageNumber.value = newPageNumber;
|
||||
data.value = null;
|
||||
fetchData(props.searchterm);
|
||||
fetchData(props.searchterm, props.authorId);
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="booksearch">
|
||||
<div class="booksearch my-2">
|
||||
<div v-if="error">{{$t('searchbook.error', {error: error.message})}}</div>
|
||||
<div v-else-if="data && data.length > 0">
|
||||
<div class="booksearchlist" v-for="book in data" :key="book.id">
|
||||
|
||||
@@ -45,6 +45,15 @@ export function getAuthor(data, error, id) {
|
||||
return useFetch(data, error, baseUrl + '/author/' + id);
|
||||
}
|
||||
|
||||
export function getAuthorBooks(data, error, id, limit, offset) {
|
||||
const queryParams = new URLSearchParams({limit: limit, offset: offset});
|
||||
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) {
|
||||
return useFetch(data, error, baseUrl + '/book/' + id);
|
||||
}
|
||||
|
||||
71
internal/apitest/get_book_per_author.go
Normal file
71
internal/apitest/get_book_per_author.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package apitest
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/testutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type bookAuthorGet struct {
|
||||
Id uint `json:"id"`
|
||||
Title string `json:"title" binding:"required,max=300"`
|
||||
Rating int `json:"rating"`
|
||||
Read bool `json:"read"`
|
||||
WantRead bool `json:"wantread"`
|
||||
CoverPath string `json:"coverPath"`
|
||||
}
|
||||
|
||||
func TestSearchBookPerAuthor_Ok(t *testing.T) {
|
||||
books := testFetchBookAuthor(t, 1, "", "")
|
||||
assert.Equal(t, 3, len(books))
|
||||
}
|
||||
|
||||
func TestSearchBookPerAuthor_Limit(t *testing.T) {
|
||||
books := testFetchBookAuthor(t, 1, "2", "")
|
||||
assert.Equal(t, 2, len(books))
|
||||
}
|
||||
|
||||
func TestSearchBookPerAuthor_Offset(t *testing.T) {
|
||||
books := testFetchBookAuthor(t, 1, "10", "2")
|
||||
assert.Equal(t, 1, len(books))
|
||||
}
|
||||
|
||||
func testFetchBookAuthor(t *testing.T, authorId uint, limit string, offset string) []bookAuthorGet {
|
||||
router := testutils.TestSetup()
|
||||
|
||||
u, err := url.Parse(fmt.Sprintf("/author/%d/books", authorId))
|
||||
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", u.String(), nil)
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
var books []bookAuthorGet
|
||||
s := w.Body.String()
|
||||
err = json.Unmarshal([]byte(s), &books)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, 200, w.Code)
|
||||
return books
|
||||
}
|
||||
@@ -71,3 +71,22 @@ func TestGetReadingBooksCountHandler_Demo(t *testing.T) {
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -13,12 +13,12 @@ func Auth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
//do not check current user if we are creating an account or logging in
|
||||
if strings.HasPrefix(c.FullPath(), "/auth") {
|
||||
if strings.HasPrefix(c.FullPath(), "/auth/") {
|
||||
return
|
||||
}
|
||||
|
||||
//do not check static files
|
||||
if strings.HasPrefix(c.FullPath(), "/bookcover") {
|
||||
if strings.HasPrefix(c.FullPath(), "/bookcover/") {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,27 @@ type BookSearchGet struct {
|
||||
CoverPath string `json:"coverPath"`
|
||||
}
|
||||
|
||||
func FetchBookSearchByAuthorGet(db *gorm.DB, userId uint, authorId uint64, limit int, offset int) ([]BookSearchGet, error) {
|
||||
var books []BookSearchGet
|
||||
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
||||
query = query.Limit(limit)
|
||||
query = query.Offset(offset)
|
||||
res := query.Find(&books)
|
||||
return books, res.Error
|
||||
}
|
||||
|
||||
func FetchBookSearchByAuthorGetCount(db *gorm.DB, userId uint, authorId uint64) (int64, error) {
|
||||
var count int64
|
||||
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
||||
res := query.Count(&count)
|
||||
return count, res.Error
|
||||
}
|
||||
|
||||
func fetchBookSearchByAuthorQuery(db *gorm.DB, userId uint, authorId uint64) *gorm.DB {
|
||||
query := fetchBookSearchQueryBuilder(db, userId)
|
||||
return query.Where("authors.id = ?", authorId)
|
||||
}
|
||||
|
||||
func FetchBookSearchGet(db *gorm.DB, userId uint, searchterm string, limit int, offset int) ([]BookSearchGet, error) {
|
||||
var books []BookSearchGet
|
||||
query := fetchBookSearchQuery(db, userId, searchterm)
|
||||
|
||||
73
internal/routes/authorbooksget.go
Normal file
73
internal/routes/authorbooksget.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/myvalidator"
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/query"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetAuthorBooksHandler(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
|
||||
}
|
||||
|
||||
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.FetchBookSearchByAuthorGet(ac.Db, user.ID, authorId, limit, offset)
|
||||
if err != nil {
|
||||
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})
|
||||
}
|
||||
@@ -69,6 +69,12 @@ func Setup(config *config.Config) *gin.Engine {
|
||||
r.GET("/author/:id", func(c *gin.Context) {
|
||||
routes.GetAuthorHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||
})
|
||||
r.GET("/author/:id/books", func(c *gin.Context) {
|
||||
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) {
|
||||
routes.PostSignupHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user