Use usual book widget for collection form view

This commit is contained in:
2026-04-07 16:16:11 +02:00
parent a537c12a3b
commit f2899b968c
6 changed files with 63 additions and 83 deletions

View File

@@ -1,7 +1,7 @@
<script setup>
import { computed, ref } from 'vue'
import { getCollection } from './api.js'
import CollectionFormBookItem from './CollectionFormBookItem.vue'
import BookListElement from './BookListElement.vue'
import AddBookToCollection from './AddBookToCollection.vue'
import Pagination from './Pagination.vue'
@@ -43,7 +43,7 @@ function fetchCollection() {
<h2 class="title">{{ data.name }}</h2>
<AddBookToCollection :collection-id="props.id" @created="fetchCollection" />
<div>
<CollectionFormBookItem v-for="book in data.books" :key="book.id" v-bind="book" />
<BookListElement v-for="book in data.books" :key="book.id" v-bind="book" />
</div>
<Pagination
class="mt-5"

View File

@@ -1,37 +0,0 @@
<script setup>
import { getImagePathOrDefault } from './api.js'
import { computed } from 'vue'
const props = defineProps({
title: String,
coverPath: String,
})
const imagePathOrDefault = computed(() => getImagePathOrDefault(props.coverPath))
</script>
<template>
<div class="bookcontainer has-background-dark p-2 m-2">
<img class="mr-2" v-bind:src="imagePathOrDefault" v-bind:alt="title" />
<h3 class="centered subtitle">{{ title }}</h3>
</div>
</template>
<style>
img {
max-height: 200px;
max-width: 200px;
height: auto;
width: auto;
}
.bookcontainer {
display: flex;
transition: ease-in-out 0.04s;
border-radius: 20px;
}
.bookcontainer:hover {
transform: scale(1.01);
transition: ease-in-out 0.02s;
}
</style>

View File

@@ -10,19 +10,6 @@ import (
"gorm.io/gorm"
)
func CollectionQueryToCollectionDto(collectionsQueryResult []query.CollectionsQueryResult) dto.CollectionGet {
var collection dto.CollectionGet
for _, collectionsDb := range collectionsQueryResult {
collection.Name = collectionsDb.Name
book := collectionDbToBookItem(&collectionsDb)
if book != nil {
collection.Books = append(collection.Books, *book)
}
collection.UserID = collectionsDb.UserID
}
return collection
}
func CollectionQueryToCollectionItemDto(collectionsQueryResult []query.CollectionsQueryResult) []dto.CollectionItemGet {
var collections []dto.CollectionItemGet
for _, collectionDb := range collectionsQueryResult {
@@ -35,7 +22,7 @@ func CollectionQueryToCollectionItemDto(collectionsQueryResult []query.Collectio
//current collection is the last element
i = len(collections) - 1
}
book := collectionDbToBookItem(&collectionDb)
book := collectionDbToCollectionBookItem(&collectionDb)
if book != nil {
collections[i].Books = append(collections[i].Books, *book)
}
@@ -43,7 +30,7 @@ func CollectionQueryToCollectionItemDto(collectionsQueryResult []query.Collectio
return collections
}
func collectionDbToBookItem(collectionDb *query.CollectionsQueryResult) *dto.CollectionBookItemGet {
func collectionDbToCollectionBookItem(collectionDb *query.CollectionsQueryResult) *dto.CollectionBookItemGet {
if collectionDb.BookId > 0 {
bookItem := dto.CollectionBookItemGet{
ID: collectionDb.BookId,

View File

@@ -47,7 +47,7 @@ type BookItemGet struct {
type CollectionGet struct {
Name string `json:"name"`
Count int64 `json:"count"`
Books []CollectionBookItemGet `json:"books"`
Books []BookItemGet `json:"books"`
UserID uint `json:"-"`
}

View File

@@ -184,6 +184,16 @@ func selectBookItem() string {
return "books.id, books.title, authors.name as author, books.short_description as description, books.inventaire_id, user_books.rating, user_books.read, DATE(user_books.start_read_date) as start_read_date, user_books.want_read, " + selectStaticFilesPath()
}
// collection header without the books
func FetchCollectionHeader(db *gorm.DB, collectionId uint) (dto.CollectionGet, error) {
var collection dto.CollectionGet
query := db.Model(&model.Collection{})
query = query.Select("collections.name, collections.user_id")
query = query.Where("collections.id = ?", collectionId)
res := query.Find(&collection)
return collection, res.Error
}
type CollectionsQueryResult struct {
ID uint
UserID uint
@@ -206,7 +216,7 @@ func FetchAllCollections(db *gorm.DB, userId uint, limit int, offset int) ([]Col
}
for _, collectionId := range collectionIds {
//only takes first 5 books
queryResults, err := FetchCollectionBooks(db, collectionId.ID, 5, 0)
queryResults, err := fetchCollectionItemBook(db, collectionId.ID, 5, 0)
if err != nil {
return collections, res.Error
}
@@ -215,22 +225,16 @@ func FetchAllCollections(db *gorm.DB, userId uint, limit int, offset int) ([]Col
return collections, res.Error
}
func FetchCollectionBooks(db *gorm.DB, collectionId uint, limit int, offset int) ([]CollectionsQueryResult, error) {
func fetchCollectionItemBook(db *gorm.DB, collectionId uint, limit int, offset int) ([]CollectionsQueryResult, error) {
var collections []CollectionsQueryResult
query := fetchCollectionBooksQuery(db, collectionId)
query := fetchCollectionItemBooksQuery(db, collectionId)
query = query.Limit(limit)
query = query.Offset(offset)
res := query.Find(&collections)
return collections, res.Error
}
func FetchCollectionBooksCount(db *gorm.DB, collectionId uint) (int64, error) {
var count int64
res := fetchCollectionBooksQuery(db, collectionId).Where("book_id IS NOT NULL").Count(&count)
return count, res.Error
}
func fetchCollectionBooksQuery(db *gorm.DB, collectionId uint) *gorm.DB {
func fetchCollectionItemBooksQuery(db *gorm.DB, collectionId uint) *gorm.DB {
query := db.Model(&model.Collection{})
query = query.Select("collections.id, collections.user_id, collections.name, books.id as book_id, books.title as book_title, " + selectStaticFilesPath())
query = query.Joins("left join collection_books on (collection_books.collection_id = collections.id)")
@@ -250,6 +254,29 @@ func fetchCollections(db *gorm.DB, userId uint) *gorm.DB {
return db.Model(&model.Collection{}).Where("collections.user_id = ?", userId)
}
func FetchCollectionBooks(db *gorm.DB, userId uint, collectionId uint, limit int, offset int) ([]dto.BookItemGet, error) {
var books []dto.BookItemGet
query := fetchCollectionBooksQuery(db, userId, collectionId)
query = query.Limit(limit)
query = query.Offset(offset)
query = query.Order("books.id DESC")
res := query.Find(&books)
return books, res.Error
}
func FetchCollectionBooksCount(db *gorm.DB, userId uint, collectionId uint) (int64, error) {
var count int64
res := fetchCollectionBooksQuery(db, userId, collectionId).Count(&count)
return count, res.Error
}
func fetchCollectionBooksQuery(db *gorm.DB, userId uint, collectionId uint) *gorm.DB {
query := fetchBookQueryBuilder(db, userId)
query = query.Joins("left join collection_books on (collection_books.book_id = books.id)")
query = query.Where("collection_books.collection_id = ?", collectionId)
return query
}
func selectStaticFilesPath() string {
return "(CASE COALESCE(static_files.path, '') WHEN '' THEN '' ELSE concat('" + fileutils.GetWsLinkPrefix() + "', static_files.path) END) as CoverPath"
}

View File

@@ -5,7 +5,6 @@ import (
"net/http"
"strconv"
"git.artlef.fr/bibliomane/internal/adapter"
"git.artlef.fr/bibliomane/internal/appcontext"
"git.artlef.fr/bibliomane/internal/i18nresource"
"git.artlef.fr/bibliomane/internal/myvalidator"
@@ -36,18 +35,7 @@ func GetCollectionHandler(ac appcontext.AppContext) {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
collectionBooksDb, err := query.FetchCollectionBooks(ac.Db, uint(collectionId), limit, offset)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
collection := adapter.CollectionQueryToCollectionDto(collectionBooksDb)
count, err := query.FetchCollectionBooksCount(ac.Db, uint(collectionId))
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
collection.Count = count
collection, err := query.FetchCollectionHeader(ac.Db, uint(collectionId))
if collection.UserID != user.ID {
err := myvalidator.HttpError{
StatusCode: http.StatusUnauthorized,
@@ -55,7 +43,22 @@ func GetCollectionHandler(ac appcontext.AppContext) {
}
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
} else {
ac.C.JSON(http.StatusOK, collection)
}
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
books, err := query.FetchCollectionBooks(ac.Db, user.ID, uint(collectionId), limit, offset)
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
collection.Books = books
count, err := query.FetchCollectionBooksCount(ac.Db, user.ID, uint(collectionId))
if err != nil {
myvalidator.ReturnErrorsAsJsonResponse(&ac, err)
return
}
collection.Count = count
ac.C.JSON(http.StatusOK, collection)
}