Compare commits
3 Commits
d55ba5c3c7
...
8807235dad
Author | SHA1 | Date | |
---|---|---|---|
8807235dad | |||
87c5676846 | |||
fc2b066c12 |
|
@ -39,9 +39,6 @@ var (
|
|||
)
|
||||
|
||||
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.ParentID).Error; err != nil {
|
||||
|
@ -63,3 +60,10 @@ func (c *Category) BeforeSave(tx *gorm.DB) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Category) AfterCreate(tx *gorm.DB) error {
|
||||
if c.ParentID == c.ID {
|
||||
return ERROR_CATEGORY_SELF_REFERENCING
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,16 +1,23 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.qowevisa.me/Qowevisa/fin-check-api/db"
|
||||
"git.qowevisa.me/Qowevisa/fin-check-api/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var categoryTransform func(*db.Category) types.DbCategory = func(inp *db.Category) types.DbCategory {
|
||||
nameWithParent := inp.Name
|
||||
if inp.Parent != nil {
|
||||
nameWithParent = fmt.Sprintf("%s -> %s", inp.Parent.Name, inp.Name)
|
||||
}
|
||||
return types.DbCategory{
|
||||
ID: inp.ID,
|
||||
Name: inp.Name,
|
||||
ParentID: inp.ParentID,
|
||||
NameWithParent: nameWithParent,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,7 +57,7 @@ func CategoryGetAll(c *gin.Context) {
|
|||
}
|
||||
dbc := db.Connect()
|
||||
var entities []*db.Category
|
||||
if err := dbc.Find(&entities, db.Category{UserID: userID}).Error; err != nil {
|
||||
if err := dbc.Preload("Parent").Find(&entities, db.Category{UserID: userID}).Error; err != nil {
|
||||
c.JSON(500, types.ErrorResponse{Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
|
|
@ -42,6 +42,8 @@ type DbCategory struct {
|
|||
// Parent is used as a infinite sub-category structure
|
||||
// Can be 0
|
||||
ParentID uint `json:"parent_id" example:"0"`
|
||||
// Purely UI things
|
||||
NameWithParent string `json:"name_with_parent" example:"World -> Moldova"`
|
||||
}
|
||||
|
||||
type DbDebt struct {
|
||||
|
|
Loading…
Reference in New Issue
Block a user