Search existing books

This commit is contained in:
2025-10-14 00:29:53 +02:00
parent f72318b5bc
commit bb0ede6abd
13 changed files with 156 additions and 3 deletions

View File

@@ -20,3 +20,8 @@ type bookUserGet struct {
Author string `json:"author" binding:"max=100"`
Rating int `json:"rating" binding:"min=0,max=10"`
}
type bookSearchGet struct {
Title string `json:"title" binding:"required,max=300"`
Author string `json:"author" binding:"max=100"`
}

View File

@@ -33,3 +33,10 @@ func (u userSignup) toUser() (model.User, error) {
user.Password = string(hashedPassword)
return user, nil
}
func fromBookDb(b *model.Book) bookSearchGet {
return bookSearchGet{
Title: b.Title,
Author: b.Author,
}
}

View File

@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"strings"
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
"git.artlef.fr/PersonalLibraryManager/internal/i18nresource"
@@ -23,13 +24,24 @@ func GetMyBooksHanderl(ac appcontext.AppContext) {
return
}
ac.Db.Preload("Book").Where("user_id = ?", user.ID).Find(&userbooks)
var booksDto []bookUserGet
booksDto := make([]bookUserGet, 0)
for _, userbook := range userbooks {
booksDto = append(booksDto, fromUserBookDb(&userbook))
}
ac.C.JSON(http.StatusOK, booksDto)
}
func GetSearchBooksHandler(ac appcontext.AppContext) {
searchterm := ac.C.Param("searchterm")
var booksDb []model.Book
ac.Db.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(searchterm)+"%").Find(&booksDb)
books := make([]bookSearchGet, 0)
for _, b := range booksDb {
books = append(books, fromBookDb(&b))
}
ac.C.JSON(http.StatusOK, books)
}
func PostBookHandler(ac appcontext.AppContext) {
var book bookPostCreate
err := ac.C.ShouldBindJSON(&book)