Display read/wantread in search view

This commit is contained in:
2025-11-18 17:34:34 +01:00
parent 81d0be3a77
commit 59f6635f1b
5 changed files with 71 additions and 20 deletions

View File

@@ -30,33 +30,37 @@ func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) {
}
type BookSearchGet struct {
ID uint `json:"id"`
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
ID uint `json:"id"`
Rating int `json:"rating"`
Read bool `json:"read"`
WantRead bool `json:"wantread"`
CoverPath string `json:"coverPath"`
}
func FetchBookSearchGet(db *gorm.DB, searchterm string, limit int, offset int) ([]BookSearchGet, error) {
func FetchBookSearchGet(db *gorm.DB, userId uint, searchterm string, limit int, offset int) ([]BookSearchGet, error) {
var books []BookSearchGet
query := fetchBookSearchQuery(db, searchterm)
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, searchterm string) (int64, error) {
query := fetchBookSearchQuery(db, searchterm)
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, searchterm string) *gorm.DB {
func fetchBookSearchQuery(db *gorm.DB, userId uint, searchterm string) *gorm.DB {
query := db.Model(&model.Book{})
query = query.Select("books.title, books.author, books.id," + selectStaticFilesPath())
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(title) LIKE ?", "%"+strings.ToLower(searchterm)+"%")
query = query.Where("LOWER(books.title) LIKE ?", "%"+strings.ToLower(searchterm)+"%")
return query
}