137 lines
4.7 KiB
Go
137 lines
4.7 KiB
Go
package query
|
|
|
|
import (
|
|
"git.artlef.fr/bibliomane/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type CollectionHeader struct {
|
|
Name string
|
|
UserID uint
|
|
}
|
|
|
|
// collection header without the books
|
|
func FetchCollectionHeader(db *gorm.DB, collectionId uint) (CollectionHeader, error) {
|
|
var collection CollectionHeader
|
|
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
|
|
}
|
|
|
|
func FetchCollectionHeaderFromItem(db *gorm.DB, itemId uint) (CollectionHeader, error) {
|
|
var collection CollectionHeader
|
|
query := db.Model(&model.Collection{})
|
|
query = query.Select("collections.name, collections.user_id")
|
|
query = query.Joins("left join collection_items on (collection_items.collection_id = collections.id)")
|
|
query = query.Where("collection_items.id = ?", itemId)
|
|
res := query.Find(&collection)
|
|
return collection, res.Error
|
|
}
|
|
|
|
type CollectionsQueryResult struct {
|
|
ID uint
|
|
UserID uint
|
|
Name string
|
|
BookId uint
|
|
BookTitle string
|
|
CoverPath string
|
|
}
|
|
|
|
type collectionId struct {
|
|
ID uint
|
|
}
|
|
|
|
func FetchAllCollections(db *gorm.DB, userId uint, limit int, offset int) ([]CollectionsQueryResult, error) {
|
|
var collections []CollectionsQueryResult
|
|
var collectionIds []collectionId
|
|
res := fetchCollections(db, userId).Limit(limit).Offset(offset).Order("collections.id DESC").Find(&collectionIds)
|
|
if res.Error != nil {
|
|
return collections, res.Error
|
|
}
|
|
for _, collectionId := range collectionIds {
|
|
//only takes first 8 books
|
|
queryResults, err := fetchCollectionItemBook(db, collectionId.ID, 8, 0)
|
|
if err != nil {
|
|
return collections, res.Error
|
|
}
|
|
collections = append(collections, queryResults...)
|
|
}
|
|
return collections, res.Error
|
|
}
|
|
|
|
func fetchCollectionItemBook(db *gorm.DB, collectionId uint, limit int, offset int) ([]CollectionsQueryResult, error) {
|
|
var collections []CollectionsQueryResult
|
|
query := fetchCollectionItemBooksQuery(db, collectionId)
|
|
query = query.Limit(limit)
|
|
query = query.Offset(offset)
|
|
res := query.Find(&collections)
|
|
return collections, res.Error
|
|
}
|
|
|
|
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_items on (collection_items.collection_id = collections.id)")
|
|
query = query.Joins("left join books on (books.id = collection_items.book_id)")
|
|
query = joinStaticFiles(query)
|
|
query = query.Where("collections.id = ?", collectionId)
|
|
return query
|
|
}
|
|
|
|
func FetchAllCollectionsCount(db *gorm.DB, userId uint) (int64, error) {
|
|
var count int64
|
|
res := fetchCollections(db, userId).Count(&count)
|
|
return count, res.Error
|
|
}
|
|
|
|
func fetchCollections(db *gorm.DB, userId uint) *gorm.DB {
|
|
return db.Model(&model.Collection{}).Where("collections.user_id = ?", userId)
|
|
}
|
|
|
|
type CollectionItemQueryResult struct {
|
|
ItemID uint
|
|
Position uint
|
|
ID uint
|
|
Title string
|
|
Author string
|
|
Description string
|
|
InventaireID string
|
|
IsInventaireEdition bool
|
|
Rating int
|
|
Read bool
|
|
StartReadDate string
|
|
WantRead bool
|
|
CoverPath string
|
|
}
|
|
|
|
func FetchCollectionItems(db *gorm.DB, userId uint, collectionId uint, limit int, offset int) ([]CollectionItemQueryResult, error) {
|
|
var collectionitems []CollectionItemQueryResult
|
|
query := fetchCollectionBooksQuery(db, userId, collectionId)
|
|
query = query.Limit(limit)
|
|
query = query.Offset(offset)
|
|
query = query.Order("collection_items.position")
|
|
res := query.Find(&collectionitems)
|
|
return collectionitems, 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 := db.Model(&model.CollectionItem{})
|
|
query = query.Select("collection_items.position, collection_items.ID as item_id, " + selectBookItem())
|
|
query = query.Joins("left join collections on (collection_items.collection_id = collections.id)")
|
|
query = query.Joins("left join books on (books.id = collection_items.book_id)")
|
|
query = joinAuthors(query)
|
|
query = query.Joins("left join user_books on (user_books.book_id = books.id and user_books.user_id = ?)", userId)
|
|
query = joinStaticFiles(query)
|
|
query = query.Order("collection_items.position")
|
|
query = query.Where("collections.id = ?", collectionId)
|
|
return query
|
|
}
|