Search existing books
This commit is contained in:
@@ -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"`
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user