swirl/biz/chart.go
2022-01-06 16:54:14 +08:00

70 lines
1.8 KiB
Go

package biz
import (
"context"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/dao"
)
type ChartBiz interface {
Search(ctx context.Context, args *dao.ChartSearchArgs) (charts []*dao.Chart, total int, err error)
Delete(ctx context.Context, id, title string, user web.User) (err error)
Find(ctx context.Context, id string) (chart *dao.Chart, err error)
Create(ctx context.Context, chart *dao.Chart, user web.User) (err error)
Update(ctx context.Context, chart *dao.Chart, user web.User) (err error)
}
func NewChart(d dao.Interface, mb MetricBiz, eb EventBiz) ChartBiz {
return &chartBiz{
d: d,
mb: mb,
eb: eb,
}
}
type chartBiz struct {
d dao.Interface
mb MetricBiz
eb EventBiz
}
func (b *chartBiz) Search(ctx context.Context, args *dao.ChartSearchArgs) (charts []*dao.Chart, total int, err error) {
return b.d.ChartSearch(ctx, args)
}
func (b *chartBiz) Create(ctx context.Context, chart *dao.Chart, user web.User) (err error) {
chart.ID = createId()
chart.CreatedAt = now()
chart.CreatedBy = newOperator(user)
chart.UpdatedAt = chart.CreatedAt
chart.UpdatedBy = chart.CreatedBy
err = b.d.ChartCreate(ctx, chart)
if err == nil {
b.eb.CreateChart(EventActionCreate, chart.ID, chart.Title, user)
}
return
}
func (b *chartBiz) Delete(ctx context.Context, id, title string, user web.User) (err error) {
err = b.d.ChartDelete(ctx, id)
if err == nil {
b.eb.CreateChart(EventActionDelete, id, title, user)
}
return
}
func (b *chartBiz) Find(ctx context.Context, id string) (chart *dao.Chart, err error) {
return b.d.ChartGet(ctx, id)
}
func (b *chartBiz) Update(ctx context.Context, chart *dao.Chart, user web.User) (err error) {
chart.UpdatedAt = now()
chart.UpdatedBy = newOperator(user)
err = b.d.ChartUpdate(ctx, chart)
if err == nil {
b.eb.CreateChart(EventActionUpdate, chart.ID, chart.Title, user)
}
return
}