Implemented Author lookup

+ book lookup optimization

Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
konrad 2017-10-10 18:11:09 +02:00 committed by kolaente
parent fed91b9e8a
commit 6ce0c553d4
7 changed files with 92 additions and 7 deletions

View File

@ -23,4 +23,10 @@ type AuthorBook struct {
func (AuthorBook) TableName() string {
return "authors_books"
}
func GetAuthorByID(id int64) (author Author, exists bool, err error) {
has, err := x.Id(id).Get(&author)
return author, has, err
}

11
models/authors_list.go Normal file
View File

@ -0,0 +1,11 @@
package models
func ListAuthors() (authors []Author, err error) {
err = x.Find(&authors)
if err != nil {
return []Author{}, err
}
return authors, nil
}

View File

@ -21,9 +21,9 @@ func (Book) TableName() string{
return "books"
}
func GetBookById(ID string) (book Book, err error) {
func GetBookById(ID int64) (book Book, exists bool, err error) {
// Get the Book
_, err = x.ID(ID).Get(&book)
has, err := x.ID(ID).Get(&book)
// Get publisher
publisher := Publisher{ID: book.Publisher}
@ -47,5 +47,5 @@ func GetBookById(ID string) (book Book, err error) {
book.Authors = authors
return book, err
return book, has, err
}

View File

@ -0,0 +1,37 @@
package v1
import (
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"strconv"
)
func AuthorShow(c echo.Context) error {
author := c.Param("id")
if author == "" {
return c.JSON(http.StatusBadRequest, models.Message{"Author ID cannot be empty."})
}
// Make int
authorID, err := strconv.ParseInt(author, 10, 64)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting author infos."})
}
// Get Author Infos
authorInfos, exists, err := models.GetAuthorByID(authorID)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting author infos."})
}
// Check if it exists
if !exists {
return c.JSON(http.StatusNotFound, models.Message{"Author not found."})
}
return c.JSON(http.StatusOK, authorInfos)
}

View File

@ -0,0 +1,18 @@
package v1
import (
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
)
func AuthorsList(c echo.Context) error {
list, err := models.ListAuthors()
if err != nil{
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting authors"})
}
return c.JSON(http.StatusOK, list)
}

View File

@ -4,21 +4,30 @@ import (
"github.com/labstack/echo"
"net/http"
"git.mowie.cc/konrad/Library/models"
"strconv"
)
func BookShow(c echo.Context) error {
bookID := c.Param("id")
book := c.Param("id")
if bookID == "" {
if book == "" {
return c.JSON(http.StatusBadRequest, models.Message{"Book id cannot be empty."})
}
// Make int
bookID, err := strconv.ParseInt(book, 10, 64)
// Get book infos
book, err := models.GetBookById(bookID)
bookInfo, exists, err := models.GetBookById(bookID)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not get book infos"})
}
return c.JSON(http.StatusOK, book)
// Check if it exists
if !exists {
return c.JSON(http.StatusNotFound, models.Message{"Book not found."})
}
return c.JSON(http.StatusOK, bookInfo)
}

View File

@ -31,6 +31,8 @@ func RegisterRoutes(e *echo.Echo) {
a.GET("/books/:id", apiv1.BookShow)
// Lookup Authors
a.GET("/authors/list", apiv1.AuthorsList)
a.GET("/authors/:id", apiv1.AuthorShow)
// Lookup Publishers
@ -69,12 +71,14 @@ func RegisterRoutes(e *echo.Echo) {
/books/list - Auflisten
/books/add - |Hinzufügen
/authors/:id - Autor anzeigen
/authors/:id/edit - |Autor bearbeiten
/authors/:id/delete - |Autor löschen (auch mit allem in books_author)
/authors/list - Autoren auflisten
/authors/search?s=d - Autoren suchen
/authors/add - |Hinzufügen
/publishers/:id - Verlag anzeigen
/publishers/:id/edit - |Verlag bearbeiten
/publishers/:id/delete - |Verlag löschen (bei büchern Verlag auf 0 setzen)
/publishers/list - Verlage auflisten