From c93b2f433c48fdb44a66f81be065136631bdec35 Mon Sep 17 00:00:00 2001 From: konrad Date: Wed, 29 Nov 2017 15:22:25 +0100 Subject: [PATCH] Optimized search route --- routes/api/v1/authors_list.go | 5 ++++- routes/api/v1/authors_search.go | 33 ------------------------------ routes/api/v1/books_list.go | 5 ++++- routes/api/v1/books_search.go | 33 ------------------------------ routes/api/v1/publishers_list.go | 5 ++++- routes/api/v1/publishers_search.go | 33 ------------------------------ routes/routes.go | 3 --- 7 files changed, 12 insertions(+), 105 deletions(-) delete mode 100644 routes/api/v1/authors_search.go delete mode 100644 routes/api/v1/books_search.go delete mode 100644 routes/api/v1/publishers_search.go diff --git a/routes/api/v1/authors_list.go b/routes/api/v1/authors_list.go index 3b4863b..47bb978 100644 --- a/routes/api/v1/authors_list.go +++ b/routes/api/v1/authors_list.go @@ -9,7 +9,10 @@ import ( // AuthorsList is the handler to list authors func AuthorsList(c echo.Context) error { - list, err := models.ListAuthors("") + // Prepare the searchterm + search := c.QueryParam("s") + + list, err := models.ListAuthors(search) 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 deleted file mode 100644 index 38bd147..0000000 --- a/routes/api/v1/authors_search.go +++ /dev/null @@ -1,33 +0,0 @@ -package v1 - -import ( - "github.com/labstack/echo" - "net/http" - - "git.mowie.cc/konrad/Library/models" -) - -// AuthorSearch is the handler to search for authors -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) -} diff --git a/routes/api/v1/books_list.go b/routes/api/v1/books_list.go index b2c0cae..2298b4f 100644 --- a/routes/api/v1/books_list.go +++ b/routes/api/v1/books_list.go @@ -10,7 +10,10 @@ import ( // BookList is the handler to list books func BookList(c echo.Context) error { - list, err := models.ListBooks("") + // Prepare the searchterm + search := c.QueryParam("s") + + list, err := models.ListBooks(search) if err != nil { return c.JSON(http.StatusInternalServerError, models.Message{"Error getting books"}) diff --git a/routes/api/v1/books_search.go b/routes/api/v1/books_search.go deleted file mode 100644 index caa6f8d..0000000 --- a/routes/api/v1/books_search.go +++ /dev/null @@ -1,33 +0,0 @@ -package v1 - -import ( - "github.com/labstack/echo" - "net/http" - - "git.mowie.cc/konrad/Library/models" -) - -// BookSearch is the handler to search for books -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) -} diff --git a/routes/api/v1/publishers_list.go b/routes/api/v1/publishers_list.go index 3acb8db..13ec35b 100644 --- a/routes/api/v1/publishers_list.go +++ b/routes/api/v1/publishers_list.go @@ -9,7 +9,10 @@ import ( // PublishersList is the handler to list publishers func PublishersList(c echo.Context) error { - list, err := models.ListPublishers("") + // Prepare the searchterm + search := c.QueryParam("s") + + list, err := models.ListPublishers(search) 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 deleted file mode 100644 index bba32d2..0000000 --- a/routes/api/v1/publishers_search.go +++ /dev/null @@ -1,33 +0,0 @@ -package v1 - -import ( - "github.com/labstack/echo" - "net/http" - - "git.mowie.cc/konrad/Library/models" -) - -// PublisherSearch is the handler to search for a publisher -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) -} diff --git a/routes/routes.go b/routes/routes.go index 4a706a6..88d82d5 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -64,17 +64,14 @@ func RegisterRoutes(e *echo.Echo) { // Lookup Books a.GET("/books", apiv1.BookList) a.GET("/books/:id", apiv1.BookShow) - a.GET("/books/search", apiv1.BookSearch) // Lookup Authors a.GET("/authors", apiv1.AuthorsList) a.GET("/authors/:id", apiv1.AuthorShow) - a.GET("/authors/search", apiv1.AuthorSearch) // Lookup Publishers a.GET("/publishers", apiv1.PublishersList) a.GET("/publishers/:id", apiv1.PublisherShow) - a.GET("/publishers/search", apiv1.PublisherSearch) // Lookup Items a.GET("/items", apiv1.ItemsList)