Library/routes/api/v1/books_search.go

34 lines
723 B
Go

package v1
import (
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
)
// BookSearch is the handler to search for books
func BookSearch(c echo.Context) error {
// Prepare the searchterm
search := c.QueryParam("s")
if search == "" {
return c.JSON(http.StatusBadRequest, models.Message{"Search cannot be empty."})
}
// Get the Books
list, err := models.ListBooks(search)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting books"})
}
// Check if we have any results
if len(list) == 0 {
return c.JSON(http.StatusNotFound, models.Message{"Couldn't find any books matching your search term"})
}
return c.JSON(http.StatusOK, list)
}