Implemented Author lookup
+ book lookup optimization Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
parent
fed91b9e8a
commit
6ce0c553d4
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue