Compare commits
4 Commits
a9a4f26528
...
ead0a4bd60
Author | SHA1 | Date | |
---|---|---|---|
ead0a4bd60 | |||
dd19718dc8 | |||
fff8c256d1 | |||
3b3a8b13d6 |
|
@ -104,6 +104,13 @@ func main() {
|
||||||
transfersRoutes.PUT("/edit/:id", handlers.TransferPutId)
|
transfersRoutes.PUT("/edit/:id", handlers.TransferPutId)
|
||||||
transfersRoutes.DELETE("/delete/:id", handlers.TransferDeleteId)
|
transfersRoutes.DELETE("/delete/:id", handlers.TransferDeleteId)
|
||||||
}
|
}
|
||||||
|
itemRoutes := api.Group("/item", middleware.AuthMiddleware())
|
||||||
|
{
|
||||||
|
itemRoutes.GET("/:id", handlers.ItemGetId)
|
||||||
|
itemRoutes.GET("/all", handlers.ItemGetAll)
|
||||||
|
itemRoutes.POST("/filter", handlers.ItemPostFilter)
|
||||||
|
itemRoutes.DELETE("/delete/:id", handlers.ItemDeleteId)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||||
|
|
17
db/item.go
17
db/item.go
|
@ -22,6 +22,23 @@ type Item struct {
|
||||||
//
|
//
|
||||||
TypeID uint
|
TypeID uint
|
||||||
Type *Type
|
Type *Type
|
||||||
|
UserID uint
|
||||||
|
User *User
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements db.UserIdentifiable:1
|
||||||
|
func (i Item) GetID() uint {
|
||||||
|
return i.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements db.UserIdentifiable:2
|
||||||
|
func (i Item) GetUserID() uint {
|
||||||
|
return i.UserID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Implements db.UserIdentifiable:3
|
||||||
|
func (i *Item) SetUserID(id uint) {
|
||||||
|
i.UserID = id
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetItem(id uint, preloadPrices bool) (*Item, error) {
|
func GetItem(id uint, preloadPrices bool) (*Item, error) {
|
||||||
|
|
135
handlers/item.go
Normal file
135
handlers/item.go
Normal file
|
@ -0,0 +1,135 @@
|
||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
|
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
||||||
|
"git.qowevisa.me/Qowevisa/fin-check-api/types"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
var itemTransform func(inp *db.Item) types.DbItem = func(inp *db.Item) types.DbItem {
|
||||||
|
return types.DbItem{
|
||||||
|
ID: inp.ID,
|
||||||
|
CategoryID: inp.CategoryID,
|
||||||
|
CurrentPriceID: inp.CurrentPriceID,
|
||||||
|
TypeID: inp.TypeID,
|
||||||
|
Name: inp.Name,
|
||||||
|
Comment: inp.Comment,
|
||||||
|
MetricType: inp.MetricType,
|
||||||
|
MetricValue: inp.MetricValue,
|
||||||
|
Proteins: inp.Proteins,
|
||||||
|
Carbs: inp.Carbs,
|
||||||
|
Fats: inp.Fats,
|
||||||
|
Price: inp.CurrentPrice.Price,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Summary Get item by id
|
||||||
|
// @Description Get item by id
|
||||||
|
// @Tags item
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Param Authorization header string true "Bearer token"
|
||||||
|
// @Param item path int true "id"
|
||||||
|
// @Success 200 {object} types.DbItem
|
||||||
|
// @Failure 400 {object} types.ErrorResponse
|
||||||
|
// @Failure 401 {object} types.ErrorResponse
|
||||||
|
// @Failure 403 {object} types.ErrorResponse
|
||||||
|
// @Failure 500 {object} types.ErrorResponse
|
||||||
|
// @Security ApiKeyAuth
|
||||||
|
// @Router /item/:id [get]
|
||||||
|
func ItemGetId(c *gin.Context) {
|
||||||
|
GetHandler(itemTransform)(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Summary Get all items for user
|
||||||
|
// @Description Get all items for user
|
||||||
|
// @Tags type
|
||||||
|
// @Produce json
|
||||||
|
// @Param Authorization header string true "Bearer token"
|
||||||
|
// @Success 200 {object} []types.DbItem
|
||||||
|
// @Failure 401 {object} types.ErrorResponse
|
||||||
|
// @Failure 500 {object} types.ErrorResponse
|
||||||
|
// @Security ApiKeyAuth
|
||||||
|
// @Router /item/all [get]
|
||||||
|
func ItemGetAll(c *gin.Context) {
|
||||||
|
userID, err := GetUserID(c)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
dbc := db.Connect()
|
||||||
|
var entities []*db.Item
|
||||||
|
if err := dbc.Find(&entities, db.Item{UserID: userID}).Error; err != nil {
|
||||||
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret []types.DbItem
|
||||||
|
for _, entity := range entities {
|
||||||
|
ret = append(ret, itemTransform(entity))
|
||||||
|
}
|
||||||
|
c.JSON(200, ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Summary Get all items for user filtered
|
||||||
|
// @Description Get all items for user based on body criteria
|
||||||
|
// @Tags type
|
||||||
|
// @Produce json
|
||||||
|
// @Param Authorization header string true "Bearer token"
|
||||||
|
// @Success 200 {object} []types.DbItem
|
||||||
|
// @Failure 401 {object} types.ErrorResponse
|
||||||
|
// @Failure 500 {object} types.ErrorResponse
|
||||||
|
// @Security ApiKeyAuth
|
||||||
|
// @Router /item/filter [post]
|
||||||
|
func ItemPostFilter(c *gin.Context) {
|
||||||
|
userID, err := GetUserID(c)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var filterObj types.DbItemSearch
|
||||||
|
if err := c.ShouldBindJSON(&filterObj); err != nil {
|
||||||
|
log.Printf("err is %v\n", err)
|
||||||
|
c.JSON(400, types.ErrorResponse{Message: "Invalid request"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
dbc := db.Connect()
|
||||||
|
var entities []*db.Item
|
||||||
|
filter := db.Item{
|
||||||
|
UserID: userID,
|
||||||
|
CategoryID: filterObj.CategoryID,
|
||||||
|
TypeID: filterObj.TypeID,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := dbc.Find(&entities, filter).Error; err != nil {
|
||||||
|
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret []types.DbItem
|
||||||
|
for _, entity := range entities {
|
||||||
|
ret = append(ret, itemTransform(entity))
|
||||||
|
}
|
||||||
|
c.JSON(200, ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Summary Delete item by id
|
||||||
|
// @Description Delete item by id
|
||||||
|
// @Tags item
|
||||||
|
// @Accept json
|
||||||
|
// @Produce json
|
||||||
|
// @Param Authorization header string true "Bearer token"
|
||||||
|
// @Param itemID path int true "id"
|
||||||
|
// @Success 200 {object} types.DbItem
|
||||||
|
// @Failure 400 {object} types.ErrorResponse
|
||||||
|
// @Failure 401 {object} types.ErrorResponse
|
||||||
|
// @Failure 403 {object} types.ErrorResponse
|
||||||
|
// @Failure 500 {object} types.ErrorResponse
|
||||||
|
// @Security ApiKeyAuth
|
||||||
|
// @Router /item/delete/:id [delete]
|
||||||
|
func ItemDeleteId(c *gin.Context) {
|
||||||
|
DeleteHandler[*db.Item]()(c)
|
||||||
|
}
|
|
@ -97,3 +97,23 @@ type DbTransfer struct {
|
||||||
Value uint64 `json:"value" example:"20000"`
|
Value uint64 `json:"value" example:"20000"`
|
||||||
Date time.Time `json:"date" example:"29/11/2001 12:00"`
|
Date time.Time `json:"date" example:"29/11/2001 12:00"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DbItem struct {
|
||||||
|
ID uint `json:"id" example:"1"`
|
||||||
|
CategoryID uint `json:"category_id" example:"1"`
|
||||||
|
CurrentPriceID uint `json:"current_price_id" example:"1"`
|
||||||
|
TypeID uint `json:"type_id" example:"1"`
|
||||||
|
Name string `json:"name" example:"pizza"`
|
||||||
|
Comment string `json:"comment" example:"this is an item"`
|
||||||
|
MetricType uint8 `json:"metric_type" example:"0"`
|
||||||
|
MetricValue uint64 `json:"metric_value" example:"10000"`
|
||||||
|
Proteins uint64 `json:"proteins" example:"0"`
|
||||||
|
Carbs uint64 `json:"carbs" example:"0"`
|
||||||
|
Fats uint64 `json:"fats" example:"0"`
|
||||||
|
Price uint64 `json:"price" example:"10050"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type DbItemSearch struct {
|
||||||
|
CategoryID uint `json:"category_id" example:"1"`
|
||||||
|
TypeID uint `json:"type_id" example:"1"`
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user