Skip to content

Commit

Permalink
fix: createPost添加返回参数postId
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx committed Feb 10, 2024
1 parent c50d87a commit a9239e6
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion biz/application/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var PostSet = wire.NewSet(
)

func (s *PostService) CreatePost(ctx context.Context, req *gencontent.CreatePostReq) (resp *gencontent.CreatePostResp, err error) {
if err = s.PostMongoMapper.Insert(ctx, &postmapper.Post{
if resp.PostId, err = s.PostMongoMapper.Insert(ctx, &postmapper.Post{
Title: req.Title,
Text: req.Text,
Url: req.Url,
Expand All @@ -47,6 +47,7 @@ func (s *PostService) CreatePost(ctx context.Context, req *gencontent.CreatePost
}); err != nil {
return resp, err
}

return resp, nil
}

Expand Down
6 changes: 3 additions & 3 deletions biz/infrastructure/mapper/post/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const prefixPostCacheKey = "cache:post:"

type (
IPostMongoMapper interface {
Insert(ctx context.Context, data *Post) error
Insert(ctx context.Context, data *Post) (string, error)
FindOne(ctx context.Context, id string) (*Post, error)
Update(ctx context.Context, data *Post) error
Delete(ctx context.Context, id string) error
Expand Down Expand Up @@ -118,7 +118,7 @@ func (m *MongoMapper) FindManyAndCount(ctx context.Context, fopts *FilterOptions
return posts, total, nil
}

func (m *MongoMapper) Insert(ctx context.Context, data *Post) error {
func (m *MongoMapper) Insert(ctx context.Context, data *Post) (string, error) {
if data.ID.IsZero() {
data.ID = primitive.NewObjectID()
data.CreateAt = time.Now()
Expand All @@ -127,7 +127,7 @@ func (m *MongoMapper) Insert(ctx context.Context, data *Post) error {

key := prefixPostCacheKey + data.ID.Hex()
_, err := m.conn.InsertOne(ctx, key, data)
return err
return data.ID.Hex(), err
}

func (m *MongoMapper) FindOne(ctx context.Context, id string) (*Post, error) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/CloudStriver/go-pkg v0.0.0-20231229114943-910edcb8788d
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240209105344-32f0bd207ac6
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240210022809-ad9936dc5ed8
github.com/bytedance/sonic v1.10.2
github.com/cloudwego/kitex v0.8.0
github.com/elastic/go-elasticsearch/v8 v8.11.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240209072649-698e4ff5d11f h1
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240209072649-698e4ff5d11f/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240209105344-32f0bd207ac6 h1:qlEpPhf4H7t130ZMFM4iNU+8RWSPgjBURU5OQviBCoo=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240209105344-32f0bd207ac6/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240210022809-ad9936dc5ed8 h1:aq9QAEH/pgYLBJQEss3NHBGHbpdiWpyJLu8mj2AegIU=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240210022809-ad9936dc5ed8/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
Expand Down

0 comments on commit a9239e6

Please sign in to comment.