Files
mayfly-go/server/internal/mongo/application/mongo.go

74 lines
1.9 KiB
Go
Raw Normal View History

2022-05-17 20:23:08 +08:00
package application
import (
"context"
2022-09-09 18:26:08 +08:00
"mayfly-go/internal/mongo/domain/entity"
"mayfly-go/internal/mongo/domain/repository"
"mayfly-go/internal/mongo/mgm"
"mayfly-go/pkg/base"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/model"
2022-05-17 20:23:08 +08:00
)
type Mongo interface {
base.App[*entity.Mongo]
2022-05-17 20:23:08 +08:00
// 分页获取机器脚本信息列表
GetPageList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
2022-05-17 20:23:08 +08:00
2022-10-26 20:49:29 +08:00
Count(condition *entity.MongoQuery) int64
2022-05-17 20:23:08 +08:00
Save(ctx context.Context, entity *entity.Mongo) error
2022-05-17 20:23:08 +08:00
// 删除数据库信息
Delete(ctx context.Context, id uint64) error
2022-05-17 20:23:08 +08:00
2023-08-25 19:41:52 +08:00
// 获取mongo连接实例
2022-05-17 20:23:08 +08:00
// @param id mongo id
GetMongoConn(id uint64) (*mgm.MongoConn, error)
2022-05-17 20:23:08 +08:00
}
2022-09-09 18:26:08 +08:00
func newMongoAppImpl(mongoRepo repository.Mongo) Mongo {
return &mongoAppImpl{
base.AppImpl[*entity.Mongo, repository.Mongo]{Repo: mongoRepo},
2022-09-09 18:26:08 +08:00
}
2022-05-17 20:23:08 +08:00
}
2022-09-09 18:26:08 +08:00
type mongoAppImpl struct {
base.AppImpl[*entity.Mongo, repository.Mongo]
2022-05-17 20:23:08 +08:00
}
// 分页获取数据库信息列表
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...)
2022-05-17 20:23:08 +08:00
}
2022-10-26 20:49:29 +08:00
func (d *mongoAppImpl) Count(condition *entity.MongoQuery) int64 {
return d.GetRepo().Count(condition)
2022-05-17 20:23:08 +08:00
}
func (d *mongoAppImpl) Delete(ctx context.Context, id uint64) error {
mgm.CloseConn(id)
return d.GetRepo().DeleteById(ctx, id)
2022-05-17 20:23:08 +08:00
}
func (d *mongoAppImpl) Save(ctx context.Context, m *entity.Mongo) error {
2022-05-17 20:23:08 +08:00
if m.Id == 0 {
return d.GetRepo().Insert(ctx, m)
2022-05-17 20:23:08 +08:00
}
// 先关闭连接
mgm.CloseConn(m.Id)
return d.GetRepo().UpdateById(ctx, m)
2022-05-17 20:23:08 +08:00
}
func (d *mongoAppImpl) GetMongoConn(id uint64) (*mgm.MongoConn, error) {
return mgm.GetMongoConn(id, func() (*mgm.MongoInfo, error) {
mongo, err := d.GetById(new(entity.Mongo), id)
2022-05-17 20:23:08 +08:00
if err != nil {
return nil, errorx.NewBiz("mongo信息不存在")
2022-05-17 20:23:08 +08:00
}
return mongo.ToMongoInfo(), nil
2022-05-17 20:23:08 +08:00
})
}