Refactor code

This commit is contained in:
cuigh
2021-12-16 20:23:08 +08:00
parent c3115b952c
commit 6705bd6d64
51 changed files with 848 additions and 1176 deletions

View File

@@ -39,7 +39,7 @@ func chartSearch(b biz.ChartBiz) web.HandlerFunc {
return func(ctx web.Context) (err error) {
var (
args = &model.ChartSearchArgs{}
charts []*biz.Chart
charts []*model.Chart
total int
)
@@ -85,7 +85,7 @@ func chartDelete(b biz.ChartBiz) web.HandlerFunc {
func chartSave(b biz.ChartBiz) web.HandlerFunc {
return func(ctx web.Context) error {
r := &biz.Chart{}
r := &model.Chart{}
err := ctx.Bind(r, true)
if err == nil {
if r.ID == "" {
@@ -122,7 +122,7 @@ func chartFetchData(b biz.ChartBiz) web.HandlerFunc {
func chartFindDashboard(b biz.ChartBiz) web.HandlerFunc {
return func(ctx web.Context) (err error) {
var (
d *biz.Dashboard
d *model.Dashboard
name = ctx.Query("name")
key = ctx.Query("key")
)

View File

@@ -26,7 +26,7 @@ func eventSearch(b biz.EventBiz) web.HandlerFunc {
return func(ctx web.Context) (err error) {
var (
args = &model.EventSearchArgs{}
events []*biz.Event
events []*model.Event
total int
)

View File

@@ -3,6 +3,7 @@ package api
import (
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
"github.com/cuigh/swirl/model"
)
// RegistryHandler encapsulates registry related handlers.
@@ -60,7 +61,7 @@ func registryDelete(b biz.RegistryBiz) web.HandlerFunc {
func registrySave(b biz.RegistryBiz) web.HandlerFunc {
return func(ctx web.Context) error {
r := &biz.Registry{}
r := &model.Registry{}
err := ctx.Bind(r, true)
if err == nil {
if r.ID == "" {

View File

@@ -3,6 +3,7 @@ package api
import (
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
"github.com/cuigh/swirl/model"
)
// RoleHandler encapsulates role related handlers.
@@ -61,7 +62,7 @@ func roleDelete(b biz.RoleBiz) web.HandlerFunc {
func roleSave(b biz.RoleBiz) web.HandlerFunc {
return func(ctx web.Context) error {
r := &biz.Role{}
r := &model.Role{}
err := ctx.Bind(r, true)
if err == nil {
if r.ID == "" {

View File

@@ -6,6 +6,7 @@ import (
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
"github.com/cuigh/swirl/docker/compose"
"github.com/cuigh/swirl/model"
)
// StackHandler encapsulates stack related handlers.
@@ -53,7 +54,7 @@ func stackSearch(b biz.StackBiz) web.HandlerFunc {
return func(ctx web.Context) (err error) {
var (
args = &Args{}
stacks []*biz.Stack
stacks []*model.Stack
)
if err = ctx.Bind(args); err == nil {
@@ -119,8 +120,13 @@ func stackDeploy(b biz.StackBiz) web.HandlerFunc {
}
func stackSave(b biz.StackBiz) web.HandlerFunc {
type Args struct {
ID string `json:"id"`
model.Stack
}
return func(ctx web.Context) error {
stack := &biz.Stack{}
stack := &Args{}
err := ctx.Bind(stack, true)
if err != nil {
return err
@@ -133,9 +139,9 @@ func stackSave(b biz.StackBiz) web.HandlerFunc {
}
if stack.ID == "" {
err = b.Create(stack, ctx.User())
err = b.Create(&stack.Stack, ctx.User())
} else {
err = b.Update(stack, ctx.User())
err = b.Update(&stack.Stack, ctx.User())
}
return ajax(ctx, err)
}

View File

@@ -6,9 +6,12 @@ import (
"github.com/cuigh/auxo/app"
"github.com/cuigh/auxo/data"
"github.com/cuigh/auxo/errors"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
"github.com/cuigh/swirl/docker"
"github.com/cuigh/swirl/misc"
"github.com/cuigh/swirl/model"
)
//var ErrSystemInitialized = errors.New("system was already initialized")
@@ -22,10 +25,10 @@ type SystemHandler struct {
}
// NewSystem creates an instance of SystemHandler
func NewSystem(d *docker.Docker, b biz.SystemBiz) *SystemHandler {
func NewSystem(d *docker.Docker, b biz.SystemBiz, ub biz.UserBiz) *SystemHandler {
return &SystemHandler{
CheckState: systemCheckState(b),
CreateAdmin: systemCreateAdmin(b),
CreateAdmin: systemCreateAdmin(ub),
Version: systemVersion,
Summarize: systemSummarize(d),
}
@@ -74,12 +77,21 @@ func systemSummarize(d *docker.Docker) web.HandlerFunc {
}
}
func systemCreateAdmin(b biz.SystemBiz) web.HandlerFunc {
func systemCreateAdmin(ub biz.UserBiz) web.HandlerFunc {
return func(c web.Context) (err error) {
user := &biz.User{}
if err = c.Bind(user, true); err == nil {
err = b.CreateAdmin(user)
user := &model.User{}
if err = c.Bind(user, true); err != nil {
return err
}
var count int
if count, err = ub.Count(); err == nil && count > 0 {
return errors.Coded(misc.ErrSystemInitialized, "system was already initialized")
}
user.Admin = true
user.Type = biz.UserTypeInternal
_, err = ub.Create(user, nil)
return ajax(c, err)
}
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/cuigh/auxo/data"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
"github.com/cuigh/swirl/model"
"github.com/cuigh/swirl/security"
)
@@ -69,10 +70,9 @@ func userSignIn(auth *security.Authenticator, eb biz.EventBiz) web.HandlerFunc {
func userSave(b biz.UserBiz) web.HandlerFunc {
return func(ctx web.Context) error {
user := &biz.User{}
user := &model.User{}
err := ctx.Bind(user, true)
if err == nil {
user.Type = biz.UserTypeInternal
if user.ID == "" {
_, err = b.Create(user, ctx.User())
} else {
@@ -171,7 +171,7 @@ func userModifyPassword(b biz.UserBiz) web.HandlerFunc {
func userModifyProfile(b biz.UserBiz) web.HandlerFunc {
return func(ctx web.Context) error {
u := &biz.User{}
u := &model.User{}
err := ctx.Bind(u, true)
if err == nil {
err = b.ModifyProfile(u, ctx.User())