package v1

import (
	"github.com/labstack/echo"
	"strconv"
	"net/http"
	"git.mowie.cc/konrad/Library/models"
)

func AuthorDelete(c echo.Context) error {

	id := c.Param("id")

	// Make int
	authorID, err := strconv.ParseInt(id, 10, 64)

	if err != nil {
		return c.JSON(http.StatusInternalServerError, models.Message{"Could not get author infos"})
	}

	// Check if the author exists
	_, exists, err := models.GetAuthorByID(authorID)

	if err != nil {
		return c.JSON(http.StatusInternalServerError, models.Message{"Could get author"})
	}

	if !exists {
		return c.JSON(http.StatusBadRequest, models.Message{"The author does not exist."})
	}

	// Delete it
	err = models.DeleteAuthorByID(authorID)

	if err != nil {
		return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete author"})
	}

	return c.JSON(http.StatusOK, models.Message{"success"})
}