2017-10-09 19:06:40 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
|
|
|
"git.mowie.cc/konrad/Library/models"
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-10-10 20:47:54 +00:00
|
|
|
func BookAdd(c echo.Context) error {
|
2017-10-10 07:47:05 +00:00
|
|
|
// Check for Request Content
|
2017-10-09 19:06:40 +00:00
|
|
|
book := c.FormValue("book")
|
|
|
|
if book == "" {
|
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"})
|
|
|
|
}
|
|
|
|
|
2017-10-10 07:47:05 +00:00
|
|
|
// Decode the JSON
|
2017-10-09 19:06:40 +00:00
|
|
|
var bookstruct models.Book
|
|
|
|
dec := json.NewDecoder(strings.NewReader(book))
|
|
|
|
|
|
|
|
err := dec.Decode(&bookstruct)
|
|
|
|
if err != nil {
|
2017-10-11 15:20:56 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding book: " + err.Error()})
|
2017-10-09 19:06:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-10 07:47:05 +00:00
|
|
|
// Insert the book
|
2017-10-09 19:06:40 +00:00
|
|
|
err = models.AddBook(bookstruct)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, models.Message{"success"})
|
|
|
|
}
|