179 lines
4.3 KiB
Go
179 lines
4.3 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/guxuan/hailin_service/internal/mods/rbac/dal"
|
|
"github.com/guxuan/hailin_service/internal/mods/rbac/schema"
|
|
"github.com/guxuan/hailin_service/pkg/cachex"
|
|
"github.com/guxuan/hailin_service/pkg/errors"
|
|
"github.com/guxuan/hailin_service/pkg/util"
|
|
)
|
|
|
|
// Role management for RBAC
|
|
type Job struct {
|
|
Cache cachex.Cacher
|
|
Trans *util.Trans
|
|
JobDAL *dal.Job
|
|
JobAreaDAL *dal.JobArea
|
|
}
|
|
|
|
// Query roles from the data access object based on the provided parameters and options.
|
|
func (a *Job) QueryJobArea(ctx context.Context) (*[]schema.JobArea, error) {
|
|
return a.JobAreaDAL.Query(ctx)
|
|
}
|
|
|
|
// Create a new role in the data access object.
|
|
func (a *Job) CreateJobArea(ctx context.Context, formItem *schema.JobAreaForm) (*schema.JobArea, error) {
|
|
area := &schema.JobArea{
|
|
ID: util.NewXID(),
|
|
CreatedAt: time.Now(),
|
|
}
|
|
area.Name = formItem.Name
|
|
area.Status = formItem.Status
|
|
if err := a.JobAreaDAL.Create(ctx, area); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return area, nil
|
|
}
|
|
|
|
func (a *Job) QueryWebJobList(ctx context.Context) ([]*schema.WebJobData, error) {
|
|
var result []*schema.WebJobData
|
|
|
|
query, err := a.JobAreaDAL.Query(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, area := range *query {
|
|
if area.Status == "enabled" {
|
|
queryResult, err := a.JobDAL.Query(ctx, schema.JobQueryParam{JobAreaID: area.ID, Status: "enabled"}, schema.JobQueryOptions{
|
|
QueryOptions: util.QueryOptions{
|
|
OrderFields: []util.OrderByParam{
|
|
{Field: "sequence", Direction: util.ASC},
|
|
},
|
|
}})
|
|
if err == nil {
|
|
var info schema.WebJobData
|
|
info.JobAreaTitle = area.Name
|
|
info.JobList = queryResult.Data
|
|
result = append(result, &info)
|
|
}
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Update the specified role in the data access object.
|
|
func (a *Job) UpdateJobArea(ctx context.Context, id string, formItem *schema.JobAreaForm) error {
|
|
area, err := a.JobAreaDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
area.UpdatedAt = time.Now()
|
|
area.Name = formItem.Name
|
|
area.Status = formItem.Status
|
|
if err := a.JobAreaDAL.Update(ctx, area); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete the specified role from the data access object.
|
|
func (a *Job) DeleteJobArea(ctx context.Context, id string) error {
|
|
exists, err := a.JobAreaDAL.Exists(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if !exists {
|
|
return errors.NotFound("", "Role not found")
|
|
}
|
|
|
|
if err := a.JobAreaDAL.Delete(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Query roles from the data access object based on the provided parameters and options.
|
|
func (a *Job) Query(ctx context.Context, params schema.JobQueryParam) (*schema.JobQueryResult, error) {
|
|
params.Pagination = true
|
|
result, err := a.JobDAL.Query(ctx, params, schema.JobQueryOptions{
|
|
QueryOptions: util.QueryOptions{
|
|
OrderFields: []util.OrderByParam{
|
|
{Field: "sequence", Direction: util.ASC},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// Get the specified role from the data access object.
|
|
func (a *Job) Get(ctx context.Context, id string) (*schema.Job, error) {
|
|
job, err := a.JobDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if job == nil {
|
|
return nil, errors.NotFound("", "Banner not found")
|
|
}
|
|
|
|
return job, nil
|
|
}
|
|
|
|
// Create a new role in the data access object.
|
|
func (a *Job) Create(ctx context.Context, formItem *schema.JobForm) (*schema.Job, error) {
|
|
|
|
job := &schema.Job{
|
|
ID: util.NewXID(),
|
|
CreatedAt: time.Now(),
|
|
}
|
|
if err := formItem.FillTo(job); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := a.JobDAL.Create(ctx, job); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return job, nil
|
|
}
|
|
|
|
// Update the specified role in the data access object.
|
|
func (a *Job) Update(ctx context.Context, id string, formItem *schema.JobForm) error {
|
|
job, err := a.JobDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := formItem.FillTo(job); err != nil {
|
|
return err
|
|
}
|
|
job.UpdatedAt = time.Now()
|
|
|
|
if err := a.JobDAL.Update(ctx, job); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete the specified role from the data access object.
|
|
func (a *Job) Delete(ctx context.Context, id string) error {
|
|
exists, err := a.JobDAL.Exists(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if !exists {
|
|
return errors.NotFound("", "Role not found")
|
|
}
|
|
|
|
if err := a.JobDAL.Delete(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
|
|
}
|