author is now on separate table
This commit is contained in:
@@ -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