Library/routes/api/v1/book_show.go

38 lines
844 B
Go
Raw Normal View History

package v1
import (
2018-03-05 11:53:12 +00:00
"git.kolaente.de/konrad/Library/models"
"github.com/labstack/echo"
"net/http"
"strconv"
)
2017-11-08 09:55:17 +00:00
// BookShow is the handler to show informations about a book
func BookShow(c echo.Context) error {
book := c.Param("id")
if book == "" {
return c.JSON(http.StatusBadRequest, models.Message{"Book id cannot be empty."})
}
// Make int
bookID, err := strconv.ParseInt(book, 10, 64)
if err != nil {
return c.JSON(http.StatusBadRequest, models.Message{"Book ID is invalid."})
}
// Get book infos
2017-11-08 09:57:38 +00:00
bookInfo, exists, err := models.GetBookByID(bookID)
if err != nil {
2018-01-15 12:20:58 +00:00
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book infos."})
}
// Check if it exists
if !exists {
return c.JSON(http.StatusNotFound, models.Message{"Book not found."})
}
return c.JSON(http.StatusOK, bookInfo)
2017-11-07 15:35:10 +00:00
}