implemented authors and publishers search

Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
konrad 2017-10-10 21:58:55 +02:00 committed by kolaente
parent 38fdf9cbd8
commit 2e360becca
9 changed files with 95 additions and 7 deletions

View File

@ -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"`
}

View File

@ -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
}

View File

@ -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

View File

@ -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"})

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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"})

View File

@ -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)
}

View File

@ -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)