100 lines
2.5 KiB
Go
100 lines
2.5 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 Memorabilia struct {
|
|
Cache cachex.Cacher
|
|
Trans *util.Trans
|
|
MemorabiliaDAL *dal.Memorabilia
|
|
}
|
|
|
|
// Query roles from the data access object based on the provided parameters and options.
|
|
func (a *Memorabilia) Query(ctx context.Context, params schema.MemorabiliaQueryParam) (*schema.MemorabiliaQueryResult, error) {
|
|
params.Pagination = true
|
|
result, err := a.MemorabiliaDAL.Query(ctx, params, schema.MemorabiliaQueryOptions{
|
|
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 *Memorabilia) Get(ctx context.Context, id string) (*schema.Memorabilia, error) {
|
|
memorabilia, err := a.MemorabiliaDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if memorabilia == nil {
|
|
return nil, errors.NotFound("", "Banner not found")
|
|
}
|
|
|
|
return memorabilia, nil
|
|
}
|
|
|
|
// Create a new role in the data access object.
|
|
func (a *Memorabilia) Create(ctx context.Context, formItem *schema.MemorabiliaForm) (*schema.Memorabilia, error) {
|
|
|
|
memorabilia := &schema.Memorabilia{
|
|
ID: util.NewXID(),
|
|
CreatedAt: time.Now(),
|
|
}
|
|
if err := formItem.FillTo(memorabilia); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := a.MemorabiliaDAL.Create(ctx, memorabilia); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return memorabilia, nil
|
|
}
|
|
|
|
// Update the specified role in the data access object.
|
|
func (a *Memorabilia) Update(ctx context.Context, id string, formItem *schema.MemorabiliaForm) error {
|
|
memorabilia, err := a.MemorabiliaDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := formItem.FillTo(memorabilia); err != nil {
|
|
return err
|
|
}
|
|
memorabilia.UpdatedAt = time.Now()
|
|
|
|
if err := a.MemorabiliaDAL.Update(ctx, memorabilia); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete the specified role from the data access object.
|
|
func (a *Memorabilia) Delete(ctx context.Context, id string) error {
|
|
exists, err := a.MemorabiliaDAL.Exists(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if !exists {
|
|
return errors.NotFound("", "Role not found")
|
|
}
|
|
|
|
if err := a.MemorabiliaDAL.Delete(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
|
|
}
|