2017-10-11 15:20:56 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2017-11-07 15:35:10 +00:00
|
|
|
"encoding/json"
|
|
|
|
"git.mowie.cc/konrad/Library/models"
|
2017-10-11 15:20:56 +00:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-11-08 09:55:17 +00:00
|
|
|
// AuthorAdd is the handler to add an author
|
2017-10-11 15:20:56 +00:00
|
|
|
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
|
2017-10-11 16:17:51 +00:00
|
|
|
newAuthor, err := models.AddAuthor(authorstruct)
|
2017-10-11 15:20:56 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
|
|
|
}
|
|
|
|
|
2017-10-11 16:17:51 +00:00
|
|
|
return c.JSON(http.StatusOK, newAuthor)
|
2017-10-11 15:20:56 +00:00
|
|
|
}
|