package v1

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

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

	// Decode the JSON
	var publisherstruct models.Publisher
	dec := json.NewDecoder(strings.NewReader(publisher))

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

	// Insert the publisher
	err = models.AddPublisher(publisherstruct)

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

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