33 lines
700 B
Go
33 lines
700 B
Go
|
package v1
|
||
|
|
||
|
import (
|
||
|
"github.com/labstack/echo"
|
||
|
"net/http"
|
||
|
"git.mowie.cc/konrad/Library/models"
|
||
|
"encoding/json"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func Add(c echo.Context) error {
|
||
|
book := c.FormValue("book")
|
||
|
if book == "" {
|
||
|
return c.JSON(http.StatusBadRequest, models.Message{"No book model provided"})
|
||
|
}
|
||
|
|
||
|
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 = models.AddBook(bookstruct)
|
||
|
|
||
|
if err != nil {
|
||
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
||
|
}
|
||
|
|
||
|
return c.JSON(http.StatusOK, models.Message{"success"})
|
||
|
}
|