swirl/biz/chart.go

76 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"
)
2021-12-06 12:24:22 +00:00
type ChartBiz interface {
2021-12-23 11:28:31 +00:00
Search(args *dao.ChartSearchArgs) (charts []*dao.Chart, total int, err error)
2021-12-06 12:24:22 +00:00
Delete(id, title string, user web.User) (err error)
2021-12-23 11:28:31 +00:00
Find(id string) (chart *dao.Chart, err error)
Batch(ids ...string) (charts []*dao.Chart, err error)
Create(chart *dao.Chart, user web.User) (err error)
Update(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
}
2021-12-23 11:28:31 +00:00
func (b *chartBiz) Search(args *dao.ChartSearchArgs) (charts []*dao.Chart, total int, err error) {
2021-12-16 12:23:08 +00:00
return b.d.ChartSearch(context.TODO(), args)
2018-03-22 08:13:54 +00:00
}
2021-12-23 11:28:31 +00:00
func (b *chartBiz) Create(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
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-23 11:28:31 +00:00
func (b *chartBiz) Find(id string) (chart *dao.Chart, err error) {
2021-12-16 12:23:08 +00:00
return b.d.ChartGet(context.TODO(), id)
2018-03-22 08:13:54 +00:00
}
2021-12-23 11:28:31 +00:00
func (b *chartBiz) Batch(ids ...string) (charts []*dao.Chart, err error) {
2021-12-16 08:11:16 +00:00
charts, err = b.d.ChartGetBatch(context.TODO(), ids...)
return
}
2021-12-23 11:28:31 +00:00
func (b *chartBiz) Update(chart *dao.Chart, user web.User) (err error) {
2021-12-16 12:23:08 +00:00
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
}