implemented authors and publishers search
Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
parent
38fdf9cbd8
commit
2e360becca
|
@ -3,7 +3,7 @@ package models
|
||||||
type Author struct {
|
type Author struct {
|
||||||
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
||||||
Forename string `xorm:"varchar(250)"`
|
Forename string `xorm:"varchar(250)"`
|
||||||
Lastame string `xorm:"varchar(250) not null"`
|
Lastname string `xorm:"varchar(250) not null"`
|
||||||
Created int64 `xorm:"created"`
|
Created int64 `xorm:"created"`
|
||||||
Updated int64 `xorm:"updated"`
|
Updated int64 `xorm:"updated"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,16 @@
|
||||||
package models
|
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 {
|
if err != nil {
|
||||||
return []Author{}, err
|
return []Author{}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,14 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
func ListPublishers() (publishers []Publisher, err error) {
|
func ListPublishers(searchterm string) (publishers []Publisher, err error) {
|
||||||
err = x.Find(&publishers)
|
|
||||||
|
if searchterm == "" {
|
||||||
|
err = x.Find(&publishers)
|
||||||
|
} else {
|
||||||
|
err = x.
|
||||||
|
Where("name LIKE ?", "%" + searchterm + "%").
|
||||||
|
Find(&publishers)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []Publisher{}, err
|
return []Publisher{}, err
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
func AuthorsList(c echo.Context) error {
|
func AuthorsList(c echo.Context) error {
|
||||||
|
|
||||||
list, err := models.ListAuthors()
|
list, err := models.ListAuthors("")
|
||||||
|
|
||||||
if err != nil{
|
if err != nil{
|
||||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting authors"})
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting authors"})
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -9,17 +9,24 @@ import (
|
||||||
|
|
||||||
func BookSearch(c echo.Context) error {
|
func BookSearch(c echo.Context) error {
|
||||||
|
|
||||||
|
// Prepare the searchterm
|
||||||
search := c.QueryParam("s")
|
search := c.QueryParam("s")
|
||||||
|
|
||||||
if search == "" {
|
if search == "" {
|
||||||
return c.JSON(http.StatusBadRequest, models.Message{"Search cannot be empty."})
|
return c.JSON(http.StatusBadRequest, models.Message{"Search cannot be empty."})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the Books
|
||||||
list, err := models.ListBooks(search)
|
list, err := models.ListBooks(search)
|
||||||
|
|
||||||
if err != nil{
|
if err != nil{
|
||||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting books"})
|
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)
|
return c.JSON(http.StatusOK, list)
|
||||||
}
|
}
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
func PublishersList(c echo.Context) error {
|
func PublishersList(c echo.Context) error {
|
||||||
|
|
||||||
list, err := models.ListPublishers()
|
list, err := models.ListPublishers("")
|
||||||
|
|
||||||
if err != nil{
|
if err != nil{
|
||||||
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publishers"})
|
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting publishers"})
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
|
@ -34,10 +34,12 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
// Lookup Authors
|
// Lookup Authors
|
||||||
a.GET("/authors/list", apiv1.AuthorsList)
|
a.GET("/authors/list", apiv1.AuthorsList)
|
||||||
a.GET("/authors/:id", apiv1.AuthorShow)
|
a.GET("/authors/:id", apiv1.AuthorShow)
|
||||||
|
a.GET("/authors/search", apiv1.AuthorSearch)
|
||||||
|
|
||||||
// Lookup Publishers
|
// Lookup Publishers
|
||||||
a.GET("/publishers/list", apiv1.PublishersList)
|
a.GET("/publishers/list", apiv1.PublishersList)
|
||||||
a.GET("/publishers/:id", apiv1.PublisherShow)
|
a.GET("/publishers/:id", apiv1.PublisherShow)
|
||||||
|
a.GET("/publishers/search", apiv1.PublisherSearch)
|
||||||
|
|
||||||
// Login Route
|
// Login Route
|
||||||
e.POST("/login", Login)
|
e.POST("/login", Login)
|
||||||
|
|
Loading…
Reference in New Issue