Added lookup one single book
Signed-off-by: kolaente <konrad@kola-entertainments.de>
This commit is contained in:
parent
2c913f99d5
commit
167ba086d7
|
@ -1,5 +1,7 @@
|
||||||
package models
|
package models
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
type Book struct {
|
type Book struct {
|
||||||
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
ID int64 `xorm:"int(11) autoincr not null unique pk"`
|
||||||
Title string `xorm:"varchar(250) not null"`
|
Title string `xorm:"varchar(250) not null"`
|
||||||
|
@ -17,4 +19,33 @@ type Book struct {
|
||||||
|
|
||||||
func (Book) TableName() string{
|
func (Book) TableName() string{
|
||||||
return "books"
|
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
|
||||||
}
|
}
|
|
@ -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)
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"git.mowie.cc/konrad/Library/models"
|
"git.mowie.cc/konrad/Library/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func List(c echo.Context) error {
|
func BookList(c echo.Context) error {
|
||||||
|
|
||||||
list, err := models.ListBooks()
|
list, err := models.ListBooks()
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,8 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
// API Routes
|
// API Routes
|
||||||
a := e.Group("/api/v1")
|
a := e.Group("/api/v1")
|
||||||
// Lookup Books
|
// Lookup Books
|
||||||
a.GET("/books/list", apiv1.List)
|
a.GET("/books/list", apiv1.BookList)
|
||||||
|
a.GET("/books/:id", apiv1.BookShow)
|
||||||
|
|
||||||
// Lookup Authors
|
// Lookup Authors
|
||||||
|
|
||||||
|
@ -61,6 +62,7 @@ func RegisterRoutes(e *echo.Echo) {
|
||||||
/login - Einloggen
|
/login - Einloggen
|
||||||
/logout - ausloggen
|
/logout - ausloggen
|
||||||
|
|
||||||
|
/books/:id - Buch anzeigen
|
||||||
/books/:id/edit - |Buch bearbeiten (inkl mengen)
|
/books/:id/edit - |Buch bearbeiten (inkl mengen)
|
||||||
/books/:id/delete - |Buch löschen
|
/books/:id/delete - |Buch löschen
|
||||||
/books/search?s=se - Suchen
|
/books/search?s=se - Suchen
|
||||||
|
|
Loading…
Reference in New Issue