70 lines
2.6 KiB
Go
70 lines
2.6 KiB
Go
package query
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/fileutils"
|
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type BookGet struct {
|
|
Title string `json:"title" binding:"required,max=300"`
|
|
Author string `json:"author" binding:"max=100"`
|
|
Rating int `json:"rating"`
|
|
Read bool `json:"read"`
|
|
CoverPath string `json:"coverPath"`
|
|
}
|
|
|
|
func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) {
|
|
var book BookGet
|
|
query := db.Model(&model.Book{})
|
|
query = query.Select("books.title, books.author, user_books.rating, user_books.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("books.id = ?", bookId)
|
|
res := query.First(&book)
|
|
return book, res.Error
|
|
}
|
|
|
|
type BookSearchGet struct {
|
|
Title string `json:"title" binding:"required,max=300"`
|
|
Author string `json:"author" binding:"max=100"`
|
|
ID uint `json:"id"`
|
|
CoverPath string `json:"coverPath"`
|
|
}
|
|
|
|
func FetchBookSearchGet(db *gorm.DB, searchterm string) ([]BookSearchGet, error) {
|
|
var books []BookSearchGet
|
|
query := db.Model(&model.Book{})
|
|
query = query.Select("books.title, books.author, books.id," + selectStaticFilesPath())
|
|
query = query.Joins("left join static_files on (static_files.id = books.cover_id)")
|
|
query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(searchterm)+"%")
|
|
res := query.Find(&books)
|
|
return books, res.Error
|
|
}
|
|
|
|
type BookUserGet struct {
|
|
ID uint `json:"id"`
|
|
Title string `json:"title" binding:"required,max=300"`
|
|
Author string `json:"author" binding:"max=100"`
|
|
Rating int `json:"rating" binding:"min=0,max=10"`
|
|
Read bool `json:"read" binding:"boolean"`
|
|
CoverPath string `json:"coverPath"`
|
|
}
|
|
|
|
func FetchBookUserGet(db *gorm.DB, userId uint) ([]BookUserGet, error) {
|
|
var books []BookUserGet
|
|
query := db.Model(&model.UserBook{})
|
|
query = query.Select("books.id, books.title, books.author, user_books.rating, user_books.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 = query.Where("user_id = ?", userId)
|
|
res := query.Find(&books)
|
|
return books, res.Error
|
|
}
|
|
|
|
func selectStaticFilesPath() string {
|
|
return "(CASE COALESCE(static_files.path, '') WHEN '' THEN '' ELSE concat('" + fileutils.GetWsLinkPrefix() + "', static_files.path) END) as CoverPath"
|
|
}
|