Compare commits
4 Commits
ba94ad6084
...
29a0353888
Author | SHA1 | Date | |
---|---|---|---|
29a0353888 | |||
22007ef38e | |||
8a7d52efda | |||
9b77237d50 |
|
@ -62,6 +62,7 @@ func main() {
|
|||
{
|
||||
categoriesRoutes.POST("/add", handlers.CategoryAdd)
|
||||
categoriesRoutes.GET("/:id", handlers.CategoryGetId)
|
||||
categoriesRoutes.GET("/all", handlers.CategoryGetAll)
|
||||
categoriesRoutes.PUT("/edit/:id", handlers.CategoryPutId)
|
||||
categoriesRoutes.DELETE("/delete/:id", handlers.CategoryDeleteId)
|
||||
}
|
||||
|
@ -83,6 +84,7 @@ func main() {
|
|||
{
|
||||
typesRoutes.POST("/add", handlers.TypeAdd)
|
||||
typesRoutes.GET("/:id", handlers.TypeGetId)
|
||||
typesRoutes.GET("/all", handlers.TypeGetAll)
|
||||
typesRoutes.PUT("/edit/:id", handlers.TypePutId)
|
||||
typesRoutes.DELETE("/delete/:id", handlers.TypeDeleteId)
|
||||
}
|
||||
|
|
|
@ -35,12 +35,16 @@ var (
|
|||
ERROR_CATEGORY_PARENT_NOT_FOUND = errors.New("Can't find Category with ParentID for user")
|
||||
ERROR_CATEGORY_NAME_NOT_UNIQUE = errors.New("Name for Category have to be unique for user")
|
||||
ERROR_CATEGORY_USER_ID_NOT_EQUAL = errors.New("ParentID is invalid for user")
|
||||
ERROR_CATEGORY_SELF_REFERENCING = errors.New("Category can't set itself as a parent")
|
||||
)
|
||||
|
||||
func (c *Category) BeforeSave(tx *gorm.DB) error {
|
||||
if c.ParentID == c.ID {
|
||||
return ERROR_CATEGORY_SELF_REFERENCING
|
||||
}
|
||||
if c.ParentID != 0 {
|
||||
var parent Category
|
||||
if err := tx.Find(&parent, c.ID).Error; err != nil {
|
||||
if err := tx.Find(&parent, c.ParentID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if parent.ID == 0 {
|
||||
|
@ -54,7 +58,7 @@ func (c *Category) BeforeSave(tx *gorm.DB) error {
|
|||
if err := tx.Find(&dup, Category{Name: c.Name, UserID: c.UserID}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if dup.ID != 0 {
|
||||
if c.ID != dup.ID && dup.ID != 0 {
|
||||
return ERROR_CATEGORY_NAME_NOT_UNIQUE
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -6,6 +6,14 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var categoryTransform func(*db.Category) types.DbCategory = func(inp *db.Category) types.DbCategory {
|
||||
return types.DbCategory{
|
||||
ID: inp.ID,
|
||||
Name: inp.Name,
|
||||
ParentID: inp.ParentID,
|
||||
}
|
||||
}
|
||||
|
||||
// @Summary Get category by id
|
||||
// @Description Get category by id
|
||||
// @Tags category
|
||||
|
@ -21,13 +29,37 @@ import (
|
|||
// @Security ApiKeyAuth
|
||||
// @Router /category/:id [get]
|
||||
func CategoryGetId(c *gin.Context) {
|
||||
GetHandler(func(inp *db.Category) types.DbCategory {
|
||||
return types.DbCategory{
|
||||
ID: inp.ID,
|
||||
Name: inp.Name,
|
||||
ParentID: inp.ParentID,
|
||||
}
|
||||
})(c)
|
||||
GetHandler(categoryTransform)(c)
|
||||
}
|
||||
|
||||
// @Summary Get all categories for user
|
||||
// @Description Get all categories for user
|
||||
// @Tags type
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "Bearer token"
|
||||
// @Success 200 {object} []types.DbCategory
|
||||
// @Failure 401 {object} types.ErrorResponse
|
||||
// @Failure 500 {object} types.ErrorResponse
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /category/all [get]
|
||||
func CategoryGetAll(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.Category
|
||||
if err := dbc.Find(&entities, db.Category{UserID: userID}).Error; err != nil {
|
||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var ret []types.DbCategory
|
||||
for _, entity := range entities {
|
||||
ret = append(ret, categoryTransform(entity))
|
||||
}
|
||||
c.JSON(200, ret)
|
||||
}
|
||||
|
||||
// @Summary Get category by id
|
||||
|
@ -70,13 +102,7 @@ func CategoryPutId(c *gin.Context) {
|
|||
dst.Name = src.Name
|
||||
dst.ParentID = src.ParentID
|
||||
},
|
||||
func(inp *db.Category) types.DbCategory {
|
||||
return types.DbCategory{
|
||||
ID: inp.ID,
|
||||
Name: inp.Name,
|
||||
ParentID: inp.ParentID,
|
||||
}
|
||||
},
|
||||
categoryTransform,
|
||||
)(c)
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,15 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var typeTransform func(inp *db.Type) types.DbType = func(inp *db.Type) types.DbType {
|
||||
return types.DbType{
|
||||
ID: inp.ID,
|
||||
Name: inp.Name,
|
||||
Comment: inp.Comment,
|
||||
Color: inp.Color,
|
||||
}
|
||||
}
|
||||
|
||||
// @Summary Get dbtype by id
|
||||
// @Description Get dbtype by id
|
||||
// @Tags dbtype
|
||||
|
@ -21,14 +30,37 @@ import (
|
|||
// @Security ApiKeyAuth
|
||||
// @Router /dbtype/:id [get]
|
||||
func TypeGetId(c *gin.Context) {
|
||||
GetHandler(func(inp *db.Type) types.DbType {
|
||||
return types.DbType{
|
||||
ID: inp.ID,
|
||||
Name: inp.Name,
|
||||
Comment: inp.Comment,
|
||||
Color: inp.Color,
|
||||
}
|
||||
})(c)
|
||||
GetHandler(typeTransform)(c)
|
||||
}
|
||||
|
||||
// @Summary Get all types for user
|
||||
// @Description Get all types for user
|
||||
// @Tags type
|
||||
// @Produce json
|
||||
// @Param Authorization header string true "Bearer token"
|
||||
// @Success 200 {object} []types.DbType
|
||||
// @Failure 401 {object} types.ErrorResponse
|
||||
// @Failure 500 {object} types.ErrorResponse
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /type/all [get]
|
||||
func TypeGetAll(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.Type
|
||||
if err := dbc.Find(&entities, db.Type{UserID: userID}).Error; err != nil {
|
||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var ret []types.DbType
|
||||
for _, entity := range entities {
|
||||
ret = append(ret, typeTransform(entity))
|
||||
}
|
||||
c.JSON(200, ret)
|
||||
}
|
||||
|
||||
// @Summary Get dbtype by id
|
||||
|
@ -74,14 +106,7 @@ func TypePutId(c *gin.Context) {
|
|||
dst.Comment = updates.Comment
|
||||
dst.Color = updates.Color
|
||||
},
|
||||
func(inp *db.Type) types.DbType {
|
||||
return types.DbType{
|
||||
ID: inp.ID,
|
||||
Name: inp.Name,
|
||||
Comment: inp.Comment,
|
||||
Color: inp.Color,
|
||||
}
|
||||
},
|
||||
typeTransform,
|
||||
)(c)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user