From 94347575244d6d67daa64d3793aa3aeb54753f5d Mon Sep 17 00:00:00 2001 From: sulinehk Date: Fri, 8 Mar 2024 20:09:06 +0800 Subject: [PATCH] feat(post): add Update --- .../post/rpc/internal/logic/update_logic.go | 24 ++- .../rpc/internal/test/update_logic_test.go | 140 ++++++++++++++++++ 2 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 service/post/rpc/internal/test/update_logic_test.go diff --git a/service/post/rpc/internal/logic/update_logic.go b/service/post/rpc/internal/logic/update_logic.go index ae4d3ae..d69e2ab 100644 --- a/service/post/rpc/internal/logic/update_logic.go +++ b/service/post/rpc/internal/logic/update_logic.go @@ -2,8 +2,11 @@ package logic import ( "context" + "database/sql" + "github.com/linehk/go-microservices-blogger/errcode" "github.com/linehk/go-microservices-blogger/service/post/rpc/internal/svc" + "github.com/linehk/go-microservices-blogger/service/post/rpc/model" "github.com/linehk/go-microservices-blogger/service/post/rpc/post" "github.com/zeromicro/go-zero/core/logx" @@ -24,7 +27,24 @@ func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogi } func (l *UpdateLogic) Update(in *post.UpdateReq) (*post.Post, error) { - // todo: add your logic here and delete this line + postReq := in.GetPost() + postModel := &model.Post{ + Uuid: in.GetPostId(), + BlogUuid: sql.NullString{String: in.GetBlogId(), Valid: true}, + Published: sql.NullTime{Time: postReq.GetPublished().AsTime(), Valid: true}, + Updated: sql.NullTime{Time: postReq.GetUpdated().AsTime(), Valid: true}, + Url: postReq.GetUrl(), + SelfLink: sql.NullString{String: postReq.GetSelfLink(), Valid: true}, + Title: sql.NullString{String: postReq.GetTitle(), Valid: true}, + TitleLink: sql.NullString{String: postReq.GetTitleLink(), Valid: true}, + Content: sql.NullString{String: postReq.GetContent(), Valid: true}, + CustomMetaData: sql.NullString{String: postReq.GetCustomMetaData(), Valid: true}, + Status: sql.NullString{String: postReq.GetStatus(), Valid: true}, + } + if err := l.svcCtx.PostModel.Update(l.ctx, postModel); err != nil { + l.Error(errcode.Msg(errcode.Database)) + return nil, errcode.Wrap(errcode.Database) + } - return &post.Post{}, nil + return Get(l.ctx, l.svcCtx, l.Logger, postModel) } diff --git a/service/post/rpc/internal/test/update_logic_test.go b/service/post/rpc/internal/test/update_logic_test.go new file mode 100644 index 0000000..ef607c5 --- /dev/null +++ b/service/post/rpc/internal/test/update_logic_test.go @@ -0,0 +1,140 @@ +package test + +import ( + "context" + "testing" + "time" + + "github.com/google/uuid" + "github.com/linehk/go-microservices-blogger/errcode" + "github.com/linehk/go-microservices-blogger/service/comment/rpc/commentservice" + commentmodel "github.com/linehk/go-microservices-blogger/service/comment/rpc/model" + "github.com/linehk/go-microservices-blogger/service/post/rpc/internal/logic" + "github.com/linehk/go-microservices-blogger/service/post/rpc/internal/svc" + "github.com/linehk/go-microservices-blogger/service/post/rpc/model" + "github.com/linehk/go-microservices-blogger/service/post/rpc/post" + "github.com/stretchr/testify/assert" + "go.uber.org/mock/gomock" + "google.golang.org/protobuf/types/known/timestamppb" +) + +func TestUpdate(t *testing.T) { + ctrl := gomock.NewController(t) + ctx := context.Background() + postRepo := model.NewMockPostModel(ctrl) + imageRepo := model.NewMockImageModel(ctrl) + authorRepo := model.NewMockAuthorModel(ctrl) + commentService := commentservice.NewMockCommentService(ctrl) + labelRepo := model.NewMockLabelModel(ctrl) + locationRepo := model.NewMockLocationModel(ctrl) + commentRepo := commentmodel.NewMockCommentModel(ctrl) + logicService := logic.NewUpdateLogic(ctx, &svc.ServiceContext{ + AuthorModel: authorRepo, + ImageModel: imageRepo, + LabelModel: labelRepo, + LocationModel: locationRepo, + PostModel: postRepo, + CommentService: commentService, + CommentModel: commentRepo, + }) + defer ctrl.Finish() + + blogId := uuid.NewString() + postId := uuid.NewString() + published := time.Now() + updated := time.Now() + postUrl := "Url" + postSelfLink := "postSelfLink" + postTitle := "Title" + postTitleLink := "postTitleLink" + postContent := "Content" + customMetaData := "CustomMetaData" + postStatus := "Status" + + imageUrl1 := "imageUrl1" + imageUrl2 := "imageUrl2" + + displayName := "DisplayName" + authorUrl := "authorUrl" + + authorImageUrl := "authorImageUrl" + + commentStatus1 := "Status1" + commentStatus2 := "Status2" + commentSelfLink1 := "commentSelfLink1" + commentSelfLink2 := "commentSelfLink2" + commentContent1 := "commentContent1" + commentContent2 := "commentContent2" + commentAuthorDisplayName := "commentAuthorDisplayName" + commentAuthorUrl := "commentAuthorUrl" + commentAuthorImageUrl := "commentAuthorImageUrl" + + labelValue1 := "labelValue1" + labelValue2 := "labelValue2" + + locationName := "locationName" + locationLat := 1.1 + locationLng := 2.2 + locationSpan := "locationSpan" + + updateReq := &post.UpdateReq{ + BlogId: blogId, + PostId: postId, + Post: &post.Post{ + Published: timestamppb.New(published), + Updated: timestamppb.New(updated), + Url: postUrl, + SelfLink: postSelfLink, + Title: postTitle, + TitleLink: postTitleLink, + Content: postContent, + Images: []*post.Image{{Url: imageUrl1}, {Url: imageUrl2}}, + CustomMetaData: customMetaData, + Author: &post.Author{ + DisplayName: displayName, + Url: authorUrl, + Image: &post.Image{Url: authorImageUrl}, + }, + Replies: &post.Reply{ + Items: []*post.Comment{{ + Status: commentStatus1, + Published: timestamppb.New(published), + Updated: timestamppb.New(updated), + SelfLink: commentSelfLink1, + Content: commentContent1, + Author: &post.Author{ + DisplayName: commentAuthorDisplayName, + Url: commentAuthorUrl, + Image: &post.Image{Url: commentAuthorImageUrl}, + }, + }, { + Status: commentStatus2, + Published: timestamppb.New(published), + Updated: timestamppb.New(updated), + SelfLink: commentSelfLink2, + Content: commentContent2, + Author: &post.Author{ + DisplayName: commentAuthorDisplayName, + Url: commentAuthorUrl, + Image: &post.Image{Url: commentAuthorImageUrl}, + }, + }}, + }, + Labels: []string{labelValue1, labelValue2}, + Location: &post.Location{ + Name: locationName, + Lat: float32(locationLat), + Lng: float32(locationLng), + Span: locationSpan, + }, + Status: postStatus, + }, + } + + // Database + expectedErr := errcode.Wrap(errcode.Database) + postRepo.EXPECT().Update(ctx, gomock.Any()).Return(expectedErr) + actual, actualErr := logicService.Update(updateReq) + assert.Nil(t, actual) + assert.Equal(t, expectedErr, actualErr) +}