From ce6c02a1cc43f31767e03acaf32faffe10c272eb Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 10 Oct 2017 18:23:52 +0200 Subject: [PATCH] Implemented Publisher List functions Signed-off-by: kolaente --- models/publisher.go | 6 ++++++ models/publishers_list.go | 11 ++++++++++ routes/api/v1/publisher_show.go | 37 ++++++++++++++++++++++++++++++++ routes/api/v1/publishers_list.go | 18 ++++++++++++++++ routes/routes.go | 2 ++ 5 files changed, 74 insertions(+) create mode 100644 models/publishers_list.go create mode 100644 routes/api/v1/publisher_show.go create mode 100644 routes/api/v1/publishers_list.go diff --git a/models/publisher.go b/models/publisher.go index 02bea31..a385f38 100644 --- a/models/publisher.go +++ b/models/publisher.go @@ -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 } \ No newline at end of file diff --git a/models/publishers_list.go b/models/publishers_list.go new file mode 100644 index 0000000..5556f63 --- /dev/null +++ b/models/publishers_list.go @@ -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 +} \ No newline at end of file diff --git a/routes/api/v1/publisher_show.go b/routes/api/v1/publisher_show.go new file mode 100644 index 0000000..53175d0 --- /dev/null +++ b/routes/api/v1/publisher_show.go @@ -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) + +} diff --git a/routes/api/v1/publishers_list.go b/routes/api/v1/publishers_list.go new file mode 100644 index 0000000..cb9a645 --- /dev/null +++ b/routes/api/v1/publishers_list.go @@ -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) +} \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go index c41fa60..400dfb7 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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)