|
|
@ -67,6 +67,7 @@ import (
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/monc"
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/monc"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var prefixUserCacheKey = "cache:user:"
|
|
|
|
var prefixUserCacheKey = "cache:user:"
|
|
|
@ -74,8 +75,8 @@ var prefixUserCacheKey = "cache:user:"
|
|
|
|
type userModel interface {
|
|
|
|
type userModel interface {
|
|
|
|
Insert(ctx context.Context, data *User) error
|
|
|
|
Insert(ctx context.Context, data *User) error
|
|
|
|
FindOne(ctx context.Context, id string) (*User, error)
|
|
|
|
FindOne(ctx context.Context, id string) (*User, error)
|
|
|
|
Update(ctx context.Context, data *User) error
|
|
|
|
Update(ctx context.Context, data *User) (*mongo.UpdateResult, error)
|
|
|
|
Delete(ctx context.Context, id string) error
|
|
|
|
Delete(ctx context.Context, id string) (int64, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type defaultUserModel struct {
|
|
|
|
type defaultUserModel struct {
|
|
|
@ -117,21 +118,21 @@ func (m *defaultUserModel) FindOne(ctx context.Context, id string) (*User, error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) Update(ctx context.Context, data *User) error {
|
|
|
|
func (m *defaultUserModel) Update(ctx context.Context, data *User) (*mongo.UpdateResult, error) {
|
|
|
|
data.UpdateAt = time.Now()
|
|
|
|
data.UpdateAt = time.Now()
|
|
|
|
key := prefixUserCacheKey + data.ID.Hex()
|
|
|
|
key := prefixUserCacheKey + data.ID.Hex()
|
|
|
|
_, err := m.conn.ReplaceOne(ctx, key, bson.M{"_id": data.ID}, data)
|
|
|
|
res, err := m.conn.ReplaceOne(ctx, key, bson.M{"_id": data.ID}, bson.M{"$set": data})
|
|
|
|
return err
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *defaultUserModel) Delete(ctx context.Context, id string) error {
|
|
|
|
func (m *defaultUserModel) Delete(ctx context.Context, id string) (int64, error) {
|
|
|
|
oid, err := primitive.ObjectIDFromHex(id)
|
|
|
|
oid, err := primitive.ObjectIDFromHex(id)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidObjectId
|
|
|
|
return 0, ErrInvalidObjectId
|
|
|
|
}
|
|
|
|
}
|
|
|
|
key := prefixUserCacheKey + id
|
|
|
|
key := prefixUserCacheKey + id
|
|
|
|
_, err = m.conn.DeleteOne(ctx, key, bson.M{"_id": oid})
|
|
|
|
res, err := m.conn.DeleteOne(ctx, key, bson.M{"_id": oid})
|
|
|
|
return err
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|