Use the same widget for books everywhere
This commit is contained in:
@@ -10,8 +10,8 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (dto.BookGet, error) {
|
||||
var book dto.BookGet
|
||||
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.summary, " +
|
||||
"user_books.review, user_books.rating, user_books.read, user_books.want_read, " +
|
||||
@@ -27,8 +27,8 @@ func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (dto.BookGet, error)
|
||||
return book, res.Error
|
||||
}
|
||||
|
||||
func FetchReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGetBook, error) {
|
||||
var books []dto.BookUserGetBook
|
||||
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)
|
||||
@@ -43,8 +43,8 @@ func FetchReadUserBookCount(db *gorm.DB, userId uint) (int64, error) {
|
||||
return count, res.Error
|
||||
}
|
||||
|
||||
func FetchReadingUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGetBook, error) {
|
||||
var books []dto.BookUserGetBook
|
||||
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)
|
||||
@@ -70,8 +70,8 @@ func fetchReadingUserBookQuery(db *gorm.DB, userId uint) *gorm.DB {
|
||||
return query
|
||||
}
|
||||
|
||||
func FetchWantReadUserBook(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookUserGetBook, error) {
|
||||
var books []dto.BookUserGetBook
|
||||
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)
|
||||
@@ -93,9 +93,10 @@ func fetchWantReadUserBookQuery(db *gorm.DB, userId uint) *gorm.DB {
|
||||
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("books.id, books.title, authors.name as author, user_books.rating, user_books.read, user_books.want_read, " + selectStaticFilesPath())
|
||||
query = query.Select(selectBookItem())
|
||||
query = query.Joins("left join books on (books.id = user_books.book_id)")
|
||||
query = joinAuthors(query)
|
||||
query = joinStaticFiles(query)
|
||||
@@ -103,8 +104,8 @@ func fetchUserBookGet(db *gorm.DB, userId uint) *gorm.DB {
|
||||
return query
|
||||
}
|
||||
|
||||
func FetchAllBooks(db *gorm.DB, userId uint, limit int, offset int) ([]dto.BookSearchGetBook, error) {
|
||||
var books []dto.BookSearchGetBook
|
||||
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)
|
||||
@@ -120,8 +121,8 @@ func FetchAllBooksCount(db *gorm.DB, userId uint) (int64, error) {
|
||||
return count, res.Error
|
||||
}
|
||||
|
||||
func FetchBookSearchByAuthorGet(db *gorm.DB, userId uint, authorId uint64, limit int, offset int) ([]dto.BookSearchGetBook, error) {
|
||||
var books []dto.BookSearchGetBook
|
||||
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)
|
||||
@@ -141,8 +142,8 @@ func fetchBookSearchByAuthorQuery(db *gorm.DB, userId uint, authorId uint64) *go
|
||||
return query.Where("authors.id = ?", authorId)
|
||||
}
|
||||
|
||||
func FetchBookSearchGet(db *gorm.DB, userId uint, searchterm string, limit int, offset int) ([]dto.BookSearchGetBook, error) {
|
||||
var books []dto.BookSearchGetBook
|
||||
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)
|
||||
@@ -169,15 +170,20 @@ func fetchBookSearchQuery(db *gorm.DB, userId uint, searchterm string) *gorm.DB
|
||||
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("books.id, books.title, authors.name as author, books.small_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())
|
||||
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.small_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"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user