hailin/internal/mods/rbac/biz/article.biz.go
2025-06-19 10:30:46 +08:00

101 lines
2.4 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 Article struct {
Cache cachex.Cacher
Trans *util.Trans
ArticleDAL *dal.Article
}
// Query roles from the data access object based on the provided parameters and options.
func (a *Article) Query(ctx context.Context, params schema.ArticleQueryParam) (*schema.ArticleQueryResult, error) {
params.Pagination = true
result, err := a.ArticleDAL.Query(ctx, params, schema.ArticleQueryOptions{
QueryOptions: util.QueryOptions{
OrderFields: []util.OrderByParam{
{Field: "created_at", Direction: util.DESC},
{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 *Article) Get(ctx context.Context, id string) (*schema.Article, error) {
article, err := a.ArticleDAL.Get(ctx, id)
if err != nil {
return nil, err
} else if article == nil {
return nil, errors.NotFound("", "Banner not found")
}
return article, nil
}
// Create a new role in the data access object.
func (a *Article) Create(ctx context.Context, formItem *schema.ArticleForm) (*schema.Article, error) {
article := &schema.Article{
ID: util.NewXID(),
CreatedAt: time.Now(),
}
if err := formItem.FillTo(article); err != nil {
return nil, err
}
if err := a.ArticleDAL.Create(ctx, article); err != nil {
return nil, err
}
return article, nil
}
// Update the specified role in the data access object.
func (a *Article) Update(ctx context.Context, id string, formItem *schema.ArticleForm) error {
article, err := a.ArticleDAL.Get(ctx, id)
if err != nil {
return err
}
if err := formItem.FillTo(article); err != nil {
return err
}
article.UpdatedAt = time.Now()
if err := a.ArticleDAL.Update(ctx, article); err != nil {
return err
}
return nil
}
// Delete the specified role from the data access object.
func (a *Article) Delete(ctx context.Context, id string) error {
exists, err := a.ArticleDAL.Exists(ctx, id)
if err != nil {
return err
} else if !exists {
return errors.NotFound("", "Role not found")
}
if err := a.ArticleDAL.Delete(ctx, id); err != nil {
return err
}
return nil
}