Split query.go in two files

This commit is contained in:
2026-04-08 15:26:40 +02:00
parent 26931c734b
commit dbf0face76
2 changed files with 100 additions and 93 deletions

View File

@@ -184,99 +184,6 @@ 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() 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
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_books on (collection_books.collection_id = collections.id)")
query = query.Joins("left join books on (books.id = collection_books.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)
}
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 { func selectStaticFilesPath() string {
return "(CASE COALESCE(static_files.path, '') WHEN '' THEN '' ELSE concat('" + fileutils.GetWsLinkPrefix() + "', static_files.path) END) as CoverPath" return "(CASE COALESCE(static_files.path, '') WHEN '' THEN '' ELSE concat('" + fileutils.GetWsLinkPrefix() + "', static_files.path) END) as CoverPath"
} }

View File

@@ -0,0 +1,100 @@
package query
import (
"git.artlef.fr/bibliomane/internal/dto"
"git.artlef.fr/bibliomane/internal/model"
"gorm.io/gorm"
)
// 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
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_books on (collection_books.collection_id = collections.id)")
query = query.Joins("left join books on (books.id = collection_books.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)
}
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
}