refactor: api层尽可能屏蔽gin框架相关代码

This commit is contained in:
meilin.huang
2024-02-24 16:30:29 +08:00
parent 7e7f02b502
commit b56b0187cf
44 changed files with 639 additions and 595 deletions

View File

@@ -7,7 +7,6 @@ import (
"mayfly-go/internal/db/application"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"strconv"
"strings"
@@ -21,13 +20,13 @@ type DbRestore struct {
// GetPageList 获取数据库恢复任务
// @router /api/dbs/:dbId/restores [GET]
func (d *DbRestore) GetPageList(rc *req.Ctx) {
dbId := uint64(ginx.PathParamInt(rc.GinCtx, "dbId"))
dbId := uint64(rc.F.PathParamInt("dbId"))
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
db, err := d.dbApp.GetById(new(entity.Db), dbId, "db_instance_id", "database")
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
var restores []vo.DbRestore
queryCond, page := ginx.BindQueryAndPage[*entity.DbRestoreQuery](rc.GinCtx, new(entity.DbRestoreQuery))
queryCond, page := req.BindQueryAndPage[*entity.DbRestoreQuery](rc, new(entity.DbRestoreQuery))
queryCond.DbInstanceId = db.InstanceId
queryCond.InDbNames = strings.Fields(db.Database)
res, err := d.restoreApp.GetPageList(queryCond, page, &restores)
@@ -39,10 +38,10 @@ func (d *DbRestore) GetPageList(rc *req.Ctx) {
// @router /api/dbs/:dbId/restores [POST]
func (d *DbRestore) Create(rc *req.Ctx) {
restoreForm := &form.DbRestoreForm{}
ginx.BindJsonAndValid(rc.GinCtx, restoreForm)
req.BindJsonAndValid(rc, restoreForm)
rc.ReqParam = restoreForm
dbId := uint64(ginx.PathParamInt(rc.GinCtx, "dbId"))
dbId := uint64(rc.F.PathParamInt("dbId"))
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
db, err := d.dbApp.GetById(new(entity.Db), dbId, "instanceId")
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
@@ -70,7 +69,7 @@ func (d *DbRestore) createWithBackupHistory(backupHistoryIds string) {
// @router /api/dbs/:dbId/restores/:restoreId [PUT]
func (d *DbRestore) Update(rc *req.Ctx) {
restoreForm := &form.DbRestoreForm{}
ginx.BindJsonAndValid(rc.GinCtx, restoreForm)
req.BindJsonAndValid(rc, restoreForm)
rc.ReqParam = restoreForm
job := &entity.DbRestore{}
@@ -81,7 +80,7 @@ func (d *DbRestore) Update(rc *req.Ctx) {
}
func (d *DbRestore) walk(rc *req.Ctx, fn func(ctx context.Context, restoreId uint64) error) error {
idsStr := ginx.PathParam(rc.GinCtx, "restoreId")
idsStr := rc.F.PathParam("restoreId")
biz.NotEmpty(idsStr, "restoreId 为空")
rc.ReqParam = idsStr
ids := strings.Fields(idsStr)
@@ -123,7 +122,7 @@ func (d *DbRestore) Disable(rc *req.Ctx) {
// GetDbNamesWithoutRestore 获取未配置定时恢复的数据库名称
// @router /api/dbs/:dbId/db-names-without-backup [GET]
func (d *DbRestore) GetDbNamesWithoutRestore(rc *req.Ctx) {
dbId := uint64(ginx.PathParamInt(rc.GinCtx, "dbId"))
dbId := uint64(rc.F.PathParamInt("dbId"))
db, err := d.dbApp.GetById(new(entity.Db), dbId, "instance_id", "database")
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
dbNames := strings.Fields(db.Database)
@@ -136,9 +135,9 @@ func (d *DbRestore) GetDbNamesWithoutRestore(rc *req.Ctx) {
// @router /api/dbs/:dbId/restores/:restoreId/histories [GET]
func (d *DbRestore) GetHistoryPageList(rc *req.Ctx) {
queryCond := &entity.DbRestoreHistoryQuery{
DbRestoreId: uint64(ginx.PathParamInt(rc.GinCtx, "restoreId")),
DbRestoreId: uint64(rc.F.PathParamInt("restoreId")),
}
res, err := d.restoreApp.GetHistoryPageList(queryCond, ginx.GetPageParam(rc.GinCtx), new([]vo.DbRestoreHistory))
res, err := d.restoreApp.GetHistoryPageList(queryCond, rc.F.GetPageParam(), new([]vo.DbRestoreHistory))
biz.ErrIsNilAppendErr(err, "获取数据库备份历史失败: %v")
rc.ResData = res
}