diff --git a/database/seeders/sys_role.go b/database/seeders/sys_role.go index e66bdf1..43e731d 100755 --- a/database/seeders/sys_role.go +++ b/database/seeders/sys_role.go @@ -25,7 +25,7 @@ func NewSysRoleSeeder() contracts.Seeder { } func (s *SysRoleSeeder) Run(faker *gofakeit.Faker, db *gorm.DB) { - times := 10 + times := 50 for i := 0; i < times; i++ { data := s.Generate(faker, i) if i == 0 { @@ -38,10 +38,27 @@ func (s *SysRoleSeeder) Run(faker *gofakeit.Faker, db *gorm.DB) { } func (s *SysRoleSeeder) Generate(faker *gofakeit.Faker, idx int) models.SysRole { + parentID := 0 + if idx > 10 { + parentID = faker.IntRange(1, 10) + } + + if idx > 20 { + parentID = faker.IntRange(10, 20) + } + + if idx > 30 { + parentID = faker.IntRange(20, 30) + } + + if idx > 40 { + parentID = faker.IntRange(30, 40) + } + return models.SysRole{ UUID: faker.UUID(), Name: faker.Name(), - ParentID: uint64(faker.IntRange(1, 100)), + ParentID: uint64(parentID), DefaultRouter: faker.Name(), } } diff --git a/modules/auth/controller/role.go b/modules/auth/controller/role.go index 99cdbf0..d4b298b 100755 --- a/modules/auth/controller/role.go +++ b/modules/auth/controller/role.go @@ -12,6 +12,7 @@ import ( type RoleController interface { GetByFilter(*gin.Context, dto.RoleRequestFilter, request.PageFilter) (*response.PageResponse[*models.SysRole], error) + Tree(*gin.Context) ([]*dto.RoleTree, error) Create(*gin.Context, dto.RoleRequestForm) error Delete(*gin.Context, int) error UpdateByID(*gin.Context, int, dto.RoleRequestForm) error @@ -36,6 +37,9 @@ func (c *roleControllerImpl) GetByFilter( ) (*response.PageResponse[*models.SysRole], error) { return c.roleSvc.GetByFilter(ctx, filter, page) } +func (c *roleControllerImpl) Tree(ctx *gin.Context) ([]*dto.RoleTree, error) { + return c.roleSvc.Tree(ctx) +} func (c *roleControllerImpl) Create(ctx *gin.Context, req dto.RoleRequestForm) error { _, err := c.roleSvc.Create(ctx, req) diff --git a/modules/auth/dao/role.go b/modules/auth/dao/role.go index 49eb73f..80d74fe 100755 --- a/modules/auth/dao/role.go +++ b/modules/auth/dao/role.go @@ -15,6 +15,7 @@ type RoleDao interface { UpdateByID(context.Context, *models.SysRole) (*models.SysRole, error) DeleteByID(context.Context, uint64) error DeletePermanentlyByID(context.Context, uint64) error + All(context.Context) ([]*models.SysRole, error) } type roleDaoImpl struct { @@ -54,6 +55,11 @@ func (dao *roleDaoImpl) GetByFilter(ctx context.Context, filter dto.RoleRequestF return items, uint64(total), nil } +func (dao *roleDaoImpl) All(ctx context.Context) ([]*models.SysRole, error) { + role := dao.query.SysRole + return role.WithContext(ctx).Find() +} + func (dao *roleDaoImpl) FindByID(ctx context.Context, id uint64) (*models.SysRole, error) { role := dao.query.SysRole return role.WithContext(ctx).Where(role.ID.Eq(id)).First() diff --git a/modules/auth/dao/user.go b/modules/auth/dao/user.go index 4e99d0e..33d5e66 100755 --- a/modules/auth/dao/user.go +++ b/modules/auth/dao/user.go @@ -17,6 +17,7 @@ type userDaoImpl struct { func NewUserDao(query *query.Query) UserDao { return &userDaoImpl{query: query} } + func (dao *userDaoImpl) FindByID(ctx context.Context, id uint64) (*models.User, error) { user := dao.query.User return user.WithContext(ctx).Where(user.ID.Eq(id)).First() diff --git a/modules/auth/dto/role.go b/modules/auth/dto/role.go index a60a62e..e8f9e63 100644 --- a/modules/auth/dto/role.go +++ b/modules/auth/dto/role.go @@ -11,3 +11,12 @@ type RoleRequestForm struct { ParentID uint `json:"parent_id,omitempty"` DefaultRouter string `json:"default_router,omitempty"` } + +type RoleTree struct { + ID uint64 `json:"id,omitempty"` + UUID string `json:"uuid,omitempty"` + Name string `json:"name,omitempty"` + ParentID uint64 `json:"parent_id,omitempty"` + DefaultRouter string `json:"default_router,omitempty"` + Children []*RoleTree `json:"children,omitempty"` +} diff --git a/modules/auth/routes/routes.go b/modules/auth/routes/routes.go index afb979e..33cf46e 100755 --- a/modules/auth/routes/routes.go +++ b/modules/auth/routes/routes.go @@ -40,6 +40,8 @@ func (r *Route) Register() { gen.BindQuery(request.PageFilter{}, err.BindQueryFailed), )) + roleGroup.GET("/tree", gen.DataFunc(r.role.Tree)) + roleGroup.POST("", gen.Func1( r.role.Create, gen.BindBody(dto.RoleRequestForm{}, err.BindBodyFailed), diff --git a/modules/auth/service/role.go b/modules/auth/service/role.go index 901c28e..fa55b36 100755 --- a/modules/auth/service/role.go +++ b/modules/auth/service/role.go @@ -13,6 +13,7 @@ import ( type RoleService interface { GetByFilter(context.Context, dto.RoleRequestFilter, request.PageFilter) (*response.PageResponse[*models.SysRole], error) Create(context.Context, dto.RoleRequestForm) (*models.SysRole, error) + Tree(context.Context) ([]*dto.RoleTree, error) UpdateByID(context.Context, uint64, dto.RoleRequestForm) (*models.SysRole, error) DeleteByID(context.Context, uint64) error } @@ -48,6 +49,32 @@ func (svc *roleService) GetByFilter( }, nil } +func (svc *roleService) treeMaker(ctx context.Context, models []*models.SysRole, pid uint64) []*dto.RoleTree { + items := []*dto.RoleTree{} + for _, model := range models { + if model.ParentID == pid { + items = append(items, &dto.RoleTree{ + ID: model.ID, + UUID: model.UUID, + Name: model.Name, + ParentID: 0, + DefaultRouter: model.DefaultRouter, + Children: svc.treeMaker(ctx, models, model.ID), + }) + } + } + return items +} + +func (svc *roleService) Tree(ctx context.Context) ([]*dto.RoleTree, error) { + models, err := svc.dao.All(ctx) + if err != nil { + return nil, err + } + + return svc.treeMaker(ctx, models, 0), nil +} + func (svc *roleService) Create(ctx context.Context, req dto.RoleRequestForm) (*models.SysRole, error) { model := models.SysRole{ UUID: svc.uuid.MustGenerate(),