package v1

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

func BookAdd(c echo.Context) error {
	// Check for Request Content
	book := c.FormValue("book")
	if book == "" {
		return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"})
	}

	// Decode the JSON
	var bookstruct models.Book
	dec := json.NewDecoder(strings.NewReader(book))

	err := dec.Decode(&bookstruct)
	if err != nil {
		return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding book: " + err.Error()})
	}

	// Insert the book
	newBook, err := models.AddBook(bookstruct)

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

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