refactor: 新增base.Repo与base.App,重构repo与app层代码

This commit is contained in:
meilin.huang
2023-10-26 17:15:49 +08:00
parent 10f6b03fb5
commit a1303b52eb
115 changed files with 1867 additions and 1696 deletions

View File

@@ -37,7 +37,9 @@ func (m *Mongo) Mongos(rc *req.Ctx) {
}
queryCond.TagIds = tagIds
rc.ResData = m.MongoApp.GetPageList(queryCond, page, new([]entity.Mongo))
res, err := m.MongoApp.GetPageList(queryCond, page, new([]entity.Mongo))
biz.ErrIsNil(err)
rc.ResData = res
}
func (m *Mongo) MongoTags(rc *req.Ctx) {
@@ -56,7 +58,7 @@ func (m *Mongo) Save(rc *req.Ctx) {
rc.ReqParam = form
mongo.SetBaseInfo(rc.LoginAccount)
m.MongoApp.Save(mongo)
biz.ErrIsNil(m.MongoApp.Save(mongo))
}
func (m *Mongo) DeleteMongo(rc *req.Ctx) {

View File

@@ -8,6 +8,7 @@ import (
"mayfly-go/internal/machine/infrastructure/machine"
"mayfly-go/internal/mongo/domain/entity"
"mayfly-go/internal/mongo/domain/repository"
"mayfly-go/pkg/base"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/cache"
"mayfly-go/pkg/logx"
@@ -23,21 +24,17 @@ import (
)
type Mongo interface {
base.App[*entity.Mongo]
// 分页获取机器脚本信息列表
GetPageList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetPageList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
Count(condition *entity.MongoQuery) int64
// 根据条件获取
GetBy(condition *entity.Mongo, cols ...string) error
// 根据id获取
GetById(id uint64, cols ...string) *entity.Mongo
Save(entity *entity.Mongo)
Save(entity *entity.Mongo) error
// 删除数据库信息
Delete(id uint64)
Delete(id uint64) error
// 获取mongo连接实例
// @param id mongo id
@@ -46,53 +43,45 @@ type Mongo interface {
func newMongoAppImpl(mongoRepo repository.Mongo) Mongo {
return &mongoAppImpl{
mongoRepo: mongoRepo,
base.AppImpl[*entity.Mongo, repository.Mongo]{Repo: mongoRepo},
}
}
type mongoAppImpl struct {
mongoRepo repository.Mongo
base.AppImpl[*entity.Mongo, repository.Mongo]
}
// 分页获取数据库信息列表
func (d *mongoAppImpl) GetPageList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
return d.mongoRepo.GetList(condition, pageParam, toEntity, orderBy...)
func (d *mongoAppImpl) GetPageList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
return d.GetRepo().GetList(condition, pageParam, toEntity, orderBy...)
}
func (d *mongoAppImpl) Count(condition *entity.MongoQuery) int64 {
return d.mongoRepo.Count(condition)
return d.GetRepo().Count(condition)
}
// 根据条件获取
func (d *mongoAppImpl) GetBy(condition *entity.Mongo, cols ...string) error {
return d.mongoRepo.Get(condition, cols...)
}
// 根据id获取
func (d *mongoAppImpl) GetById(id uint64, cols ...string) *entity.Mongo {
return d.mongoRepo.GetById(id, cols...)
}
func (d *mongoAppImpl) Delete(id uint64) {
d.mongoRepo.Delete(id)
func (d *mongoAppImpl) Delete(id uint64) error {
DeleteMongoCache(id)
return d.GetRepo().DeleteById(id)
}
func (d *mongoAppImpl) Save(m *entity.Mongo) {
func (d *mongoAppImpl) Save(m *entity.Mongo) error {
if m.Id == 0 {
d.mongoRepo.Insert(m)
} else {
// 先关闭连接
DeleteMongoCache(m.Id)
d.mongoRepo.Update(m)
return d.GetRepo().Insert(m)
}
// 先关闭连接
DeleteMongoCache(m.Id)
return d.GetRepo().UpdateById(m)
}
func (d *mongoAppImpl) GetMongoInst(id uint64) *MongoInstance {
mongoInstance, err := GetMongoInstance(id, func(u uint64) *entity.Mongo {
mongo := d.GetById(u)
biz.NotNil(mongo, "mongo信息不存在")
return mongo
mongoInstance, err := GetMongoInstance(id, func(u uint64) (*entity.Mongo, error) {
mongo, err := d.GetById(new(entity.Mongo), u)
if err != nil {
return nil, err
}
return mongo, nil
})
biz.ErrIsNilAppendErr(err, "连接mongo失败: %s")
return mongoInstance
@@ -122,9 +111,14 @@ func init() {
}
// 获取mongo的连接实例
func GetMongoInstance(mongoId uint64, getMongoEntity func(uint64) *entity.Mongo) (*MongoInstance, error) {
func GetMongoInstance(mongoId uint64, getMongoEntity func(uint64) (*entity.Mongo, error)) (*MongoInstance, error) {
mi, err := mongoCliCache.ComputeIfAbsent(mongoId, func(_ any) (any, error) {
c, err := connect(getMongoEntity(mongoId))
mongoEntity, err := getMongoEntity(mongoId)
if err != nil {
return nil, err
}
c, err := connect(mongoEntity)
if err != nil {
return nil, err
}
@@ -211,7 +205,11 @@ type MongoSshDialer struct {
}
func (sd *MongoSshDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
if sshConn, err := machineapp.GetMachineApp().GetSshTunnelMachine(sd.machineId).GetDialConn(network, address); err == nil {
stm, err := machineapp.GetMachineApp().GetSshTunnelMachine(sd.machineId)
if err != nil {
return nil, err
}
if sshConn, err := stm.GetDialConn(network, address); err == nil {
// 将ssh conn包装否则内部部设置超时会报错,ssh conn不支持设置超时会返回错误: ssh: tcpChan: deadline not supported
return &netx.WrapSshConn{Conn: sshConn}, nil
} else {

View File

@@ -2,24 +2,15 @@ package repository
import (
"mayfly-go/internal/mongo/domain/entity"
"mayfly-go/pkg/base"
"mayfly-go/pkg/model"
)
type Mongo interface {
base.Repo[*entity.Mongo]
// 分页获取列表
GetList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any]
GetList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
Count(condition *entity.MongoQuery) int64
// 根据条件获取
Get(condition *entity.Mongo, cols ...string) error
// 根据id获取
GetById(id uint64, cols ...string) *entity.Mongo
Insert(db *entity.Mongo)
Update(db *entity.Mongo)
Delete(id uint64)
}

View File

@@ -3,19 +3,21 @@ package persistence
import (
"mayfly-go/internal/mongo/domain/entity"
"mayfly-go/internal/mongo/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
)
type mongoRepoImpl struct{}
type mongoRepoImpl struct {
base.RepoImpl[*entity.Mongo]
}
func newMongoRepo() repository.Mongo {
return new(mongoRepoImpl)
return &mongoRepoImpl{base.RepoImpl[*entity.Mongo]{M: new(entity.Mongo)}}
}
// 分页获取数据库信息列表
func (d *mongoRepoImpl) GetList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
func (d *mongoRepoImpl) GetList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
qd := gormx.NewQuery(new(entity.Mongo)).
Like("name", condition.Name).
In("tag_id", condition.TagIds).
@@ -31,30 +33,3 @@ func (d *mongoRepoImpl) Count(condition *entity.MongoQuery) int64 {
}
return gormx.CountByCond(new(entity.Mongo), where)
}
// 根据条件获取
func (d *mongoRepoImpl) Get(condition *entity.Mongo, cols ...string) error {
return gormx.GetBy(condition, cols...)
}
// 根据id获取
func (d *mongoRepoImpl) GetById(id uint64, cols ...string) *entity.Mongo {
db := new(entity.Mongo)
if err := gormx.GetById(db, id, cols...); err != nil {
return nil
}
return db
}
func (d *mongoRepoImpl) Insert(db *entity.Mongo) {
biz.ErrIsNil(gormx.Insert(db), "新增mongo信息失败")
}
func (d *mongoRepoImpl) Update(db *entity.Mongo) {
biz.ErrIsNil(gormx.UpdateById(db), "更新mongo信息失败")
}
func (d *mongoRepoImpl) Delete(id uint64) {
gormx.DeleteById(new(entity.Mongo), id)
}