2017-10-10 19:40:14 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.mowie.cc/konrad/Library/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BookSearch(c echo.Context) error {
|
|
|
|
|
2017-10-10 19:58:55 +00:00
|
|
|
// Prepare the searchterm
|
2017-10-10 19:40:14 +00:00
|
|
|
search := c.QueryParam("s")
|
|
|
|
|
|
|
|
if search == "" {
|
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"Search cannot be empty."})
|
|
|
|
}
|
|
|
|
|
2017-10-10 19:58:55 +00:00
|
|
|
// Get the Books
|
2017-10-10 19:40:14 +00:00
|
|
|
list, err := models.ListBooks(search)
|
|
|
|
|
|
|
|
if err != nil{
|
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting books"})
|
|
|
|
}
|
|
|
|
|
2017-10-10 19:58:55 +00:00
|
|
|
// 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"})
|
|
|
|
}
|
|
|
|
|
2017-10-10 19:40:14 +00:00
|
|
|
return c.JSON(http.StatusOK, list)
|
|
|
|
}
|