35 lines
813 B
Go
35 lines
813 B
Go
package routes
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"git.artlef.fr/PersonalLibraryManager/internal/appcontext"
|
|
"git.artlef.fr/PersonalLibraryManager/internal/model"
|
|
)
|
|
|
|
type bookSearchGet struct {
|
|
Title string `json:"title" binding:"required,max=300"`
|
|
Author string `json:"author" binding:"max=100"`
|
|
ID uint `json:"id"`
|
|
}
|
|
|
|
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, bookDbToWs(&b))
|
|
}
|
|
ac.C.JSON(http.StatusOK, books)
|
|
}
|
|
|
|
func bookDbToWs(b *model.Book) bookSearchGet {
|
|
return bookSearchGet{
|
|
Title: b.Title,
|
|
Author: b.Author,
|
|
ID: b.ID,
|
|
}
|
|
}
|