57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package v1
|
|
|
|
import (
|
|
"encoding/json"
|
|
"git.mowie.cc/konrad/Library/models"
|
|
"github.com/labstack/echo"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func PublisherUpdate(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"})
|
|
}
|
|
|
|
// Look for the publishers id
|
|
id := c.Param("id")
|
|
|
|
// Make int
|
|
publisherID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get publisher infos"})
|
|
}
|
|
|
|
// Check if the publisher exists
|
|
_, exists, err := models.GetPublisherByID(publisherID)
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get publisher infos"})
|
|
}
|
|
|
|
if !exists {
|
|
return c.JSON(http.StatusBadRequest, models.Message{"The publisher does not exist."})
|
|
}
|
|
|
|
// 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
|
|
newPublisher, err := models.UpdatePublisher(publisherstruct, publisherID)
|
|
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, newPublisher)
|
|
}
|