From 00ca2b3d9e8f9ba164956d78a53c2ed703aa489d Mon Sep 17 00:00:00 2001 From: sulinehk Date: Fri, 8 Mar 2024 19:48:32 +0800 Subject: [PATCH] feat(post): add Revert --- .../post/rpc/internal/logic/revert_logic.go | 20 +++- .../rpc/internal/test/revert_logic_test.go | 94 +++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 service/post/rpc/internal/test/revert_logic_test.go diff --git a/service/post/rpc/internal/logic/revert_logic.go b/service/post/rpc/internal/logic/revert_logic.go index 1db67ff..0fb6827 100644 --- a/service/post/rpc/internal/logic/revert_logic.go +++ b/service/post/rpc/internal/logic/revert_logic.go @@ -2,8 +2,11 @@ package logic import ( "context" + "errors" + "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,20 @@ func NewRevertLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RevertLogi } func (l *RevertLogic) Revert(in *post.RevertReq) (*post.Post, error) { - // todo: add your logic here and delete this line + postModel, err := l.svcCtx.PostModel.FindOneByBlogUuidAndPostUuid(l.ctx, in.GetBlogId(), in.GetPostId()) + if errors.Is(err, model.ErrNotFound) { + l.Error(errcode.Msg(errcode.PostNotExist)) + return nil, errcode.Wrap(errcode.PostNotExist) + } + if err != nil { + l.Error(errcode.Msg(errcode.Database)) + return nil, errcode.Wrap(errcode.Database) + } + postModel.Published.Valid = false + + if err := l.svcCtx.PostModel.Update(l.ctx, postModel); err != nil { + return nil, err + } - return &post.Post{}, nil + return Get(l.ctx, l.svcCtx, l.Logger, postModel) } diff --git a/service/post/rpc/internal/test/revert_logic_test.go b/service/post/rpc/internal/test/revert_logic_test.go new file mode 100644 index 0000000..baf2d03 --- /dev/null +++ b/service/post/rpc/internal/test/revert_logic_test.go @@ -0,0 +1,94 @@ +package test + +import ( + "context" + "database/sql" + "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" +) + +func TestRevert(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.NewRevertLogic(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() + publishReq := &post.RevertReq{ + BlogId: blogId, + PostId: postId, + } + + published := time.Now() + updated := time.Now() + postUrl := "Url" + postSelfLink := "postSelfLink" + postTitle := "Title" + postTitleLink := "postTitleLink" + postContent := "Content" + customMetaData := "CustomMetaData" + postStatus := "Status" + postModel := &model.Post{ + Id: 1, + Uuid: postId, + BlogUuid: sql.NullString{String: blogId, Valid: true}, + Published: sql.NullTime{Time: published, Valid: true}, + Updated: sql.NullTime{Time: updated, Valid: true}, + Url: postUrl, + SelfLink: sql.NullString{String: postSelfLink, Valid: true}, + Title: sql.NullString{String: postTitle, Valid: true}, + TitleLink: sql.NullString{String: postTitleLink, Valid: true}, + Content: sql.NullString{String: postContent, Valid: true}, + CustomMetaData: sql.NullString{String: customMetaData, Valid: true}, + Status: sql.NullString{String: postStatus, Valid: true}, + } + + // PostNotExist + expectedErr := errcode.Wrap(errcode.PostNotExist) + postRepo.EXPECT().FindOneByBlogUuidAndPostUuid(ctx, blogId, postId).Return(nil, model.ErrNotFound) + actual, actualErr := logicService.Revert(publishReq) + assert.Nil(t, actual) + assert.Equal(t, expectedErr, actualErr) + + // FindOneByBlogUuidAndPostUuid Database + expectedErr = errcode.Wrap(errcode.Database) + postRepo.EXPECT().FindOneByBlogUuidAndPostUuid(ctx, blogId, postId).Return(nil, expectedErr) + actual, actualErr = logicService.Revert(publishReq) + assert.Nil(t, actual) + assert.Equal(t, expectedErr, actualErr) + + // Update Database + expectedErr = errcode.Wrap(errcode.Database) + postRepo.EXPECT().FindOneByBlogUuidAndPostUuid(ctx, blogId, postId).Return(postModel, nil) + postRepo.EXPECT().Update(ctx, gomock.Any()).Return(expectedErr) + actual, actualErr = logicService.Revert(publishReq) + assert.Nil(t, actual) + assert.Equal(t, expectedErr, actualErr) +}