package v1

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

func BookDelete(c echo.Context) error {

	id := c.Param("id")

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

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

	// Check if the book exists
	_, exists, err := models.GetBookById(bookID)

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

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

	// Delete it
	err = models.DeleteBookByID(bookID)

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

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