Add a new tab to browse all books on the instance
This commit is contained in:
@@ -83,6 +83,9 @@ onMounted(() => {
|
|||||||
<div id="navmenu" class="navbar-menu" :class="isMenuActive ? 'is-active' : ''">
|
<div id="navmenu" class="navbar-menu" :class="isMenuActive ? 'is-active' : ''">
|
||||||
<div class="navbar-start">
|
<div class="navbar-start">
|
||||||
<NavBarSearch size-class="" class="is-hidden-touch" />
|
<NavBarSearch size-class="" class="is-hidden-touch" />
|
||||||
|
<RouterLink v-if="authStore.user" to="/browse" class="navbar-item" activeClass="is-active">
|
||||||
|
{{ $t('navbar.explore') }}
|
||||||
|
</RouterLink>
|
||||||
<RouterLink v-if="authStore.user" to="/books" class="navbar-item" activeClass="is-active">
|
<RouterLink v-if="authStore.user" to="/books" class="navbar-item" activeClass="is-active">
|
||||||
{{ $t('navbar.mybooks') }}
|
{{ $t('navbar.mybooks') }}
|
||||||
</RouterLink>
|
</RouterLink>
|
||||||
|
|||||||
@@ -17,8 +17,8 @@ const offset = computed(() => (pageNumber.value - 1) * limit)
|
|||||||
|
|
||||||
let currentFilterState = ref(FilterStates.READ)
|
let currentFilterState = ref(FilterStates.READ)
|
||||||
|
|
||||||
let data = ref(null)
|
const data = ref(null)
|
||||||
let error = ref(null)
|
const error = ref(null)
|
||||||
|
|
||||||
let totalBooksNumber = computed(() =>
|
let totalBooksNumber = computed(() =>
|
||||||
typeof data != 'undefined' && data.value != null ? data.value['count'] : 0,
|
typeof data != 'undefined' && data.value != null ? data.value['count'] : 0,
|
||||||
|
|||||||
61
front/src/InstanceBrowser.vue
Normal file
61
front/src/InstanceBrowser.vue
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<script setup>
|
||||||
|
import { ref, computed } from 'vue'
|
||||||
|
import { getAllBooks } from './api.js'
|
||||||
|
import { onBeforeRouteUpdate } from 'vue-router'
|
||||||
|
import BookListElement from './BookListElement.vue'
|
||||||
|
import Pagination from './Pagination.vue'
|
||||||
|
|
||||||
|
const limit = 5
|
||||||
|
const pageNumber = ref(1)
|
||||||
|
|
||||||
|
const offset = computed(() => (pageNumber.value - 1) * limit)
|
||||||
|
|
||||||
|
const data = ref(null)
|
||||||
|
const error = ref(null)
|
||||||
|
|
||||||
|
const pageTotal = computed(() => {
|
||||||
|
const countValue = data.value !== null ? data.value['count'] : 0
|
||||||
|
return Math.ceil(countValue / limit)
|
||||||
|
})
|
||||||
|
|
||||||
|
function fetchData() {
|
||||||
|
data.value = null
|
||||||
|
error.value = null
|
||||||
|
getAllBooks(data, error, limit, offset.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchData()
|
||||||
|
|
||||||
|
onBeforeRouteUpdate(async (to, from) => {
|
||||||
|
pageNumber.value = 1
|
||||||
|
fetchData()
|
||||||
|
})
|
||||||
|
|
||||||
|
function pageChange(newPageNumber) {
|
||||||
|
pageNumber.value = newPageNumber
|
||||||
|
fetchData()
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
<div>
|
||||||
|
<div v-if="error"> {{ error }}</div>
|
||||||
|
<div v-else-if="data && data.books && data.books.length > 0">
|
||||||
|
<div class="booksearchlist" v-for="book in data.books" :key="book.id">
|
||||||
|
<BookListElement v-bind="book" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div v-else-if="data === null">{{ $t('searchbook.loading') }}</div>
|
||||||
|
<div v-else>{{ $t('searchbook.noresult') }}</div>
|
||||||
|
</div>
|
||||||
|
<Pagination
|
||||||
|
:pageNumber="pageNumber"
|
||||||
|
:pageTotal="pageTotal"
|
||||||
|
maxItemDisplayed="11"
|
||||||
|
@pageChange="pageChange"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped></style>
|
||||||
@@ -54,6 +54,14 @@ export function getMyBooks(data, error, arg, limit, offset) {
|
|||||||
return useFetch(data, error, '/ws/mybooks/' + arg + '?' + queryParams.toString())
|
return useFetch(data, error, '/ws/mybooks/' + arg + '?' + queryParams.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getAllBooks(data, error, limit, offset) {
|
||||||
|
const queryParams = new URLSearchParams({
|
||||||
|
limit: limit,
|
||||||
|
offset: offset,
|
||||||
|
})
|
||||||
|
return useFetch(data, error, '/ws/books' + '?' + queryParams.toString())
|
||||||
|
}
|
||||||
|
|
||||||
export function getSearchBooks(data, error, searchterm, lang, searchInventaire, limit, offset) {
|
export function getSearchBooks(data, error, searchterm, lang, searchInventaire, limit, offset) {
|
||||||
const queryParams = new URLSearchParams({
|
const queryParams = new URLSearchParams({
|
||||||
lang: lang,
|
lang: lang,
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
"navbar": {
|
"navbar": {
|
||||||
"mybooks": "My Books",
|
"mybooks": "My Books",
|
||||||
"addbook": "Add Book",
|
"addbook": "Add Book",
|
||||||
|
"explore": "Explore",
|
||||||
"logout": "Log out",
|
"logout": "Log out",
|
||||||
"signup": "Sign up",
|
"signup": "Sign up",
|
||||||
"search": "Search",
|
"search": "Search",
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
},
|
},
|
||||||
"navbar": {
|
"navbar": {
|
||||||
"mybooks": "Mes Livres",
|
"mybooks": "Mes Livres",
|
||||||
|
"explore": "Explorer",
|
||||||
"addbook": "Ajouter Un Livre",
|
"addbook": "Ajouter Un Livre",
|
||||||
"logout": "Se déconnecter",
|
"logout": "Se déconnecter",
|
||||||
"signup": "S'inscrire",
|
"signup": "S'inscrire",
|
||||||
|
|||||||
@@ -10,11 +10,13 @@ import Home from './Home.vue'
|
|||||||
import ScanBook from './ScanBook.vue'
|
import ScanBook from './ScanBook.vue'
|
||||||
import SearchBook from './SearchBook.vue'
|
import SearchBook from './SearchBook.vue'
|
||||||
import ImportInventaire from './ImportInventaire.vue'
|
import ImportInventaire from './ImportInventaire.vue'
|
||||||
|
import InstanceBrowser from './InstanceBrowser.vue'
|
||||||
import { useAuthStore } from './auth.store'
|
import { useAuthStore } from './auth.store'
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
{ path: '/', component: Home },
|
{ path: '/', component: Home },
|
||||||
{ path: '/scan', component: ScanBook },
|
{ path: '/scan', component: ScanBook },
|
||||||
|
{ path: '/browse', component: InstanceBrowser },
|
||||||
{ path: '/books', component: BooksBrowser },
|
{ path: '/books', component: BooksBrowser },
|
||||||
{ path: '/book/:id', component: BookForm, props: true },
|
{ path: '/book/:id', component: BookForm, props: true },
|
||||||
{ path: '/author/:id', component: AuthorForm, props: true },
|
{ path: '/author/:id', component: AuthorForm, props: true },
|
||||||
|
|||||||
58
internal/apitest/fetchallbooks_test.go
Normal file
58
internal/apitest/fetchallbooks_test.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package apitest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.artlef.fr/bibliomane/internal/dto"
|
||||||
|
"git.artlef.fr/bibliomane/internal/testutils"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFetchAllBooks(t *testing.T) {
|
||||||
|
result := testFetchBooks(t, "15", "0")
|
||||||
|
assert.Equal(t, int64(31), result.Count)
|
||||||
|
assert.Equal(t, 15, len(result.Books))
|
||||||
|
}
|
||||||
|
|
||||||
|
func testFetchBooks(t *testing.T, limit string, offset string) dto.BookSearchGet {
|
||||||
|
router := testutils.TestSetup()
|
||||||
|
|
||||||
|
u, err := url.Parse("/ws/books")
|
||||||
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
|
q := u.Query()
|
||||||
|
q.Set("lang", "fr")
|
||||||
|
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 result dto.BookSearchGet
|
||||||
|
s := w.Body.String()
|
||||||
|
err = json.Unmarshal([]byte(s), &result)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
assert.Equal(t, 200, w.Code)
|
||||||
|
return result
|
||||||
|
}
|
||||||
@@ -103,6 +103,22 @@ func fetchUserBookGet(db *gorm.DB, userId uint) *gorm.DB {
|
|||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FetchAllBooks(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookSearchGetBook, error) {
|
||||||
|
var books []dto.BookSearchGetBook
|
||||||
|
query := fetchBookQueryBuilder(db, userId)
|
||||||
|
query = query.Limit(limit)
|
||||||
|
query = query.Offset(offset)
|
||||||
|
res := query.Find(&books)
|
||||||
|
return books, res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func FetchAllBooksCount(db *gorm.DB, userId uint) (int64, error) {
|
||||||
|
var count int64
|
||||||
|
query := fetchBookQueryBuilder(db, userId)
|
||||||
|
res := query.Count(&count)
|
||||||
|
return count, res.Error
|
||||||
|
}
|
||||||
|
|
||||||
func FetchBookSearchByAuthorGet(db *gorm.DB, userId uint, authorId uint64, limit int, offset int) ([]dto.BookSearchGetBook, error) {
|
func FetchBookSearchByAuthorGet(db *gorm.DB, userId uint, authorId uint64, limit int, offset int) ([]dto.BookSearchGetBook, error) {
|
||||||
var books []dto.BookSearchGetBook
|
var books []dto.BookSearchGetBook
|
||||||
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
||||||
|
|||||||
40
internal/routes/booksget.go
Normal file
40
internal/routes/booksget.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package routes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.artlef.fr/bibliomane/internal/appcontext"
|
||||||
|
"git.artlef.fr/bibliomane/internal/dto"
|
||||||
|
"git.artlef.fr/bibliomane/internal/myvalidator"
|
||||||
|
"git.artlef.fr/bibliomane/internal/query"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetBooksHandler(ac appcontext.AppContext) {
|
||||||
|
user, err := ac.GetAuthenticatedUser()
|
||||||
|
if err != nil {
|
||||||
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
|
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.FetchAllBooks(ac.Db, user.ID, limit, offset)
|
||||||
|
if err != nil {
|
||||||
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
count, err := query.FetchAllBooksCount(ac.Db, user.ID)
|
||||||
|
if err != nil {
|
||||||
|
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ac.C.JSON(http.StatusOK, dto.BookSearchGet{Count: count, Inventaire: false, Books: books})
|
||||||
|
}
|
||||||
@@ -39,7 +39,9 @@ func Setup(config *config.Config) *gin.Engine {
|
|||||||
ws.GET("/appinfo", func(c *gin.Context) {
|
ws.GET("/appinfo", func(c *gin.Context) {
|
||||||
routes.GetAppInfo(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
routes.GetAppInfo(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||||
})
|
})
|
||||||
|
ws.GET("/books", func(c *gin.Context) {
|
||||||
|
routes.GetBooksHandler(appcontext.AppContext{C: c, Db: db, I18n: bundle, Config: config})
|
||||||
|
})
|
||||||
ws.GET("/mybooks/read", func(c *gin.Context) {
|
ws.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})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user