From fa49f276fe63d0c9520c6a5527d6e052cc49911e Mon Sep 17 00:00:00 2001 From: Arthur Lefebvre Date: Mon, 24 Nov 2025 15:09:50 +0100 Subject: [PATCH] Move search query to another file --- internal/query/query.go | 37 ---------------------------------- internal/query/search.go | 43 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 internal/query/search.go diff --git a/internal/query/query.go b/internal/query/query.go index aafc0ee..2c8a0fd 100644 --- a/internal/query/query.go +++ b/internal/query/query.go @@ -1,8 +1,6 @@ package query import ( - "strings" - "git.artlef.fr/PersonalLibraryManager/internal/fileutils" "git.artlef.fr/PersonalLibraryManager/internal/model" "gorm.io/gorm" @@ -37,41 +35,6 @@ func FetchBookGet(db *gorm.DB, userId uint, bookId uint64) (BookGet, error) { return book, res.Error } -type BookSearchGet struct { - ID uint `json:"id"` - Title string `json:"title" binding:"required,max=300"` - Author string `json:"author" binding:"max=100"` - Rating int `json:"rating"` - Read bool `json:"read"` - WantRead bool `json:"wantread"` - CoverPath string `json:"coverPath"` -} - -func FetchBookSearchGet(db *gorm.DB, userId uint, searchterm string, limit int, offset int) ([]BookSearchGet, error) { - var books []BookSearchGet - 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 := 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)+"%") - return query -} - type BookUserGet struct { ID uint `json:"id"` Title string `json:"title" binding:"required,max=300"` diff --git a/internal/query/search.go b/internal/query/search.go new file mode 100644 index 0000000..4411289 --- /dev/null +++ b/internal/query/search.go @@ -0,0 +1,43 @@ +package query + +import ( + "strings" + + "git.artlef.fr/PersonalLibraryManager/internal/model" + "gorm.io/gorm" +) + +type BookSearchGet struct { + ID uint `json:"id"` + Title string `json:"title" binding:"required,max=300"` + Author string `json:"author" binding:"max=100"` + Rating int `json:"rating"` + Read bool `json:"read"` + WantRead bool `json:"wantread"` + CoverPath string `json:"coverPath"` +} + +func FetchBookSearchGet(db *gorm.DB, userId uint, searchterm string, limit int, offset int) ([]BookSearchGet, error) { + var books []BookSearchGet + 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 := 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)+"%") + return query +}