Added dynamically get books status

This commit is contained in:
konrad 2017-11-15 16:13:47 +01:00 committed by kolaente
parent a76f20610a
commit f48de8e75f
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 67 additions and 0 deletions

View File

@ -28,6 +28,8 @@ func AddBook(book Book) (newBook Book, err error) {
}
}
// TODO: Check for empty values, should have at least a title. If no publisher is given (aka ID=0 & Name=""), don't insert an empty one.
_, exists, err = GetPublisherByID(publisherid)
if err != nil {
return Book{}, err

View File

@ -31,6 +31,7 @@ func SetEngine() (err error) {
x.Sync(&Publisher{})
x.Sync(&Author{})
x.Sync(&AuthorBook{})
x.Sync(&Status{})
x.ShowSQL(true)
return nil

17
models/status.go Normal file
View File

@ -0,0 +1,17 @@
package models
// Status holds a status
type Status struct {
ID int64 `xorm:"int(11) autoincr not null unique pk"`
Name string `xorm:"varchar(250)"`
}
func GetStatusList() (status []Status, err error){
err = x.Find(&status)
return status, err
}
func GetStatusByID(id int64) (status Status, err error) {
_, err = x.Where("id = ?", id).Get(&status)
return status, err
}

43
routes/api/v1/status.go Normal file
View File

@ -0,0 +1,43 @@
package v1
import (
"github.com/labstack/echo"
"git.mowie.cc/konrad/Library/models"
"net/http"
"fmt"
"strconv"
)
func StatusListShow(c echo.Context) error {
status, err := models.GetStatusList()
if err != nil {
fmt.Println(err)
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
return c.JSON(http.StatusOK, status)
}
func StatusByIDShow(c echo.Context) error {
statusIn := c.Param("id")
if statusIn == "" {
return c.JSON(http.StatusBadRequest, models.Message{"Status ID cannot be empty."})
}
// Make int
statusID, err := strconv.ParseInt(statusIn, 10, 64)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Error getting status infos."})
}
status, err := models.GetStatusByID(statusID)
if err != nil {
fmt.Println(err)
return c.JSON(http.StatusInternalServerError, models.Message{"An error occured."})
}
return c.JSON(http.StatusOK, status)
}

View File

@ -72,6 +72,10 @@ func RegisterRoutes(e *echo.Echo) {
a.GET("/publishers/:id", apiv1.PublisherShow)
a.GET("/publishers/search", apiv1.PublisherSearch)
// Lookup Status
a.GET("/status", apiv1.StatusListShow)
a.GET("/status/:id", apiv1.StatusByIDShow)
// ===== Routes with Authetification =====
// Authetification
a.Use(middleware.JWT(models.Config.JWTLoginSecret))