This commit is contained in:
parent
cb3a37b483
commit
c82068b991
|
@ -1,32 +1,25 @@
|
|||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"git.mowie.cc/konrad/Library/models"
|
||||
"github.com/labstack/echo"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type bookPayload struct {
|
||||
Book models.Book `json:"book" form:"book"`
|
||||
}
|
||||
|
||||
// BookAdd is the handler to add a book
|
||||
func BookAdd(c echo.Context) error {
|
||||
// Check for Request Content
|
||||
book := c.FormValue("book")
|
||||
if book == "" {
|
||||
b := new(bookPayload)
|
||||
if err := c.Bind(b); err != nil {
|
||||
return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"})
|
||||
}
|
||||
|
||||
// Decode the JSON
|
||||
var bookstruct models.Book
|
||||
dec := json.NewDecoder(strings.NewReader(book))
|
||||
|
||||
err := dec.Decode(&bookstruct)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error decoding book: " + err.Error()})
|
||||
}
|
||||
|
||||
// Insert the book
|
||||
newBook, err := models.AddBook(bookstruct)
|
||||
newBook, err := models.AddBook(b.Book)
|
||||
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
||||
|
|
Loading…
Reference in New Issue
Block a user