2017-10-09 19:06:40 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2017-11-16 18:07:32 +00:00
|
|
|
"encoding/json"
|
2017-11-07 15:35:10 +00:00
|
|
|
"git.mowie.cc/konrad/Library/models"
|
2017-10-09 19:06:40 +00:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
2017-11-28 14:29:51 +00:00
|
|
|
"strconv"
|
2017-11-28 15:04:14 +00:00
|
|
|
"strings"
|
2017-10-09 19:06:40 +00:00
|
|
|
)
|
|
|
|
|
2017-11-21 10:38:52 +00:00
|
|
|
// BookAddOrUpdate is the handler to add a book
|
|
|
|
func BookAddOrUpdate(c echo.Context) error {
|
2017-10-10 07:47:05 +00:00
|
|
|
// Check for Request Content
|
2017-11-16 18:07:32 +00:00
|
|
|
bookFromString := c.FormValue("book")
|
2017-12-07 15:17:18 +00:00
|
|
|
var datBook *models.Book
|
2017-11-16 18:07:32 +00:00
|
|
|
|
|
|
|
if bookFromString == "" {
|
2017-12-07 15:17:18 +00:00
|
|
|
if err := c.Bind(&datBook); err != nil {
|
2018-01-15 12:20:58 +00:00
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"No book model provided."})
|
2017-11-16 18:07:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Decode the JSON
|
|
|
|
dec := json.NewDecoder(strings.NewReader(bookFromString))
|
2017-11-28 14:29:51 +00:00
|
|
|
err := dec.Decode(&datBook)
|
2017-11-16 18:07:32 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-15 12:08:17 +00:00
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"Error decoding book: " + err.Error()})
|
2017-11-16 18:07:32 +00:00
|
|
|
}
|
2017-10-09 19:06:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 14:29:51 +00:00
|
|
|
// Check if we have an ID other than the one in the struct
|
|
|
|
id := c.Param("id")
|
|
|
|
if id != "" {
|
|
|
|
// Make int
|
|
|
|
bookID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-01-15 12:20:58 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book id."})
|
2017-11-28 14:29:51 +00:00
|
|
|
}
|
|
|
|
datBook.ID = bookID
|
|
|
|
}
|
|
|
|
|
2018-01-15 12:08:17 +00:00
|
|
|
// Check if the book exists
|
|
|
|
if datBook.ID != 0 {
|
|
|
|
_, exists, err := models.GetBookByID(datBook.ID)
|
|
|
|
if err != nil {
|
2018-01-15 12:20:58 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not check if the book exists."})
|
2018-01-15 12:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2018-01-15 12:20:58 +00:00
|
|
|
return c.JSON(http.StatusNotFound, models.Message{"The book does not exist."})
|
2018-01-15 12:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 09:33:37 +00:00
|
|
|
// Check if we have at least a title
|
2017-11-29 14:59:34 +00:00
|
|
|
if datBook.Title == "" && datBook.ID == 0 {
|
2017-11-21 09:33:37 +00:00
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"You need at least a title to insert a new book!"})
|
|
|
|
}
|
|
|
|
|
2017-11-28 14:29:51 +00:00
|
|
|
// Insert or update the book
|
2017-12-07 15:17:18 +00:00
|
|
|
newBook, err := models.AddOrUpdateBook(*datBook)
|
2017-10-09 19:06:40 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-01-24 11:58:00 +00:00
|
|
|
if models.IsErrAuthorCannotBeEmpty(err) {
|
2018-01-24 12:15:28 +00:00
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"Id cannot be 0."})
|
2018-01-24 11:58:00 +00:00
|
|
|
}
|
2018-01-24 12:13:55 +00:00
|
|
|
|
|
|
|
if models.IsErrBookTitleCannotBeEmpty(err) {
|
2018-01-24 22:55:10 +00:00
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"You need to provide at least a title for the book."})
|
|
|
|
}
|
|
|
|
|
|
|
|
if models.IsErrNoPublisherName(err) {
|
|
|
|
return c.JSON(http.StatusBadRequest, models.Message{"You need to provide at least a name to insert a new publisher."})
|
2018-01-24 12:13:55 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 19:06:40 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
|
|
|
|
}
|
|
|
|
|
2017-12-05 13:46:54 +00:00
|
|
|
// Log the action
|
|
|
|
err = models.LogAction("Added or updated a book", newBook.ID, c)
|
|
|
|
if err != nil {
|
2018-01-15 12:20:58 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log."})
|
2017-12-05 13:46:54 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 16:17:51 +00:00
|
|
|
return c.JSON(http.StatusOK, newBook)
|
2017-10-09 19:06:40 +00:00
|
|
|
}
|