refactor: 后端路由定义方式&请求参数绑定重构

This commit is contained in:
meilin.huang
2023-07-08 20:05:55 +08:00
parent 183a6e4905
commit 3269dfa5d6
59 changed files with 559 additions and 1020 deletions

View File

@@ -35,8 +35,7 @@ const DEFAULT_ROW_SIZE = 5000
// @router /api/dbs [get]
func (d *Db) Dbs(rc *req.Ctx) {
condition := new(entity.DbQuery)
condition.TagPathLike = rc.GinCtx.Query("tagPath")
queryCond, page := ginx.BindQueryAndPage[*entity.DbQuery](rc.GinCtx, new(entity.DbQuery))
// 不存在可访问标签id即没有可操作数据
tagIds := d.TagApp.ListTagIdByAccountId(rc.LoginAccount.Id)
@@ -44,16 +43,14 @@ func (d *Db) Dbs(rc *req.Ctx) {
rc.ResData = model.EmptyPageResult[any]()
return
}
condition.TagIds = tagIds
rc.ResData = d.DbApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.SelectDataDbVO))
queryCond.TagIds = tagIds
rc.ResData = d.DbApp.GetPageList(queryCond, page, new([]vo.SelectDataDbVO))
}
func (d *Db) Save(rc *req.Ctx) {
form := &form.DbForm{}
ginx.BindJsonAndValid(rc.GinCtx, form)
db := new(entity.Db)
utils.Copy(db, form)
db := ginx.BindJsonAndCopyTo[*entity.Db](rc.GinCtx, form, new(entity.Db))
// 密码解密,并使用解密后的赋值
originPwd, err := utils.DefaultRsaDecrypt(form.Password, true)
@@ -319,7 +316,6 @@ func (d *Db) DumpSql(rc *req.Ctx) {
writer.WriteString("COMMIT;\n")
}
rc.NoRes = true
rc.ReqParam = fmt.Sprintf("%s, tables: %s, dumpType: %s", dbInstance.Info.GetLogDesc(), tablesStr, dumpType)
}

View File

@@ -12,12 +12,7 @@ type DbSqlExec struct {
}
func (d *DbSqlExec) DbSqlExecs(rc *req.Ctx) {
g := rc.GinCtx
m := &entity.DbSqlExec{DbId: uint64(ginx.QueryInt(g, "dbId", 0)),
Db: g.Query("db"),
Table: g.Query("table"),
Type: int8(ginx.QueryInt(g, "type", 0)),
}
m.CreatorId = rc.LoginAccount.Id
rc.ResData = d.DbSqlExecApp.GetPageList(m, ginx.GetPageParam(rc.GinCtx), new([]entity.DbSqlExec))
queryCond, page := ginx.BindQueryAndPage(rc.GinCtx, new(entity.DbSqlExecQuery))
queryCond.CreatorId = rc.LoginAccount.Id
rc.ResData = d.DbSqlExecApp.GetPageList(queryCond, page, new([]entity.DbSqlExec))
}

View File

@@ -52,7 +52,7 @@ type DbSqlExec interface {
DeleteBy(condition *entity.DbSqlExec)
// 分页获取
GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
}
func newDbSqlExecApp(dbExecSqlRepo repository.DbSqlExec) DbSqlExec {
@@ -152,7 +152,7 @@ func (d *dbSqlExecAppImpl) DeleteBy(condition *entity.DbSqlExec) {
d.dbSqlExecRepo.DeleteBy(condition)
}
func (d *dbSqlExecAppImpl) GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func (d *dbSqlExecAppImpl) GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
return d.dbSqlExecRepo.GetPageList(condition, pageParam, toEntity, orderBy...)
}

View File

@@ -16,8 +16,17 @@ type DbQuery struct {
Database string `orm:"column(database)" json:"database"`
Params string `json:"params"`
Remark string `json:"remark"`
TagId uint64
TagIds []uint64
TagPathLike string
TagIds []uint64
TagPath string `form:"tagPath"`
}
type DbSqlExecQuery struct {
Id uint64 `json:"id" form:"id"`
DbId uint64 `json:"dbId" form:"dbId"`
Db string `json:"db" form:"db"`
Table string `json:"table" form:"table"`
Type int8 `json:"type" form:"type"` // 类型
CreatorId uint64
}

View File

@@ -11,5 +11,5 @@ type DbSqlExec interface {
DeleteBy(condition *entity.DbSqlExec)
// 分页获取
GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
}

View File

@@ -20,7 +20,7 @@ func (d *dbRepoImpl) GetDbList(condition *entity.DbQuery, pageParam *model.PageP
Like("host", condition.Host).
Like("database", condition.Database).
In("tag_id", condition.TagIds).
RLike("tag_path", condition.TagPathLike).
RLike("tag_path", condition.TagPath).
OrderByAsc("tag_path")
return gormx.PageQuery(qd, pageParam, toEntity)
}
@@ -30,9 +30,6 @@ func (d *dbRepoImpl) Count(condition *entity.DbQuery) int64 {
if len(condition.TagIds) > 0 {
where["tag_id"] = condition.TagIds
}
if condition.TagId != 0 {
where["tag_id"] = condition.TagId
}
return gormx.CountByCond(new(entity.Db), where)
}

View File

@@ -23,7 +23,7 @@ func (d *dbSqlExecRepoImpl) DeleteBy(condition *entity.DbSqlExec) {
}
// 分页获取
func (d *dbSqlExecRepoImpl) GetPageList(condition *entity.DbSqlExec, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
qd := gormx.NewQuery(condition).WithCondModel(condition).WithOrderBy(orderBy...)
func (d *dbSqlExecRepoImpl) GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
qd := gormx.NewQuery(new(entity.DbSqlExec)).WithCondModel(condition).WithOrderBy(orderBy...)
return gormx.PageQuery(qd, pageParam, toEntity)
}

