From 167ba086d7445e51c5a4f37a0f92811248b25f5f Mon Sep 17 00:00:00 2001 From: konrad Date: Tue, 10 Oct 2017 17:01:33 +0200 Subject: [PATCH] Added lookup one single book Signed-off-by: kolaente --- models/book.go | 31 +++++++++++++++++++++++++++++++ routes/api/v1/book_show.go | 24 ++++++++++++++++++++++++ routes/api/v1/books_list.go | 2 +- routes/routes.go | 4 +++- 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 routes/api/v1/book_show.go diff --git a/models/book.go b/models/book.go index efd33c6..50579e7 100644 --- a/models/book.go +++ b/models/book.go @@ -1,5 +1,7 @@ package models +import "fmt" + type Book struct { ID int64 `xorm:"int(11) autoincr not null unique pk"` Title string `xorm:"varchar(250) not null"` @@ -17,4 +19,33 @@ type Book struct { func (Book) TableName() string{ return "books" +} + +func GetBookById(ID string) (book Book, err error) { + _, err = x.ID(ID).Get(&book) + + + // Get publisher + publisher := Publisher{ID: book.Publisher} + _, err = x.Get(&publisher) + if err != nil { + fmt.Println("Error getting publisher:", err) + } + book.PublisherFull = publisher + + // Get all authors + var authors []Author + err = x. + Table("authors_books"). + Join("INNER", "authors", "authors_books.author_id = authors.id"). + Where("book_id = ?", book.ID). + Find(&authors) + + if err != nil { + fmt.Println("Error getting authors:", err) + } + + book.Authors = authors + + return book, err } \ No newline at end of file diff --git a/routes/api/v1/book_show.go b/routes/api/v1/book_show.go new file mode 100644 index 0000000..b1b3e77 --- /dev/null +++ b/routes/api/v1/book_show.go @@ -0,0 +1,24 @@ +package v1 + +import ( + "github.com/labstack/echo" + "net/http" + "git.mowie.cc/konrad/Library/models" +) + +func BookShow(c echo.Context) error { + bookID := c.Param("id") + + if bookID == "" { + return c.JSON(http.StatusBadRequest, models.Message{"Book id cannot be empty."}) + } + + // Get book infos + book, 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) +} \ No newline at end of file diff --git a/routes/api/v1/books_list.go b/routes/api/v1/books_list.go index 5fc93e5..8b7be8c 100644 --- a/routes/api/v1/books_list.go +++ b/routes/api/v1/books_list.go @@ -7,7 +7,7 @@ import ( "git.mowie.cc/konrad/Library/models" ) -func List(c echo.Context) error { +func BookList(c echo.Context) error { list, err := models.ListBooks() diff --git a/routes/routes.go b/routes/routes.go index bd4a351..c24bf75 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -27,7 +27,8 @@ func RegisterRoutes(e *echo.Echo) { // API Routes a := e.Group("/api/v1") // Lookup Books - a.GET("/books/list", apiv1.List) + a.GET("/books/list", apiv1.BookList) + a.GET("/books/:id", apiv1.BookShow) // Lookup Authors @@ -61,6 +62,7 @@ func RegisterRoutes(e *echo.Echo) { /login - Einloggen /logout - ausloggen + /books/:id - Buch anzeigen /books/:id/edit - |Buch bearbeiten (inkl mengen) /books/:id/delete - |Buch löschen /books/search?s=se - Suchen