Add position for book in collection
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"git.artlef.fr/bibliomane/internal/dto"
|
||||
"git.artlef.fr/bibliomane/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -64,8 +63,8 @@ func fetchCollectionItemBook(db *gorm.DB, collectionId uint, limit int, offset i
|
||||
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 = 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
|
||||
@@ -81,14 +80,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
|
||||
type CollectionItemQueryResult struct {
|
||||
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("books.id DESC")
|
||||
res := query.Find(&books)
|
||||
return books, res.Error
|
||||
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) {
|
||||
@@ -98,8 +112,14 @@ func FetchCollectionBooksCount(db *gorm.DB, userId uint, collectionId uint) (int
|
||||
}
|
||||
|
||||
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)
|
||||
query := db.Model(&model.CollectionItem{})
|
||||
query = query.Select("collection_items.position, " + 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user