36 lines
812 B
Go
36 lines
812 B
Go
package v1
|
|
|
|
import (
|
|
"encoding/json"
|
|
"git.mowie.cc/konrad/Library/models"
|
|
"github.com/labstack/echo"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func AuthorAdd(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"})
|
|
}
|
|
|
|
// 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.AddAuthor(authorstruct)
|
|
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, newAuthor)
|
|
}
|