Compare commits

..

No commits in common. "29a0353888d7c0a00488aebedfbf11649b09efd0" and "ba94ad60845be82a2490b30174365c1cf955c6cd" have entirely different histories.

4 changed files with 32 additions and 89 deletions

View File

@ -62,7 +62,6 @@ func main() {
{ {
categoriesRoutes.POST("/add", handlers.CategoryAdd) categoriesRoutes.POST("/add", handlers.CategoryAdd)
categoriesRoutes.GET("/:id", handlers.CategoryGetId) categoriesRoutes.GET("/:id", handlers.CategoryGetId)
categoriesRoutes.GET("/all", handlers.CategoryGetAll)
categoriesRoutes.PUT("/edit/:id", handlers.CategoryPutId) categoriesRoutes.PUT("/edit/:id", handlers.CategoryPutId)
categoriesRoutes.DELETE("/delete/:id", handlers.CategoryDeleteId) categoriesRoutes.DELETE("/delete/:id", handlers.CategoryDeleteId)
} }
@ -84,7 +83,6 @@ func main() {
{ {
typesRoutes.POST("/add", handlers.TypeAdd) typesRoutes.POST("/add", handlers.TypeAdd)
typesRoutes.GET("/:id", handlers.TypeGetId) typesRoutes.GET("/:id", handlers.TypeGetId)
typesRoutes.GET("/all", handlers.TypeGetAll)
typesRoutes.PUT("/edit/:id", handlers.TypePutId) typesRoutes.PUT("/edit/:id", handlers.TypePutId)
typesRoutes.DELETE("/delete/:id", handlers.TypeDeleteId) typesRoutes.DELETE("/delete/:id", handlers.TypeDeleteId)
} }

View File

@ -35,16 +35,12 @@ var (
ERROR_CATEGORY_PARENT_NOT_FOUND = errors.New("Can't find Category with ParentID for user") 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_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_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 { func (c *Category) BeforeSave(tx *gorm.DB) error {
if c.ParentID == c.ID {
return ERROR_CATEGORY_SELF_REFERENCING
}
if c.ParentID != 0 { if c.ParentID != 0 {
var parent Category var parent Category
if err := tx.Find(&parent, c.ParentID).Error; err != nil { if err := tx.Find(&parent, c.ID).Error; err != nil {
return err return err
} }
if parent.ID == 0 { if parent.ID == 0 {
@ -58,7 +54,7 @@ func (c *Category) BeforeSave(tx *gorm.DB) error {
if err := tx.Find(&dup, Category{Name: c.Name, UserID: c.UserID}).Error; err != nil { if err := tx.Find(&dup, Category{Name: c.Name, UserID: c.UserID}).Error; err != nil {
return err return err
} }
if c.ID != dup.ID && dup.ID != 0 { if dup.ID != 0 {
return ERROR_CATEGORY_NAME_NOT_UNIQUE return ERROR_CATEGORY_NAME_NOT_UNIQUE
} }
return nil return nil

View File

@ -6,14 +6,6 @@ import (
"github.com/gin-gonic/gin" "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 // @Summary Get category by id
// @Description Get category by id // @Description Get category by id
// @Tags category // @Tags category
@ -29,37 +21,13 @@ var categoryTransform func(*db.Category) types.DbCategory = func(inp *db.Categor
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Router /category/:id [get] // @Router /category/:id [get]
func CategoryGetId(c *gin.Context) { func CategoryGetId(c *gin.Context) {
GetHandler(categoryTransform)(c) GetHandler(func(inp *db.Category) types.DbCategory {
return types.DbCategory{
ID: inp.ID,
Name: inp.Name,
ParentID: inp.ParentID,
} }
})(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 // @Summary Get category by id
@ -102,7 +70,13 @@ func CategoryPutId(c *gin.Context) {
dst.Name = src.Name dst.Name = src.Name
dst.ParentID = src.ParentID dst.ParentID = src.ParentID
}, },
categoryTransform, func(inp *db.Category) types.DbCategory {
return types.DbCategory{
ID: inp.ID,
Name: inp.Name,
ParentID: inp.ParentID,
}
},
)(c) )(c)
} }

View File

@ -6,15 +6,6 @@ import (
"github.com/gin-gonic/gin" "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 // @Summary Get dbtype by id
// @Description Get dbtype by id // @Description Get dbtype by id
// @Tags dbtype // @Tags dbtype
@ -30,37 +21,14 @@ var typeTransform func(inp *db.Type) types.DbType = func(inp *db.Type) types.DbT
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Router /dbtype/:id [get] // @Router /dbtype/:id [get]
func TypeGetId(c *gin.Context) { func TypeGetId(c *gin.Context) {
GetHandler(typeTransform)(c) GetHandler(func(inp *db.Type) types.DbType {
return types.DbType{
ID: inp.ID,
Name: inp.Name,
Comment: inp.Comment,
Color: inp.Color,
} }
})(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 // @Summary Get dbtype by id
@ -106,7 +74,14 @@ func TypePutId(c *gin.Context) {
dst.Comment = updates.Comment dst.Comment = updates.Comment
dst.Color = updates.Color dst.Color = updates.Color
}, },
typeTransform, func(inp *db.Type) types.DbType {
return types.DbType{
ID: inp.ID,
Name: inp.Name,
Comment: inp.Comment,
Color: inp.Color,
}
},
)(c) )(c)
} }