Implemented Publisher List functions

Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
konrad 2017-10-10 18:23:52 +02:00 committed by kolaente
parent 6ce0c553d4
commit ce6c02a1cc
5 changed files with 74 additions and 0 deletions

View File

@ -9,4 +9,10 @@ type Publisher struct {
func (Publisher) TableName() string {
return "publishers"
}
func GetPublisherByID(id int64) (publisher Publisher, exists bool, err error) {
has, err := x.Id(id).Get(&publisher)
return publisher, has, err
}

11
models/publishers_list.go Normal file
View File

@ -0,0 +1,11 @@
package models
func ListPublishers() (publishers []Publisher, err error) {
err = x.Find(&publishers)
if err != nil {
return []Publisher{}, err
}
return publishers, nil
}

View File

@ -0,0 +1,37 @@
package v1
import (
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"strconv"
)
func PublisherShow(c echo.Context) error {
publisher := c.Param("id")
if publisher == "" {
return c.JSON(http.StatusBadRequest, models.Message{"Publisher ID cannot be empty."})
}
// Make int
publisherID, err := strconv.ParseInt(publisher, 10, 64)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publisher infos."})
}
// Get Publisher Infos
publisherInfos, exists, err := models.GetPublisherByID(publisherID)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publisher infos."})
}
// Check if it exists
if !exists {
return c.JSON(http.StatusNotFound, models.Message{"Publisher not found."})
}
return c.JSON(http.StatusOK, publisherInfos)
}

View File

@ -0,0 +1,18 @@
package v1
import (
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
)
func PublishersList(c echo.Context) error {
list, err := models.ListPublishers()
if err != nil{
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publishers"})
}
return c.JSON(http.StatusOK, list)
}

View File

@ -35,6 +35,8 @@ func RegisterRoutes(e *echo.Echo) {
a.GET("/authors/:id", apiv1.AuthorShow)
// Lookup Publishers
a.GET("/publishers/list", apiv1.PublishersList)
a.GET("/publishers/:id", apiv1.PublisherShow)
// Login Route
e.POST("/login", Login)