95 lines
2.0 KiB
Go
95 lines
2.0 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"
|
|
)
|
|
|
|
type Team struct {
|
|
Cache cachex.Cacher
|
|
Trans *util.Trans
|
|
TeamDAL *dal.Team
|
|
}
|
|
|
|
func (a *Team) Query(ctx context.Context, params schema.TeamQueryParam) (*schema.TeamQueryResult, error) {
|
|
params.Pagination = true
|
|
result, err := a.TeamDAL.Query(ctx, params, schema.TeamQueryOptions{
|
|
QueryOptions: util.QueryOptions{
|
|
OrderFields: []util.OrderByParam{
|
|
{Field: "sequence", Direction: util.DESC},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Get the specified role from the data access object.
|
|
func (a *Team) Get(ctx context.Context, id string) (*schema.Team, error) {
|
|
item, err := a.TeamDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if item == nil {
|
|
return nil, errors.NotFound("", "Banner not found")
|
|
}
|
|
|
|
return item, nil
|
|
}
|
|
|
|
func (a *Team) Create(ctx context.Context, formItem *schema.TeamForm) (*schema.Team, error) {
|
|
|
|
team := &schema.Team{
|
|
ID: util.NewXID(),
|
|
CreatedAt: time.Now(),
|
|
}
|
|
if err := formItem.FillTo(team); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := a.TeamDAL.Create(ctx, team); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return team, nil
|
|
}
|
|
|
|
func (a *Team) Update(ctx context.Context, id string, formItem *schema.TeamForm) error {
|
|
team, err := a.TeamDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := formItem.FillTo(team); err != nil {
|
|
return err
|
|
}
|
|
team.UpdatedAt = time.Now()
|
|
|
|
if err := a.TeamDAL.Update(ctx, team); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *Team) Delete(ctx context.Context, id string) error {
|
|
exists, err := a.TeamDAL.Exists(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if !exists {
|
|
return errors.NotFound("", "Role not found")
|
|
}
|
|
|
|
if err := a.TeamDAL.Delete(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
|
|
}
|