Skip to content

Commit

Permalink
fix: 修改post参数 (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lansongxx authored Feb 6, 2024
1 parent c5f2845 commit bb80c73
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
40 changes: 31 additions & 9 deletions biz/application/service/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/wire"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/stores/redis"
"go.mongodb.org/mongo-driver/bson/primitive"
)

type IPostService interface {
Expand All @@ -35,21 +36,34 @@ var PostSet = wire.NewSet(
)

func (s *PostService) CreatePost(ctx context.Context, req *gencontent.CreatePostReq) (resp *gencontent.CreatePostResp, err error) {
resp = new(gencontent.CreatePostResp)
if err = s.PostMongoMapper.Insert(ctx, convertor.PostToPostMapper(req.Post)); err != nil {
if err = s.PostMongoMapper.Insert(ctx, &postmapper.Post{
Title: req.Title,
Text: req.Text,
Url: req.Url,
Tags: req.Tags,
UserId: req.UserId,
Status: req.Status,
Score_: 0,
}); err != nil {
return resp, err
}
return resp, nil
}

func (s *PostService) GetPost(ctx context.Context, req *gencontent.GetPostReq) (resp *gencontent.GetPostResp, err error) {
resp = new(gencontent.GetPostResp)
post, err := s.PostMongoMapper.FindOne(ctx, convertor.PostFilterOptionsToFilterOptions(req.PostFilterOptions))
post, err := s.PostMongoMapper.FindOne(ctx, req.PostId)
if err != nil {
return resp, err
}
resp.Post = convertor.PostMapperToPost(post)
return resp, nil
return &gencontent.GetPostResp{
UserId: post.UserId,
Title: post.Title,
Text: post.Text,
Tags: post.Tags,
Url: post.Url,
CreateTime: post.CreateAt.UnixMilli(),
UpdateTime: post.UpdateAt.UnixMilli(),
}, nil
}

func (s *PostService) GetPosts(ctx context.Context, req *gencontent.GetPostsReq) (resp *gencontent.GetPostsResp, err error) {
Expand All @@ -58,6 +72,7 @@ func (s *PostService) GetPosts(ctx context.Context, req *gencontent.GetPostsReq)
total int64
posts []*postmapper.Post
)

p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions)
filter := convertor.PostFilterOptionsToFilterOptions(req.PostFilterOptions)
if req.SearchOptions != nil {
Expand Down Expand Up @@ -88,15 +103,22 @@ func (s *PostService) GetPosts(ctx context.Context, req *gencontent.GetPostsReq)
}

func (s *PostService) UpdatePost(ctx context.Context, req *gencontent.UpdatePostReq) (resp *gencontent.UpdatePostResp, err error) {
resp = new(gencontent.UpdatePostResp)
if err = s.PostMongoMapper.Update(ctx, convertor.PostToPostMapper(req.Post)); err != nil {
oid, _ := primitive.ObjectIDFromHex(req.PostId)

if err = s.PostMongoMapper.Update(ctx, &postmapper.Post{
ID: oid,
Title: req.Title,
Text: req.Text,
Url: req.Url,
Tags: req.Tags,
Status: req.Status,
}); err != nil {
return resp, err
}
return resp, nil
}

func (s *PostService) DeletePost(ctx context.Context, req *gencontent.DeletePostReq) (resp *gencontent.DeletePostResp, err error) {
resp = new(gencontent.DeletePostResp)
if err = s.PostMongoMapper.Delete(ctx, req.PostId); err != nil {
return resp, err
}
Expand Down
15 changes: 7 additions & 8 deletions biz/infrastructure/mapper/post/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const prefixPostCacheKey = "cache:post:"
type (
IPostMongoMapper interface {
Insert(ctx context.Context, data *Post) error
FindOne(ctx context.Context, fopts *FilterOptions) (*Post, error)
FindOne(ctx context.Context, id string) (*Post, error)
Update(ctx context.Context, data *Post) error
Delete(ctx context.Context, id string) error
FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*Post, error)
Expand Down Expand Up @@ -130,14 +130,13 @@ func (m *MongoMapper) Insert(ctx context.Context, data *Post) error {
return err
}

func (m *MongoMapper) FindOne(ctx context.Context, fopts *FilterOptions) (*Post, error) {
filter := MakeBsonFilter(fopts)
func (m *MongoMapper) FindOne(ctx context.Context, id string) (*Post, error) {
var data Post
if fopts.OnlyPostId == nil {
return nil, consts.ErrInvalidId
}
key := prefixPostCacheKey + *fopts.OnlyPostId
err := m.conn.FindOne(ctx, key, &data, filter)
key := prefixPostCacheKey + id
oid, _ := primitive.ObjectIDFromHex(id)
err := m.conn.FindOne(ctx, key, &data, bson.M{
consts.ID: oid,
})
switch {
case err == nil:
return &data, nil
Expand Down
6 changes: 5 additions & 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-20240206035429-8096c700ad87
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206085239-35c6846693f2
github.com/bytedance/sonic v1.10.2
github.com/cloudwego/kitex v0.8.0
github.com/elastic/go-elasticsearch/v8 v8.11.1
Expand All @@ -19,6 +19,10 @@ require (
google.golang.org/grpc v1.60.1
)

//replace (
// github.com/CloudStriver/service-idl-gen-go => ../service-idl-gen-go
//)

require (
github.com/apache/thrift v0.16.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240202114307-d5edc26b91e1 h1
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240202114307-d5edc26b91e1/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206035429-8096c700ad87 h1:OGZxm1tDqseEZkbS+9gDu9R+T5KSuezqNV67ONxsgwg=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206035429-8096c700ad87/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206085239-35c6846693f2 h1:uL459qhf+/YtP3E400LNuyfqY6TTJcbA1R51sBatD+E=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206085239-35c6846693f2/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 bb80c73

Please sign in to comment.