Split query.go in two files
This commit is contained in:
197
internal/query/querybooks.go
Normal file
197
internal/query/querybooks.go
Normal file
@@ -0,0 +1,197 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.artlef.fr/bibliomane/internal/dto"
|
||||
"git.artlef.fr/bibliomane/internal/fileutils"
|
||||
"git.artlef.fr/bibliomane/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (dto.FullBookGet, error) {
|
||||
var book dto.FullBookGet
|
||||
query := db.Model(&model.Book{})
|
||||
selectQueryString := "books.title, authors.name as author, authors.id as author_id, books.isbn, books.inventaire_id, books.open_library_id, books.short_description, books.summary, " +
|
||||
"user_books.review, 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.BookItemGet, error) {
|
||||
var books []dto.BookItemGet
|
||||
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.BookItemGet, error) {
|
||||
var books []dto.BookItemGet
|
||||
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 AND (user_books.read IS NULL OR user_books.read IS FALSE)")
|
||||
return query
|
||||
}
|
||||
|
||||
func FetchWantReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookItemGet, error) {
|
||||
var books []dto.BookItemGet
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// fetch only books where userbook exists
|
||||
func fetchUserBookGet(db *gorm.DB, userId uint) *gorm.DB {
|
||||
query := db.Model(&model.UserBook{})
|
||||
query = query.Select(selectBookItem())
|
||||
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 FetchAllBooks(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookItemGet, error) {
|
||||
var books []dto.BookItemGet
|
||||
query := fetchBookQueryBuilder(db, userId)
|
||||
query = query.Limit(limit)
|
||||
query = query.Offset(offset)
|
||||
query = query.Order("books.id DESC")
|
||||
res := query.Find(&books)
|
||||
return books, res.Error
|
||||
}
|
||||
|
||||
func FetchAllBooksCount(db *gorm.DB, userId uint) (int64, error) {
|
||||
var count int64
|
||||
query := fetchBookQueryBuilder(db, userId)
|
||||
res := query.Count(&count)
|
||||
return count, res.Error
|
||||
}
|
||||
|
||||
func FetchBookSearchByAuthorGet(db *gorm.DB, userId uint, authorId uint64, limit int, offset int) ([]dto.BookItemGet, error) {
|
||||
var books []dto.BookItemGet
|
||||
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
||||
query = query.Limit(limit)
|
||||
query = query.Offset(offset)
|
||||
res := query.Find(&books)
|
||||
return books, res.Error
|
||||
}
|
||||
|
||||
func FetchBookSearchByAuthorGetCount(db *gorm.DB, userId uint, authorId uint64) (int64, error) {
|
||||
var count int64
|
||||
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
||||
res := query.Count(&count)
|
||||
return count, res.Error
|
||||
}
|
||||
|
||||
func fetchBookSearchByAuthorQuery(db *gorm.DB, userId uint, authorId uint64) *gorm.DB {
|
||||
query := fetchBookQueryBuilder(db, userId)
|
||||
return query.Where("authors.id = ?", authorId)
|
||||
}
|
||||
|
||||
func FetchBookSearchGet(db *gorm.DB, userId uint, searchterm string, limit int, offset int) ([]dto.BookItemGet, error) {
|
||||
var books []dto.BookItemGet
|
||||
query := fetchBookSearchQuery(db, userId, searchterm)
|
||||
query = query.Limit(limit)
|
||||
query = query.Offset(offset)
|
||||
res := query.Find(&books)
|
||||
return books, res.Error
|
||||
}
|
||||
|
||||
func FetchBookSearchGetCount(db *gorm.DB, userId uint, searchterm string) (int64, error) {
|
||||
query := fetchBookSearchQuery(db, userId, searchterm)
|
||||
var count int64
|
||||
res := query.Count(&count)
|
||||
return count, res.Error
|
||||
}
|
||||
|
||||
func fetchBookSearchQuery(db *gorm.DB, userId uint, searchterm string) *gorm.DB {
|
||||
query := fetchBookQueryBuilder(db, userId)
|
||||
isIsbn, _ := regexp.Match(`\d{10,13}`, []byte(searchterm))
|
||||
if isIsbn {
|
||||
query = query.Where("books.isbn = ?", searchterm)
|
||||
} else {
|
||||
query = query.Where("LOWER(books.title) LIKE ?", "%"+strings.ToLower(searchterm)+"%")
|
||||
}
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
// fetch all books even whithout user books
|
||||
func fetchBookQueryBuilder(db *gorm.DB, userId uint) *gorm.DB {
|
||||
query := db.Model(&model.Book{})
|
||||
query = query.Select(selectBookItem())
|
||||
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)
|
||||
return query
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
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)")
|
||||
}
|
||||
Reference in New Issue
Block a user