swirl/biz/chart.go

70 lines
1.8 KiB
Go
Raw Permalink Normal View History

2018-03-22 08:13:54 +00:00
package biz
import (
2021-12-06 12:24:22 +00:00
"context"
2018-03-22 08:13:54 +00:00
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/dao"
)
2021-12-06 12:24:22 +00:00
type ChartBiz interface {
2022-01-06 08:54:14 +00:00
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)
2021-12-06 12:24:22 +00:00
}
2018-03-22 08:13:54 +00:00
2021-12-06 12:24:22 +00:00
func NewChart(d dao.Interface, mb MetricBiz, eb EventBiz) ChartBiz {
return &chartBiz{
d: d,
mb: mb,
eb: eb,
}
}
2021-12-06 12:24:22 +00:00
type chartBiz struct {
2021-12-16 12:23:08 +00:00
d dao.Interface
mb MetricBiz
eb EventBiz
2018-03-22 08:13:54 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *chartBiz) Search(ctx context.Context, args *dao.ChartSearchArgs) (charts []*dao.Chart, total int, err error) {
return b.d.ChartSearch(ctx, args)
2018-03-22 08:13:54 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *chartBiz) Create(ctx context.Context, chart *dao.Chart, user web.User) (err error) {
2021-12-16 12:23:08 +00:00
chart.ID = createId()
chart.CreatedAt = now()
chart.CreatedBy = newOperator(user)
chart.UpdatedAt = chart.CreatedAt
chart.UpdatedBy = chart.CreatedBy
2022-01-06 08:54:14 +00:00
err = b.d.ChartCreate(ctx, chart)
2021-12-06 12:24:22 +00:00
if err == nil {
2021-12-16 12:23:08 +00:00
b.eb.CreateChart(EventActionCreate, chart.ID, chart.Title, user)
2021-12-06 12:24:22 +00:00
}
2018-03-22 08:13:54 +00:00
return
}
2022-01-06 08:54:14 +00:00
func (b *chartBiz) Delete(ctx context.Context, id, title string, user web.User) (err error) {
err = b.d.ChartDelete(ctx, id)
2021-12-06 12:24:22 +00:00
if err == nil {
b.eb.CreateChart(EventActionDelete, id, title, user)
}
2018-03-22 08:13:54 +00:00
return
}
2022-01-06 08:54:14 +00:00
func (b *chartBiz) Find(ctx context.Context, id string) (chart *dao.Chart, err error) {
return b.d.ChartGet(ctx, id)
2018-03-22 08:13:54 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *chartBiz) Update(ctx context.Context, chart *dao.Chart, user web.User) (err error) {
2021-12-16 12:23:08 +00:00
chart.UpdatedAt = now()
chart.UpdatedBy = newOperator(user)
2022-01-06 08:54:14 +00:00
err = b.d.ChartUpdate(ctx, chart)
2021-12-06 12:24:22 +00:00
if err == nil {
b.eb.CreateChart(EventActionUpdate, chart.ID, chart.Title, user)
}
2018-03-22 08:13:54 +00:00
return
}