From 3212eb6ae165157c8e38fbfa326b4e5300a8543d Mon Sep 17 00:00:00 2001 From: wuchao <1272174216@qq.com> Date: Wed, 27 Nov 2024 16:54:01 +0800 Subject: [PATCH] =?UTF-8?q?db=20=E7=AE=A1=E7=90=86=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/constants/common/common.go | 5 +++++ app/http/controllers/backend/db_controller.go | 2 ++ app/http/controllers/base.go | 2 +- app/http/entities/backend/db.go | 9 +++++++++ app/http/requestmapping/backend.go | 5 ++--- app/http/routes/admin.go | 1 + app/services/db_service/db_service.go | 11 ++++++++++- 7 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/constants/common/common.go b/app/constants/common/common.go index e731efb..3d3e551 100644 --- a/app/constants/common/common.go +++ b/app/constants/common/common.go @@ -5,3 +5,8 @@ const ( TOKEN_Admin = "Admin_token_" ADMIN_OAUTH_V1 = "/admin/api/oauth/v1" ) + +const ( + STATUS_DISABLE = 1 + STATUS_ENABLE = 2 +) diff --git a/app/http/controllers/backend/db_controller.go b/app/http/controllers/backend/db_controller.go index 2c902c2..1a1a0bb 100644 --- a/app/http/controllers/backend/db_controller.go +++ b/app/http/controllers/backend/db_controller.go @@ -24,4 +24,6 @@ func DbList(c *gin.Context) { func DbAdd(c *gin.Context) { request := controllers.GetRequest(c).(*backend.DbAddRequest) + err := db_service.DbAdd(request) + controllers.HandRes(c, nil, err) } diff --git a/app/http/controllers/base.go b/app/http/controllers/base.go index a6743b7..00972c9 100644 --- a/app/http/controllers/base.go +++ b/app/http/controllers/base.go @@ -144,7 +144,7 @@ func GetRequest(c *gin.Context) interface{} { func HandRes(c *gin.Context, data interface{}, err error) { if err == nil { - Success(c, data, "") + Success(c, data, "请求成功") } else { Error(c, errorcode.SystemError, err.Error()) } diff --git a/app/http/entities/backend/db.go b/app/http/entities/backend/db.go index 6d49c98..16b6de4 100644 --- a/app/http/entities/backend/db.go +++ b/app/http/entities/backend/db.go @@ -1,5 +1,7 @@ package backend +import "cron_admin/app/http/entities" + type DbListRequest struct { Page int `json:"page" validate:"required" form:"page" example:"1"` Limit int `json:"limit" validate:"required" form:"limit" example:"10"` @@ -20,4 +22,11 @@ type DbListResponse 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"` } diff --git a/app/http/requestmapping/backend.go b/app/http/requestmapping/backend.go index 332083e..86de45f 100644 --- a/app/http/requestmapping/backend.go +++ b/app/http/requestmapping/backend.go @@ -7,7 +7,6 @@ import ( var BackendRequestMap = map[string]func() interface{}{ - common.ADMIN_OAUTH_V1 + "/sql/list": func() interface{} { - return new(backend.DbListRequest) - }, + common.ADMIN_OAUTH_V1 + "/sql/list": func() interface{} { return new(backend.DbListRequest) }, + common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) }, } diff --git a/app/http/routes/admin.go b/app/http/routes/admin.go index 3a8e16a..8f51a4c 100644 --- a/app/http/routes/admin.go +++ b/app/http/routes/admin.go @@ -39,6 +39,7 @@ func RegisterAdminRoute(router *gin.Engine) { sql := v1.Group("/sql") { sql.POST("/list", backend.DbList) + sql.POST("/add", backend.DbAdd) } //任务 cmd := v1.Group("/cmd") diff --git a/app/services/db_service/db_service.go b/app/services/db_service/db_service.go index e9e46d0..1b9b24e 100644 --- a/app/services/db_service/db_service.go +++ b/app/services/db_service/db_service.go @@ -1,8 +1,10 @@ package db_service import ( + "cron_admin/app/constants/common" "cron_admin/app/http/entities/backend" "cron_admin/app/models/crondbmodel" + "cron_admin/app/utils/mapstructure" "xorm.io/builder" ) @@ -26,5 +28,12 @@ func DbList(request *backend.DbListRequest, page int, limit int) (count int64, D 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 }