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

@@ -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"
}