Files
mayfly-go/server/internal/mongo/infrastructure/persistence/mongo.go

61 lines
1.5 KiB
Go
Raw Normal View History

2022-05-17 20:23:08 +08:00
package persistence
import (
2022-09-09 18:26:08 +08:00
"mayfly-go/internal/mongo/domain/entity"
"mayfly-go/internal/mongo/domain/repository"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
2022-05-17 20:23:08 +08:00
)
2022-09-09 18:26:08 +08:00
type mongoRepoImpl struct{}
2022-05-17 20:23:08 +08:00
2022-09-09 18:26:08 +08:00
func newMongoRepo() repository.Mongo {
return new(mongoRepoImpl)
}
2022-05-17 20:23:08 +08:00
// 分页获取数据库信息列表
func (d *mongoRepoImpl) GetList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult[any] {
qd := gormx.NewQuery(new(entity.Mongo)).
Like("name", condition.Name).
In("tag_id", condition.TagIds).
RLike("tag_path", condition.TagPath).
OrderByAsc("tag_path")
return gormx.PageQuery(qd, pageParam, toEntity)
2022-05-17 20:23:08 +08:00
}
2022-10-26 20:49:29 +08:00
func (d *mongoRepoImpl) Count(condition *entity.MongoQuery) int64 {
where := make(map[string]any)
2022-10-26 20:49:29 +08:00
if len(condition.TagIds) > 0 {
where["tag_id"] = condition.TagIds
}
return gormx.CountByCond(new(entity.Mongo), where)
2022-05-17 20:23:08 +08:00
}
// 根据条件获取
2022-09-09 18:26:08 +08:00
func (d *mongoRepoImpl) Get(condition *entity.Mongo, cols ...string) error {
return gormx.GetBy(condition, cols...)
2022-05-17 20:23:08 +08:00
}
// 根据id获取
2022-09-09 18:26:08 +08:00
func (d *mongoRepoImpl) GetById(id uint64, cols ...string) *entity.Mongo {
2022-05-17 20:23:08 +08:00
db := new(entity.Mongo)
if err := gormx.GetById(db, id, cols...); err != nil {
2022-05-17 20:23:08 +08:00
return nil
}
return db
}
2022-09-09 18:26:08 +08:00
func (d *mongoRepoImpl) Insert(db *entity.Mongo) {
biz.ErrIsNil(gormx.Insert(db), "新增mongo信息失败")
2022-05-17 20:23:08 +08:00
}
2022-09-09 18:26:08 +08:00
func (d *mongoRepoImpl) Update(db *entity.Mongo) {
biz.ErrIsNil(gormx.UpdateById(db), "更新mongo信息失败")
2022-05-17 20:23:08 +08:00
}
2022-09-09 18:26:08 +08:00
func (d *mongoRepoImpl) Delete(id uint64) {
gormx.DeleteById(new(entity.Mongo), id)
2022-05-17 20:23:08 +08:00
}