From a9239e6a276335f693b742857a1e34184141e5f9 Mon Sep 17 00:00:00 2001 From: Lansong <2095621390@qq.com> Date: Sat, 10 Feb 2024 10:29:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20createPost=E6=B7=BB=E5=8A=A0=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=8F=82=E6=95=B0postId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/application/service/post.go | 3 ++- biz/infrastructure/mapper/post/mongo.go | 6 +++--- go.mod | 2 +- go.sum | 2 ++ 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/biz/application/service/post.go b/biz/application/service/post.go index 8d00eee..e06bce4 100644 --- a/biz/application/service/post.go +++ b/biz/application/service/post.go @@ -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, @@ -47,6 +47,7 @@ func (s *PostService) CreatePost(ctx context.Context, req *gencontent.CreatePost }); err != nil { return resp, err } + return resp, nil } diff --git a/biz/infrastructure/mapper/post/mongo.go b/biz/infrastructure/mapper/post/mongo.go index 9c24ce7..662c050 100644 --- a/biz/infrastructure/mapper/post/mongo.go +++ b/biz/infrastructure/mapper/post/mongo.go @@ -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 @@ -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() @@ -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) { diff --git a/go.mod b/go.mod index 7e2a03d..8335c16 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 7fa11a5..72c3019 100644 --- a/go.sum +++ b/go.sum @@ -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=