From 22dc1bbcc6a857da951925d470fc9a6c4e31cf63 Mon Sep 17 00:00:00 2001 From: wuchao <1272174216@qq.com> Date: Wed, 27 Nov 2024 17:43:49 +0800 Subject: [PATCH] =?UTF-8?q?db=E6=95=B0=E6=8D=AE=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/http/controllers/backend/db_controller.go | 12 ++++++++++ app/http/entities/backend/db.go | 24 +++++++++++++++---- app/http/requestmapping/backend.go | 2 ++ app/http/routes/admin.go | 2 ++ app/models/crondbmodel/cron_db.go | 19 ++++++++------- app/services/db_service/db_service.go | 17 ++++++++++++- 6 files changed, 61 insertions(+), 15 deletions(-) diff --git a/app/http/controllers/backend/db_controller.go b/app/http/controllers/backend/db_controller.go index ee787f3..98a9b72 100644 --- a/app/http/controllers/backend/db_controller.go +++ b/app/http/controllers/backend/db_controller.go @@ -27,3 +27,15 @@ func DbAdd(c *gin.Context) { err := db_service.DbAdd(request) controllers.HandRes(c, nil, err) } + +func DbEdit(c *gin.Context) { + request := controllers.GetRequest(c).(*backend.DbEditRequest) + err := db_service.DbEdit(request) + controllers.HandRes(c, nil, err) +} + +func DbDel(c *gin.Context) { + request := controllers.GetRequest(c).(*backend.DbDeleteRequest) + err := db_service.DbDel(request) + controllers.HandRes(c, nil, err) +} diff --git a/app/http/entities/backend/db.go b/app/http/entities/backend/db.go index b20b0da..50e8f74 100644 --- a/app/http/entities/backend/db.go +++ b/app/http/entities/backend/db.go @@ -4,18 +4,18 @@ import "cron_admin/app/http/entities" type DbListRequest struct { entities.PageRequest - DbName string `json:"db_name" form:"db_name" example:""` - Status int `json:"status" form:"status" example:"1"` - DbType string `json:"db_type" form:"db_type" example:"mysql"` + DbName string `json:"db_name" form:"db_name" example:""` + Status int `json:"status" form:"status" example:"1"` + DbType string `json:"db_type" form:"db_type" example:"mysql"` + DbPermission int `json:"db_permission" form:"db_permission" example:"1"` } type DbListResponse struct { - DbId string `json:"db_id"` + DbId int `json:"db_id"` DbName string `json:"db_name"` DbType string `json:"db_type"` Status int `json:"status"` DbPermission int `json:"db_permission"` - Source string `json:"source"` Desc string `json:"desc"` CreateTime string `json:"create_time"` } @@ -28,3 +28,17 @@ type DbAddRequest struct { Desc string `json:"desc" form:"desc" example:""` Status int `json:"status" form:"status" example:"1"` } + +type DbEditRequest struct { + DbId int `json:"db_id" validate:"required" form:"db_id" example:""` + 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"` +} + +type DbDeleteRequest struct { + DbId int `json:"db_id" validate:"required" form:"db_id" example:""` +} diff --git a/app/http/requestmapping/backend.go b/app/http/requestmapping/backend.go index 86de45f..06aa3a5 100644 --- a/app/http/requestmapping/backend.go +++ b/app/http/requestmapping/backend.go @@ -9,4 +9,6 @@ var BackendRequestMap = map[string]func() interface{}{ common.ADMIN_OAUTH_V1 + "/sql/list": func() interface{} { return new(backend.DbListRequest) }, common.ADMIN_OAUTH_V1 + "/sql/add": func() interface{} { return new(backend.DbAddRequest) }, + common.ADMIN_OAUTH_V1 + "/sql/edit": func() interface{} { return new(backend.DbEditRequest) }, + common.ADMIN_OAUTH_V1 + "/sql/del": func() interface{} { return new(backend.DbDeleteRequest) }, } diff --git a/app/http/routes/admin.go b/app/http/routes/admin.go index f2f8481..a0f3c53 100644 --- a/app/http/routes/admin.go +++ b/app/http/routes/admin.go @@ -40,6 +40,8 @@ func RegisterAdminRoute(router *gin.Engine) { { sql.POST("/list", backend.DbList) sql.POST("/add", backend.DbAdd) + sql.POST("/edit", backend.DbEdit) + sql.DELETE("/del", backend.DbDel) } //任务 cmd := v1.Group("/cmd") diff --git a/app/models/crondbmodel/cron_db.go b/app/models/crondbmodel/cron_db.go index c704cc6..de4911d 100644 --- a/app/models/crondbmodel/cron_db.go +++ b/app/models/crondbmodel/cron_db.go @@ -13,15 +13,16 @@ var ( // 实体 type CronDb struct { - DbId string `xorm:"'db_id' UNSIGNED INT"` - DbName string `xorm:"'db_name' varchar(20)"` - DbType string `xorm:"'db_type' varchar(10)"` - DbPermission int `xorm:"'db_permission' TINYINT"` - Source string `xorm:"'source' varchar(150)"` - Desc string `xorm:"'desc' varchar(200)"` - CreateTime time.Time `xorm:"'create_time' datetime"` - UpdateTime time.Time `xorm:"'update_time' timestamp"` - Status int `xorm:"'status' TINYINT"` + DbId int `xorm:"'db_id' UNSIGNED INT pk autoincr"` + DbName string `xorm:"'db_name' varchar(20)"` + DbType string `xorm:"'db_type' varchar(10)"` + DbPermission int `xorm:"'db_permission' TINYINT"` + Source string `xorm:"'source' varchar(150)"` + Desc string `xorm:"'desc' varchar(200)"` + CreateTime *time.Time `xorm:"'create_time' created datetime"` + UpdateTime time.Time `xorm:"'update_time' updated timestamp"` + DeletedTime time.Time `xorm:"'deleted_time' deleted datetime"` + Status int `xorm:"'status' TINYINT"` } // 表名 diff --git a/app/services/db_service/db_service.go b/app/services/db_service/db_service.go index 1b9b24e..f820e51 100644 --- a/app/services/db_service/db_service.go +++ b/app/services/db_service/db_service.go @@ -23,7 +23,10 @@ func DbList(request *backend.DbListRequest, page int, limit int) (count int64, D if page != 0 && limit != 0 { session = session.Limit(limit, (page-1)*limit) } - count, err = session.FindAndCount(&DbListInfo) + if request.DbPermission != 0 { + session = session.And(builder.Eq{"DbPermission": request.DbPermission}) + } + count, err = session.Omit("source", "UpdateTime").FindAndCount(&DbListInfo) if err != nil { return } @@ -37,3 +40,15 @@ func DbAdd(request *backend.DbAddRequest) (err error) { _, err = crondbmodel.GetInstance().GetDb().InsertOne(db) return } + +func DbEdit(request *backend.DbEditRequest) (err error) { + var db crondbmodel.CronDb + _ = mapstructure.Decode(request, &db) + _, err = crondbmodel.GetInstance().GetDb().ID(request.DbId).Update(&db) + return +} + +func DbDel(request *backend.DbDeleteRequest) (err error) { + _, err = crondbmodel.GetInstance().GetDb().ID(request.DbId).Delete(&crondbmodel.CronDb{}) + return +}