Library/routes/api/v1/publishers_search.go

34 lines
757 B
Go

package v1
import (
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
)
// PublisherSearch is the handler to search for a publisher
func PublisherSearch(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 Publishers
list, err := models.ListPublishers(search)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publisher"})
}
// Check if we have any results
if len(list) == 0 {
return c.JSON(http.StatusNotFound, models.Message{"Couldn't find any publisher matching your search term"})
}
return c.JSON(http.StatusOK, list)
}