swirl/biz/chart.go

77 lines
1.9 KiB
Go
Raw 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"
"github.com/cuigh/swirl/model"
)
2021-12-06 12:24:22 +00:00
type ChartBiz interface {
2021-12-16 12:23:08 +00:00
Search(args *model.ChartSearchArgs) (charts []*model.Chart, total int, err error)
2021-12-06 12:24:22 +00:00
Delete(id, title string, user web.User) (err error)
2021-12-16 12:23:08 +00:00
Find(id string) (chart *model.Chart, err error)
2021-12-06 12:24:22 +00:00
Batch(ids ...string) (charts []*model.Chart, err error)
2021-12-16 12:23:08 +00:00
Create(chart *model.Chart, user web.User) (err error)
Update(chart *model.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
}
2021-12-16 12:23:08 +00:00
func (b *chartBiz) Search(args *model.ChartSearchArgs) (charts []*model.Chart, total int, err error) {
return b.d.ChartSearch(context.TODO(), args)
2018-03-22 08:13:54 +00:00
}
2021-12-16 12:23:08 +00:00
func (b *chartBiz) Create(chart *model.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(context.TODO(), 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
}
2021-12-06 12:24:22 +00:00
func (b *chartBiz) Delete(id, title string, user web.User) (err error) {
err = b.d.ChartDelete(context.TODO(), id)
if err == nil {
b.eb.CreateChart(EventActionDelete, id, title, user)
}
2018-03-22 08:13:54 +00:00
return
}
2021-12-16 12:23:08 +00:00
func (b *chartBiz) Find(id string) (chart *model.Chart, err error) {
return b.d.ChartGet(context.TODO(), id)
2018-03-22 08:13:54 +00:00
}
2021-12-06 12:24:22 +00:00
func (b *chartBiz) Batch(ids ...string) (charts []*model.Chart, err error) {
2021-12-16 08:11:16 +00:00
charts, err = b.d.ChartGetBatch(context.TODO(), ids...)
return
}
2021-12-16 12:23:08 +00:00
func (b *chartBiz) Update(chart *model.Chart, user web.User) (err error) {
chart.UpdatedAt = now()
chart.UpdatedBy = newOperator(user)
err = b.d.ChartUpdate(context.TODO(), 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
}