swirl/biz/system.go

57 lines
1.1 KiB
Go
Raw Permalink Normal View History

2021-12-06 12:24:22 +00:00
package biz
import (
2022-01-06 08:54:14 +00:00
"context"
2021-12-06 12:24:22 +00:00
"github.com/cuigh/auxo/app"
"github.com/cuigh/auxo/data"
"github.com/cuigh/swirl/dao"
"github.com/cuigh/swirl/misc"
"github.com/docker/docker/api/types/versions"
)
type SystemBiz interface {
2022-01-06 08:54:14 +00:00
Init(ctx context.Context) (err error)
CheckState(ctx context.Context) (state *SystemState, err error)
2021-12-06 12:24:22 +00:00
}
func NewSystem(d dao.Interface, ub UserBiz, sb SettingBiz, s *misc.Setting) SystemBiz {
return &systemBiz{
2021-12-16 12:23:08 +00:00
s: s,
2021-12-06 12:24:22 +00:00
d: d,
ub: ub,
sb: sb,
}
}
type systemBiz struct {
2021-12-16 12:23:08 +00:00
s *misc.Setting
2021-12-06 12:24:22 +00:00
d dao.Interface
ub UserBiz
sb SettingBiz
}
2022-01-06 08:54:14 +00:00
func (b *systemBiz) Init(ctx context.Context) (err error) {
2021-12-06 12:24:22 +00:00
if versions.LessThan(b.s.System.Version, app.Version) {
2022-02-10 03:09:03 +00:00
// upgrade database
err = b.d.Upgrade(ctx)
2021-12-06 12:24:22 +00:00
if err == nil {
2022-01-06 08:54:14 +00:00
err = b.sb.Save(ctx, "system", data.Map{"version": app.Version}, nil)
2021-12-06 12:24:22 +00:00
}
}
return
}
2022-01-06 08:54:14 +00:00
func (b *systemBiz) CheckState(ctx context.Context) (state *SystemState, err error) {
2021-12-06 12:24:22 +00:00
var count int
2022-01-06 08:54:14 +00:00
count, err = b.ub.Count(ctx)
2021-12-06 12:24:22 +00:00
if err == nil {
state = &SystemState{Fresh: count == 0}
}
return
}
type SystemState struct {
Fresh bool `json:"fresh"`
}