Skip to content

Commit

Permalink
feat(post): add Revert
Browse files Browse the repository at this point in the history
  • Loading branch information
linehk committed Mar 8, 2024
1 parent 3aecc5e commit 00ca2b3
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 2 deletions.
20 changes: 18 additions & 2 deletions service/post/rpc/internal/logic/revert_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
94 changes: 94 additions & 0 deletions service/post/rpc/internal/test/revert_logic_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 00ca2b3

Please sign in to comment.