db 管理初始化

This commit is contained in:
wuchao 2024-11-27 16:54:01 +08:00
parent 2f915f225d
commit 3212eb6ae1
7 changed files with 30 additions and 5 deletions

View File

@ -5,3 +5,8 @@ const (
TOKEN_Admin = "Admin_token_" TOKEN_Admin = "Admin_token_"
ADMIN_OAUTH_V1 = "/admin/api/oauth/v1" ADMIN_OAUTH_V1 = "/admin/api/oauth/v1"
) )
const (
STATUS_DISABLE = 1
STATUS_ENABLE = 2
)

View File

@ -24,4 +24,6 @@ func DbList(c *gin.Context) {
func DbAdd(c *gin.Context) { func DbAdd(c *gin.Context) {
request := controllers.GetRequest(c).(*backend.DbAddRequest) request := controllers.GetRequest(c).(*backend.DbAddRequest)
err := db_service.DbAdd(request)
controllers.HandRes(c, nil, err)
} }

View File

@ -144,7 +144,7 @@ func GetRequest(c *gin.Context) interface{} {
func HandRes(c *gin.Context, data interface{}, err error) { func HandRes(c *gin.Context, data interface{}, err error) {
if err == nil { if err == nil {
Success(c, data, "") Success(c, data, "请求成功")
} else { } else {
Error(c, errorcode.SystemError, err.Error()) Error(c, errorcode.SystemError, err.Error())
} }

View File

@ -1,5 +1,7 @@
package backend package backend
import "cron_admin/app/http/entities"
type DbListRequest struct { type DbListRequest struct {
Page int `json:"page" validate:"required" form:"page" example:"1"` Page int `json:"page" validate:"required" form:"page" example:"1"`
Limit int `json:"limit" validate:"required" form:"limit" example:"10"` Limit int `json:"limit" validate:"required" form:"limit" example:"10"`
@ -20,4 +22,11 @@ type DbListResponse struct {
} }
type DbAddRequest struct { type DbAddRequest struct {
entities.PageRequest
DbName string `json:"db_name" validate:"required" form:"db_name" example:""`
DbType string `json:"db_type" validate:"required" form:"db_type" example:"mysql"`
DbPermission int `json:"db_permission" validate:"required" form:"db_permission" example:"1"`
Source string `json:"source" validate:"required" form:"source" example:""`
Desc string `json:"desc" form:"desc" example:""`
Status int `json:"status" form:"status" example:"1"`
} }

View File

@ -7,7 +7,6 @@ import (
var BackendRequestMap = map[string]func() interface{}{ var BackendRequestMap = map[string]func() interface{}{
common.ADMIN_OAUTH_V1 + "/sql/list": func() interface{} { common.ADMIN_OAUTH_V1 + "/sql/list": func() interface{} { return new(backend.DbListRequest) },
return new(backend.DbListRequest) common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) },
},
} }

View File

@ -39,6 +39,7 @@ func RegisterAdminRoute(router *gin.Engine) {
sql := v1.Group("/sql") sql := v1.Group("/sql")
{ {
sql.POST("/list", backend.DbList) sql.POST("/list", backend.DbList)
sql.POST("/add", backend.DbAdd)
} }
//任务 //任务
cmd := v1.Group("/cmd") cmd := v1.Group("/cmd")

View File

@ -1,8 +1,10 @@
package db_service package db_service
import ( import (
"cron_admin/app/constants/common"
"cron_admin/app/http/entities/backend" "cron_admin/app/http/entities/backend"
"cron_admin/app/models/crondbmodel" "cron_admin/app/models/crondbmodel"
"cron_admin/app/utils/mapstructure"
"xorm.io/builder" "xorm.io/builder"
) )
@ -26,5 +28,12 @@ func DbList(request *backend.DbListRequest, page int, limit int) (count int64, D
return return
} }
return return
}
func DbAdd(request *backend.DbAddRequest) (err error) {
var db crondbmodel.CronDb
_ = mapstructure.Decode(request, &db)
db.Status = common.STATUS_DISABLE
_, err = crondbmodel.GetInstance().GetDb().InsertOne(db)
return
} }