mongo.go重构
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
"cid/model/entity"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
@@ -11,20 +12,13 @@ import (
|
||||
|
||||
// applicationDao 应用DAO
|
||||
type applicationDao struct {
|
||||
NoCache bool
|
||||
}
|
||||
|
||||
var Application = &applicationDao{
|
||||
NoCache: true,
|
||||
}
|
||||
|
||||
func (d *applicationDao) SetNoCache() {
|
||||
Application.NoCache = true
|
||||
}
|
||||
var Application = &applicationDao{}
|
||||
|
||||
// Create 创建应用
|
||||
func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (string, error) {
|
||||
ids, err := mongo.Insert(ctx, []interface{}{app}, "application")
|
||||
ids, err := mongo.DB().Insert(ctx, []interface{}{app}, "application")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -37,14 +31,14 @@ func (d *applicationDao) Create(ctx context.Context, app *entity.Application) (s
|
||||
// GetByID 根据ID获取应用
|
||||
func (d *applicationDao) GetByID(ctx context.Context, id string) (*entity.Application, error) {
|
||||
var app *entity.Application
|
||||
err := mongo.FindOne(ctx, d.NoCache, bson.M{"_id": id}, &app, "application")
|
||||
err := mongo.DB().FindOne(ctx, bson.M{"_id": id}, &app, "application")
|
||||
return app, err
|
||||
}
|
||||
|
||||
// GetByTenantID 根据租户ID获取应用列表
|
||||
func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]*entity.Application, error) {
|
||||
var apps []*entity.Application
|
||||
err := mongo.Find(ctx, d.NoCache,
|
||||
err := mongo.DB().Find(ctx,
|
||||
bson.M{"tenantId": tenantID}, &apps, "application")
|
||||
return apps, err
|
||||
}
|
||||
@@ -52,20 +46,20 @@ func (d *applicationDao) GetByTenantID(ctx context.Context, tenantID string) ([]
|
||||
// GetByAPIKey 根据API密钥获取应用
|
||||
func (d *applicationDao) GetByAPIKey(ctx context.Context, apiKey string) (*entity.Application, error) {
|
||||
var app *entity.Application
|
||||
err := mongo.FindOne(ctx, d.NoCache,
|
||||
err := mongo.DB().FindOne(ctx,
|
||||
bson.M{"appKey": apiKey}, &app, "application")
|
||||
return app, err
|
||||
}
|
||||
|
||||
// Update 更新应用
|
||||
func (d *applicationDao) Update(ctx context.Context, app *entity.Application) error {
|
||||
_, err := mongo.Update(ctx, bson.M{"_id": app.Id}, bson.M{"$set": app}, "application")
|
||||
_, err := mongo.DB().Update(ctx, bson.M{"_id": app.Id}, bson.M{"$set": app}, "application")
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete 删除应用
|
||||
func (d *applicationDao) Delete(ctx context.Context, id string) error {
|
||||
_, err := mongo.Delete(ctx, bson.M{"_id": id}, "application")
|
||||
_, err := mongo.DB().Delete(ctx, bson.M{"_id": id}, "application")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -77,13 +71,13 @@ func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSi
|
||||
}
|
||||
|
||||
var apps []*entity.Application
|
||||
total, err := mongo.Count(ctx, d.NoCache, filter, "application")
|
||||
total, err := mongo.DB().Count(ctx, filter, "application")
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
err = mongo.Find(ctx, d.NoCache, filter, &apps, "application",
|
||||
err = mongo.DB().Find(ctx, filter, &apps, "application",
|
||||
options.Find().SetSort(bson.M{"createdAt": -1}).
|
||||
SetSkip(int64(offset)).
|
||||
SetLimit(int64(pageSize)))
|
||||
@@ -97,12 +91,12 @@ func (d *applicationDao) List(ctx context.Context, tenantID string, page, pageSi
|
||||
// GetByName 根据名称获取应用
|
||||
func (d *applicationDao) GetByName(ctx context.Context, name string) (*entity.Application, error) {
|
||||
var app *entity.Application
|
||||
err := mongo.FindOne(ctx, d.NoCache, bson.M{"name": name}, &app, "application")
|
||||
err := mongo.DB().FindOne(ctx, bson.M{"name": name}, &app, "application")
|
||||
return app, err
|
||||
}
|
||||
|
||||
// UpdateFields 更新应用部分字段
|
||||
func (d *applicationDao) UpdateFields(ctx context.Context, id string, data *entity.Application) error {
|
||||
_, err := mongo.Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "application")
|
||||
_, err := mongo.DB().Update(ctx, bson.M{"_id": id}, bson.M{"$set": data}, "application")
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user