From 6ce0c553d422ce51b29f32e5d499cf9d6c07fd62 Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 10 Oct 2017 18:11:09 +0200 Subject: [PATCH] Implemented Author lookup + book lookup optimization Signed-off-by: kolaente --- models/author.go | 6 ++++++ models/authors_list.go | 11 +++++++++++ models/book.go | 6 +++--- routes/api/v1/author_show.go | 37 +++++++++++++++++++++++++++++++++++ routes/api/v1/authors_list.go | 18 +++++++++++++++++ routes/api/v1/book_show.go | 17 ++++++++++++---- routes/routes.go | 4 ++++ 7 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 models/authors_list.go create mode 100644 routes/api/v1/author_show.go create mode 100644 routes/api/v1/authors_list.go diff --git a/models/author.go b/models/author.go index f42d6b5..94ed707 100644 --- a/models/author.go +++ b/models/author.go @@ -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 } \ No newline at end of file diff --git a/models/authors_list.go b/models/authors_list.go new file mode 100644 index 0000000..767da2a --- /dev/null +++ b/models/authors_list.go @@ -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 +} \ No newline at end of file diff --git a/models/book.go b/models/book.go index 1d265aa..9395224 100644 --- a/models/book.go +++ b/models/book.go @@ -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 } \ No newline at end of file diff --git a/routes/api/v1/author_show.go b/routes/api/v1/author_show.go new file mode 100644 index 0000000..96cfb75 --- /dev/null +++ b/routes/api/v1/author_show.go @@ -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) + +} diff --git a/routes/api/v1/authors_list.go b/routes/api/v1/authors_list.go new file mode 100644 index 0000000..9c77609 --- /dev/null +++ b/routes/api/v1/authors_list.go @@ -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) +} \ No newline at end of file diff --git a/routes/api/v1/book_show.go b/routes/api/v1/book_show.go index b1b3e77..9c16fc9 100644 --- a/routes/api/v1/book_show.go +++ b/routes/api/v1/book_show.go @@ -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) } \ No newline at end of file diff --git a/routes/routes.go b/routes/routes.go index c24bf75..c41fa60 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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