Add a view to see all books in a collection
This commit is contained in:
@@ -186,6 +186,7 @@ func selectBookItem() string {
|
||||
|
||||
type CollectionsQueryResult struct {
|
||||
ID uint
|
||||
UserID uint
|
||||
Name string
|
||||
BookId uint
|
||||
BookTitle string
|
||||
@@ -204,7 +205,8 @@ func FetchAllCollections(db *gorm.DB, userId uint, limit int, offset int) ([]Col
|
||||
return collections, res.Error
|
||||
}
|
||||
for _, collectionId := range collectionIds {
|
||||
queryResults, err := queryBooksForCollection(db, collectionId.ID)
|
||||
//only takes first 5 books
|
||||
queryResults, err := FetchCollectionBooks(db, collectionId.ID, 5, 0)
|
||||
if err != nil {
|
||||
return collections, res.Error
|
||||
}
|
||||
@@ -213,18 +215,29 @@ func FetchAllCollections(db *gorm.DB, userId uint, limit int, offset int) ([]Col
|
||||
return collections, res.Error
|
||||
}
|
||||
|
||||
func queryBooksForCollection(db *gorm.DB, collectionId uint) ([]CollectionsQueryResult, error) {
|
||||
func FetchCollectionBooks(db *gorm.DB, collectionId uint, limit int, offset int) ([]CollectionsQueryResult, error) {
|
||||
var collections []CollectionsQueryResult
|
||||
query := fetchCollectionBooksQuery(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).Count(&count)
|
||||
return count, res.Error
|
||||
}
|
||||
|
||||
func fetchCollectionBooksQuery(db *gorm.DB, collectionId uint) *gorm.DB {
|
||||
query := db.Model(&model.Collection{})
|
||||
query = query.Select("collections.id, collections.name, books.id as book_id, books.title as book_title, " + selectStaticFilesPath())
|
||||
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)
|
||||
//only takes first 5 books
|
||||
query = query.Limit(5)
|
||||
res := query.Find(&collections)
|
||||
return collections, res.Error
|
||||
return query
|
||||
}
|
||||
|
||||
func FetchAllCollectionsCount(db *gorm.DB, userId uint) (int64, error) {
|
||||
|
||||
Reference in New Issue
Block a user