author is now on separate table
This commit is contained in:
@@ -22,14 +22,15 @@ type BookGet struct {
|
||||
func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) {
|
||||
var book BookGet
|
||||
query := db.Model(&model.Book{})
|
||||
selectQueryString := "books.title, books.author, books.isbn, books.summary, " +
|
||||
selectQueryString := "books.title, authors.name as author, books.isbn, 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 = query.Joins("left join static_files on (static_files.id = books.cover_id)")
|
||||
query = joinStaticFiles(query)
|
||||
query = query.Where("books.id = ?", bookId)
|
||||
res := query.First(&book)
|
||||
return book, res.Error
|
||||
@@ -113,9 +114,10 @@ func fetchWantReadUserBookQuery(db *gorm.DB, userId uint) *gorm.DB {
|
||||
|
||||
func fetchUserBookGet(db *gorm.DB, userId uint) *gorm.DB {
|
||||
query := db.Model(&model.UserBook{})
|
||||
query = query.Select("books.id, books.title, books.author, user_books.rating, user_books.read, user_books.want_read, " + selectStaticFilesPath())
|
||||
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 = query.Joins("left join static_files on (static_files.id = books.cover_id)")
|
||||
query = joinAuthors(query)
|
||||
query = joinStaticFiles(query)
|
||||
query = query.Where("user_id = ?", userId)
|
||||
return query
|
||||
}
|
||||
@@ -123,3 +125,11 @@ func fetchUserBookGet(db *gorm.DB, userId uint) *gorm.DB {
|
||||
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)")
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
||||
@@ -34,10 +35,22 @@ func FetchBookSearchGetCount(db *gorm.DB, userId uint, searchterm string) (int64
|
||||
}
|
||||
|
||||
func fetchBookSearchQuery(db *gorm.DB, userId uint, searchterm string) *gorm.DB {
|
||||
query := db.Model(&model.Book{})
|
||||
query = query.Select("books.id, books.title, books.author, user_books.rating, user_books.read, user_books.want_read, " + selectStaticFilesPath())
|
||||
query = query.Joins("left join user_books on (user_books.book_id = books.id and user_books.user_id = ?)", userId)
|
||||
query = query.Joins("left join static_files on (static_files.id = books.cover_id)")
|
||||
query = query.Where("LOWER(books.title) LIKE ?", "%"+strings.ToLower(searchterm)+"%")
|
||||
query := fetchBookSearchQueryBuilder(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
|
||||
}
|
||||
|
||||
func fetchBookSearchQueryBuilder(db *gorm.DB, userId uint) *gorm.DB {
|
||||
query := db.Model(&model.Book{})
|
||||
query = query.Select("books.id, books.title, authors.name as author, user_books.rating, user_books.read, user_books.want_read, " + selectStaticFilesPath())
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user