View File

@@ -12,103 +12,55 @@ import (
func InitDbRouter(router *gin.RouterGroup) {
db := router.Group("dbs")
{
d := &api.Db{
DbApp: application.GetDbApp(),
DbSqlExecApp: application.GetDbSqlExecApp(),
MsgApp: msgapp.GetMsgApp(),
TagApp: tagapp.GetTagTreeApp(),
}
// 获取所有数据库列表
db.GET("", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.Dbs)
})
saveDb := req.NewLogInfo("db-保存数据库信息").WithSave(true)
db.POST("", func(c *gin.Context) {
req.NewCtxWithGin(c).
WithLog(saveDb).
Handle(d.Save)
})
d := &api.Db{
DbApp: application.GetDbApp(),
DbSqlExecApp: application.GetDbSqlExecApp(),
MsgApp: msgapp.GetMsgApp(),
TagApp: tagapp.GetTagTreeApp(),
}
reqs := [...]*req.Conf{
// 获取数据库列表
req.NewGet("", d.Dbs),
req.NewPost("", d.Save).Log(req.NewLogSave("db-保存数据库信息")),
// 获取数据库实例的所有数据库名
db.POST("databases", func(c *gin.Context) {
req.NewCtxWithGin(c).
Handle(d.GetDatabaseNames)
})
req.NewGet("/databases", d.GetDatabaseNames),
db.GET(":dbId/pwd", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.GetDbPwd)
})
req.NewGet(":dbId/pwd", d.GetDbPwd),
deleteDb := req.NewLogInfo("db-删除数据库信息").WithSave(true)
db.DELETE(":dbId", func(c *gin.Context) {
req.NewCtxWithGin(c).
WithLog(deleteDb).
Handle(d.DeleteDb)
})
req.NewDelete(":dbId", d.DeleteDb).Log(req.NewLogSave("db-删除数据库信息")),
db.GET(":dbId/t-infos", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.TableInfos)
})
req.NewGet(":dbId/t-infos", d.TableInfos),
db.GET(":dbId/t-index", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.TableIndex)
})
req.NewGet(":dbId/t-index", d.TableIndex),
db.GET(":dbId/t-create-ddl", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.GetCreateTableDdl)
})
req.NewGet(":dbId/t-create-ddl", d.GetCreateTableDdl),
execSqlLog := req.NewLogInfo("db-执行Sql")
db.POST(":dbId/exec-sql", func(g *gin.Context) {
rc := req.NewCtxWithGin(g).WithLog(execSqlLog)
rc.Handle(d.ExecSql)
})
req.NewPost(":dbId/exec-sql", d.ExecSql).Log(req.NewLog("db-执行Sql")),
execSqlFileLog := req.NewLogInfo("db-执行Sql文件").WithSave(true)
db.POST(":dbId/exec-sql-file", func(g *gin.Context) {
req.NewCtxWithGin(g).
WithLog(execSqlFileLog).
Handle(d.ExecSqlFile)
})
req.NewPost(":dbId/exec-sql-file", d.ExecSqlFile).Log(req.NewLogSave("db-执行Sql文件")),
dumpLog := req.NewLogInfo("db-导出sql文件").WithSave(true)
db.GET(":dbId/dump", func(g *gin.Context) {
req.NewCtxWithGin(g).
WithLog(dumpLog).
Handle(d.DumpSql)
})
req.NewGet(":dbId/dump", d.DumpSql).Log(req.NewLogSave("db-导出sql文件")).NoRes(),
db.GET(":dbId/t-metadata", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.TableMA)
})
req.NewGet(":dbId/t-metadata", d.TableMA),
db.GET(":dbId/c-metadata", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.ColumnMA)
})
req.NewGet(":dbId/c-metadata", d.ColumnMA),
db.GET(":dbId/hint-tables", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.HintTables)
})
req.NewGet(":dbId/hint-tables", d.HintTables),
/** db sql相关接口 */
// 用户sql相关
req.NewPost(":dbId/sql", d.SaveSql),
db.POST(":dbId/sql", func(c *gin.Context) {
rc := req.NewCtxWithGin(c)
rc.Handle(d.SaveSql)
})
req.NewGet(":dbId/sql", d.GetSql),
db.GET(":dbId/sql", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.GetSql)
})
req.NewDelete(":dbId/sql", d.DeleteSql),
db.DELETE(":dbId/sql", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.DeleteSql)
})
db.GET(":dbId/sql-names", func(c *gin.Context) {
req.NewCtxWithGin(c).Handle(d.GetSqlNames)
})
req.NewGet(":dbId/sql-names", d.GetSqlNames),
}
req.BatchSetGroup(db, reqs[:])
}

View File

@@ -10,14 +10,12 @@ import (
func InitDbSqlExecRouter(router *gin.RouterGroup) {
db := router.Group("/dbs/:dbId/sql-execs")
{
d := &api.DbSqlExec{
DbSqlExecApp: application.GetDbSqlExecApp(),
}
// 获取所有数据库sql执行记录列表
db.GET("", func(c *gin.Context) {
rc := req.NewCtxWithGin(c)
rc.Handle(d.DbSqlExecs)
})
d := &api.DbSqlExec{
DbSqlExecApp: application.GetDbSqlExecApp(),
}
// 获取所有数据库sql执行记录列表
req.NewGet("", d.DbSqlExecs).Group(db)
}