114 lines
3.8 KiB
Go
114 lines
3.8 KiB
Go
package query
|
|
|
|
import (
|
|
"git.artlef.fr/PersonalLibraryManager/internal/dto"
|
|
"git.artlef.fr/PersonalLibraryManager/internal/fileutils"
|
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (dto.BookGet, error) {
|
|
var book dto.BookGet
|
|
query := db.Model(&model.Book{})
|
|
selectQueryString := "books.title, authors.name as author, books.isbn, books.inventaire_id, books.open_library_id, books.summary, " +
|
|
"user_books.rating, user_books.read, user_books.want_read, " +
|
|
"DATE(user_books.start_read_date) as start_read_date, " +
|
|
"DATE(user_books.end_read_date) AS end_read_date, " +
|
|
selectStaticFilesPath()
|
|
query = query.Select(selectQueryString)
|
|
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.Where("books.id = ?", bookId)
|
|
res := query.First(&book)
|
|
return book, res.Error
|
|
}
|
|
|
|
func FetchReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGetBook, error) {
|
|
var books []dto.BookUserGetBook
|
|
query := fetchReadUserBookQuery(db, userId)
|
|
query = query.Limit(limit)
|
|
query = query.Offset(offset)
|
|
res := query.Find(&books)
|
|
return books, res.Error
|
|
}
|
|
|
|
func FetchReadUserBookCount(db *gorm.DB, userId uint) (int64, error) {
|
|
query := fetchReadUserBookQuery(db, userId)
|
|
var count int64
|
|
res := query.Count(&count)
|
|
return count, res.Error
|
|
}
|
|
|
|
func FetchReadingUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGetBook, error) {
|
|
var books []dto.BookUserGetBook
|
|
query := fetchReadingUserBookQuery(db, userId)
|
|
query = query.Limit(limit)
|
|
query = query.Offset(offset)
|
|
res := query.Find(&books)
|
|
return books, res.Error
|
|
}
|
|
|
|
func FetchReadingUserBookCount(db *gorm.DB, userId uint) (int64, error) {
|
|
query := fetchReadingUserBookQuery(db, userId)
|
|
var count int64
|
|
res := query.Count(&count)
|
|
return count, res.Error
|
|
}
|
|
|
|
func fetchReadUserBookQuery(db *gorm.DB, userId uint) *gorm.DB {
|
|
query := fetchUserBookGet(db, userId)
|
|
query = query.Where("user_books.read IS TRUE")
|
|
return query
|
|
}
|
|
func fetchReadingUserBookQuery(db *gorm.DB, userId uint) *gorm.DB {
|
|
query := fetchUserBookGet(db, userId)
|
|
query = query.Where("user_books.start_read_date IS NOT NULL")
|
|
return query
|
|
}
|
|
|
|
func FetchWantReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGetBook, error) {
|
|
var books []dto.BookUserGetBook
|
|
|
|
query := fetchWantReadUserBookQuery(db, userId)
|
|
query = query.Limit(limit)
|
|
query = query.Offset(offset)
|
|
res := query.Find(&books)
|
|
return books, res.Error
|
|
}
|
|
|
|
func FetchWantReadUserBookCount(db *gorm.DB, userId uint) (int64, error) {
|
|
query := fetchWantReadUserBookQuery(db, userId)
|
|
var count int64
|
|
res := query.Count(&count)
|
|
return count, res.Error
|
|
}
|
|
|
|
func fetchWantReadUserBookQuery(db *gorm.DB, userId uint) *gorm.DB {
|
|
query := fetchUserBookGet(db, userId)
|
|
query = query.Where("user_books.want_read IS TRUE")
|
|
return query
|
|
}
|
|
|
|
func fetchUserBookGet(db *gorm.DB, userId uint) *gorm.DB {
|
|
query := db.Model(&model.UserBook{})
|
|
query = query.Select("books.id, books.title, authors.name as author, user_books.rating, user_books.read, user_books.want_read, " + selectStaticFilesPath())
|
|
query = query.Joins("left join books on (books.id = user_books.book_id)")
|
|
query = joinAuthors(query)
|
|
query = joinStaticFiles(query)
|
|
query = query.Where("user_id = ?", userId)
|
|
return query
|
|
}
|
|
|
|
func selectStaticFilesPath() string {
|
|
return "(CASE COALESCE(static_files.path, '') WHEN '' THEN '' ELSE concat('" + fileutils.GetWsLinkPrefix() + "', static_files.path) END) as CoverPath"
|
|
}
|
|
|
|
func joinAuthors(query *gorm.DB) *gorm.DB {
|
|
return query.Joins("left join authors on (authors.id = books.author_id)")
|
|
}
|
|
|
|
func joinStaticFiles(query *gorm.DB) *gorm.DB {
|
|
return query.Joins("left join static_files on (static_files.id = books.cover_id)")
|
|
}
|