fix: modify the generated update function and add return values for update and delete functions (#2793)

master
foliet 2 years ago committed by GitHub
parent 5f02e623f5
commit a2df1bb164
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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
} }
``` ```

@ -8,6 +8,7 @@ import (
{{if .Cache}}"github.com/zeromicro/go-zero/core/stores/monc"{{else}}"github.com/zeromicro/go-zero/core/stores/mon"{{end}} {{if .Cache}}"github.com/zeromicro/go-zero/core/stores/monc"{{else}}"github.com/zeromicro/go-zero/core/stores/mon"{{end}}
"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"
) )
{{if .Cache}}var prefix{{.Type}}CacheKey = "cache:{{.lowerType}}:"{{end}} {{if .Cache}}var prefix{{.Type}}CacheKey = "cache:{{.lowerType}}:"{{end}}
@ -15,8 +16,8 @@ import (
type {{.lowerType}}Model interface{ type {{.lowerType}}Model interface{
Insert(ctx context.Context,data *{{.Type}}) error Insert(ctx context.Context,data *{{.Type}}) error
FindOne(ctx context.Context,id string) (*{{.Type}}, error) FindOne(ctx context.Context,id string) (*{{.Type}}, error)
Update(ctx context.Context,data *{{.Type}}) error Update(ctx context.Context,data *{{.Type}}) (*mongo.UpdateResult, error)
Delete(ctx context.Context,id string) error Delete(ctx context.Context,id string) (int64, error)
} }
type default{{.Type}}Model struct { type default{{.Type}}Model struct {
@ -59,19 +60,19 @@ func (m *default{{.Type}}Model) FindOne(ctx context.Context, id string) (*{{.Typ
} }
} }
func (m *default{{.Type}}Model) Update(ctx context.Context, data *{{.Type}}) error { func (m *default{{.Type}}Model) Update(ctx context.Context, data *{{.Type}}) (*mongo.UpdateResult, error) {
data.UpdateAt = time.Now() data.UpdateAt = time.Now()
{{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex(){{end}} {{if .Cache}}key := prefix{{.Type}}CacheKey + data.ID.Hex(){{end}}
_, err := m.conn.ReplaceOne(ctx, {{if .Cache}}key, {{end}}bson.M{"_id": data.ID}, data) res, err := m.conn.UpdateOne(ctx, {{if .Cache}}key, {{end}}bson.M{"_id": data.ID}, bson.M{"$set": data})
return err return res, err
} }
func (m *default{{.Type}}Model) Delete(ctx context.Context, id string) error { func (m *default{{.Type}}Model) 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
} }
{{if .Cache}}key := prefix{{.Type}}CacheKey +id{{end}} {{if .Cache}}key := prefix{{.Type}}CacheKey +id{{end}}
_, err = m.conn.DeleteOne(ctx, {{if .Cache}}key, {{end}}bson.M{"_id": oid}) res, err := m.conn.DeleteOne(ctx, {{if .Cache}}key, {{end}}bson.M{"_id": oid})
return err return res, err
} }

Loading…
Cancel
Save