From 2e360becca7d007732d69672b4a0fadac292a4f0 Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 10 Oct 2017 21:58:55 +0200 Subject: [PATCH] implemented authors and publishers search Signed-off-by: kolaente --- models/author.go | 2 +- models/authors_list.go | 12 +++++++++-- models/publishers_list.go | 11 ++++++++-- routes/api/v1/authors_list.go | 2 +- routes/api/v1/authors_search.go | 32 ++++++++++++++++++++++++++++++ routes/api/v1/books_search.go | 7 +++++++ routes/api/v1/publishers_list.go | 2 +- routes/api/v1/publishers_search.go | 32 ++++++++++++++++++++++++++++++ routes/routes.go | 2 ++ 9 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 routes/api/v1/authors_search.go create mode 100644 routes/api/v1/publishers_search.go diff --git a/models/author.go b/models/author.go index 94ed707..35c6088 100644 --- a/models/author.go +++ b/models/author.go @@ -3,7 +3,7 @@ package models type Author struct { ID int64 `xorm:"int(11) autoincr not null unique pk"` Forename string `xorm:"varchar(250)"` - Lastame string `xorm:"varchar(250) not null"` + Lastname string `xorm:"varchar(250) not null"` Created int64 `xorm:"created"` Updated int64 `xorm:"updated"` } diff --git a/models/authors_list.go b/models/authors_list.go index 767da2a..8d8ef85 100644 --- a/models/authors_list.go +++ b/models/authors_list.go @@ -1,8 +1,16 @@ package models -func ListAuthors() (authors []Author, err error) { - err = x.Find(&authors) +func ListAuthors(searchterm string) (authors []Author, err error) { + + if searchterm == "" { + err = x.Find(&authors) + } else { + err = x. + Where("forename LIKE ?", "%"+searchterm+"%"). + Or("lastname LIKE ?", "%"+searchterm+"%"). + Find(&authors) + } if err != nil { return []Author{}, err } diff --git a/models/publishers_list.go b/models/publishers_list.go index 5556f63..77f6197 100644 --- a/models/publishers_list.go +++ b/models/publishers_list.go @@ -1,7 +1,14 @@ package models -func ListPublishers() (publishers []Publisher, err error) { - err = x.Find(&publishers) +func ListPublishers(searchterm string) (publishers []Publisher, err error) { + + if searchterm == "" { + err = x.Find(&publishers) + } else { + err = x. + Where("name LIKE ?", "%" + searchterm + "%"). + Find(&publishers) + } if err != nil { return []Publisher{}, err diff --git a/routes/api/v1/authors_list.go b/routes/api/v1/authors_list.go index 9c77609..46f5755 100644 --- a/routes/api/v1/authors_list.go +++ b/routes/api/v1/authors_list.go @@ -8,7 +8,7 @@ import ( func AuthorsList(c echo.Context) error { - list, err := models.ListAuthors() + list, err := models.ListAuthors("") if err != nil{ return c.JSON(http.StatusInternalServerError, models.Message{"Error getting authors"}) diff --git a/routes/api/v1/authors_search.go b/routes/api/v1/authors_search.go new file mode 100644 index 0000000..1aacf1b --- /dev/null +++ b/routes/api/v1/authors_search.go @@ -0,0 +1,32 @@ +package v1 + +import ( + "github.com/labstack/echo" + "net/http" + + "git.mowie.cc/konrad/Library/models" +) + +func AuthorSearch(c echo.Context) error { + + // Prepare the searchterm + search := c.QueryParam("s") + + if search == "" { + return c.JSON(http.StatusBadRequest, models.Message{"Search cannot be empty."}) + } + + // Get the Authors + list, err := models.ListAuthors(search) + + if err != nil{ + return c.JSON(http.StatusInternalServerError, models.Message{"Error getting authors"}) + } + + // Check if we have any results + if len(list) == 0{ + return c.JSON(http.StatusNotFound, models.Message{"Couldn't find any authors matching your search term"}) + } + + return c.JSON(http.StatusOK, list) +} \ No newline at end of file diff --git a/routes/api/v1/books_search.go b/routes/api/v1/books_search.go index 2bc48bd..c588203 100644 --- a/routes/api/v1/books_search.go +++ b/routes/api/v1/books_search.go @@ -9,17 +9,24 @@ import ( func BookSearch(c echo.Context) error { + // Prepare the searchterm search := c.QueryParam("s") if search == "" { return c.JSON(http.StatusBadRequest, models.Message{"Search cannot be empty."}) } + // Get the Books list, err := models.ListBooks(search) if err != nil{ return c.JSON(http.StatusInternalServerError, models.Message{"Error getting books"}) } + // Check if we have any results + if len(list) == 0{ + return c.JSON(http.StatusNotFound, models.Message{"Couldn't find any books matching your search term"}) + } + return c.JSON(http.StatusOK, list) } \ No newline at end of file diff --git a/routes/api/v1/publishers_list.go b/routes/api/v1/publishers_list.go index cb9a645..60dc668 100644 --- a/routes/api/v1/publishers_list.go +++ b/routes/api/v1/publishers_list.go @@ -8,7 +8,7 @@ import ( func PublishersList(c echo.Context) error { - list, err := models.ListPublishers() + list, err := models.ListPublishers("") if err != nil{ return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publishers"}) diff --git a/routes/api/v1/publishers_search.go b/routes/api/v1/publishers_search.go new file mode 100644 index 0000000..1398e90 --- /dev/null +++ b/routes/api/v1/publishers_search.go @@ -0,0 +1,32 @@ +package v1 + +import ( + "github.com/labstack/echo" + "net/http" + + "git.mowie.cc/konrad/Library/models" +) + +func PublisherSearch(c echo.Context) error { + + // Prepare the searchterm + search := c.QueryParam("s") + + if search == "" { + return c.JSON(http.StatusBadRequest, models.Message{"Search cannot be empty."}) + } + + // Get the Publishers + list, err := models.ListPublishers(search) + + if err != nil{ + return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publisher"}) + } + + // Check if we have any results + if len(list) == 0{ + return c.JSON(http.StatusNotFound, models.Message{"Couldn't find any publisher matching your search term"}) + } + + return c.JSON(http.StatusOK, list) +} \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go index 1fe4505..82eb2f1 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -34,10 +34,12 @@ func RegisterRoutes(e *echo.Echo) { // Lookup Authors a.GET("/authors/list", apiv1.AuthorsList) a.GET("/authors/:id", apiv1.AuthorShow) + a.GET("/authors/search", apiv1.AuthorSearch) // Lookup Publishers a.GET("/publishers/list", apiv1.PublishersList) a.GET("/publishers/:id", apiv1.PublisherShow) + a.GET("/publishers/search", apiv1.PublisherSearch) // Login Route e.POST("/login", Login)