57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package v1
|
|
|
|
import (
|
|
"github.com/labstack/echo"
|
|
"net/http"
|
|
"git.mowie.cc/konrad/Library/models"
|
|
"encoding/json"
|
|
"strings"
|
|
"strconv"
|
|
)
|
|
|
|
func AuthorUpdate(c echo.Context) error {
|
|
// Check for Request Content
|
|
author := c.FormValue("author")
|
|
if author == "" {
|
|
return c.JSON(http.StatusBadRequest, models.Message{"No author model provided"})
|
|
}
|
|
|
|
// Look for the authors id
|
|
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 not get author infos"})
|
|
}
|
|
|
|
if !exists {
|
|
return c.JSON(http.StatusBadRequest, models.Message{"The author does not exist."})
|
|
}
|
|
|
|
// Decode the JSON
|
|
var authorstruct models.Author
|
|
dec := json.NewDecoder(strings.NewReader(author))
|
|
|
|
err = dec.Decode(&authorstruct)
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding author: " + err.Error()})
|
|
}
|
|
|
|
// Insert the author
|
|
newAuthor, err := models.UpdateAuthor(authorstruct, authorID)
|
|
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, newAuthor)
|
|
}
|