package v1

import (
	"github.com/labstack/echo"
	"net/http"

	"git.mowie.cc/konrad/Library/models"
)

func AuthorSearch(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 Authors
	list, err := models.ListAuthors(search)

	if err != nil{
		return c.JSON(http.StatusInternalServerError, models.Message{"Error getting authors"})
	}

	// Check if we have any results
	if len(list) == 0{
		return c.JSON(http.StatusNotFound, models.Message{"Couldn't find any authors matching your search term"})
	}

	return c.JSON(http.StatusOK, list)
}