Backend query module: merge two files
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package query
|
package query
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.artlef.fr/bibliomane/internal/dto"
|
"git.artlef.fr/bibliomane/internal/dto"
|
||||||
"git.artlef.fr/bibliomane/internal/fileutils"
|
"git.artlef.fr/bibliomane/internal/fileutils"
|
||||||
"git.artlef.fr/bibliomane/internal/model"
|
"git.artlef.fr/bibliomane/internal/model"
|
||||||
@@ -100,6 +103,64 @@ func fetchUserBookGet(db *gorm.DB, userId uint) *gorm.DB {
|
|||||||
return query
|
return query
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FetchBookSearchByAuthorGet(db *gorm.DB, userId uint, authorId uint64, limit int, offset int) ([]dto.BookSearchGetBook, error) {
|
||||||
|
var books []dto.BookSearchGetBook
|
||||||
|
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
||||||
|
query = query.Limit(limit)
|
||||||
|
query = query.Offset(offset)
|
||||||
|
res := query.Find(&books)
|
||||||
|
return books, res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func FetchBookSearchByAuthorGetCount(db *gorm.DB, userId uint, authorId uint64) (int64, error) {
|
||||||
|
var count int64
|
||||||
|
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
||||||
|
res := query.Count(&count)
|
||||||
|
return count, res.Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func fetchBookSearchByAuthorQuery(db *gorm.DB, userId uint, authorId uint64) *gorm.DB {
|
||||||
|
query := fetchBookQueryBuilder(db, userId)
|
||||||
|
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
|
||||||
|
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, 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, userId uint, searchterm string) *gorm.DB {
|
||||||
|
query := fetchBookQueryBuilder(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 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, 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
|
||||||
|
}
|
||||||
|
|
||||||
func selectStaticFilesPath() string {
|
func selectStaticFilesPath() string {
|
||||||
return "(CASE COALESCE(static_files.path, '') WHEN '' THEN '' ELSE concat('" + fileutils.GetWsLinkPrefix() + "', static_files.path) END) as CoverPath"
|
return "(CASE COALESCE(static_files.path, '') WHEN '' THEN '' ELSE concat('" + fileutils.GetWsLinkPrefix() + "', static_files.path) END) as CoverPath"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,68 +0,0 @@
|
|||||||
package query
|
|
||||||
|
|
||||||
import (
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.artlef.fr/bibliomane/internal/dto"
|
|
||||||
"git.artlef.fr/bibliomane/internal/model"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
func FetchBookSearchByAuthorGet(db *gorm.DB, userId uint, authorId uint64, limit int, offset int) ([]dto.BookSearchGetBook, error) {
|
|
||||||
var books []dto.BookSearchGetBook
|
|
||||||
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
|
||||||
query = query.Limit(limit)
|
|
||||||
query = query.Offset(offset)
|
|
||||||
res := query.Find(&books)
|
|
||||||
return books, res.Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func FetchBookSearchByAuthorGetCount(db *gorm.DB, userId uint, authorId uint64) (int64, error) {
|
|
||||||
var count int64
|
|
||||||
query := fetchBookSearchByAuthorQuery(db, userId, authorId)
|
|
||||||
res := query.Count(&count)
|
|
||||||
return count, res.Error
|
|
||||||
}
|
|
||||||
|
|
||||||
func fetchBookSearchByAuthorQuery(db *gorm.DB, userId uint, authorId uint64) *gorm.DB {
|
|
||||||
query := fetchBookSearchQueryBuilder(db, userId)
|
|
||||||
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
|
|
||||||
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, 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, userId uint, searchterm string) *gorm.DB {
|
|
||||||
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, books.small_description as description, books.inventaire_id, 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