Skip to content

Commit

Permalink
feat(comment): add Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
linehk committed Mar 10, 2024
1 parent cc2660a commit b1ebd49
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
19 changes: 16 additions & 3 deletions service/comment/rpc/internal/logic/delete_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package logic

import (
"context"
"errors"

"github.com/linehk/go-microservices-blogger/errcode"
"github.com/linehk/go-microservices-blogger/service/comment/rpc/comment"
"github.com/linehk/go-microservices-blogger/service/comment/rpc/internal/svc"

"github.com/linehk/go-microservices-blogger/service/comment/rpc/model"
"github.com/zeromicro/go-zero/core/logx"
)

Expand All @@ -24,7 +26,18 @@ func NewDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLogi
}

func (l *DeleteLogic) Delete(in *comment.DeleteReq) (*comment.EmptyResp, error) {
// todo: add your logic here and delete this line

commentModel, err := l.svcCtx.CommentModel.FindOneByUuid(l.ctx, in.GetCommentId())
if errors.Is(err, model.ErrNotFound) {
l.Error(errcode.Msg(errcode.CommentNotExist))
return nil, errcode.Wrap(errcode.CommentNotExist)
}
if err != nil {
l.Error(errcode.Msg(errcode.Database))
return nil, errcode.Wrap(errcode.Database)
}
if err := l.svcCtx.CommentModel.Delete(l.ctx, commentModel.Id); err != nil {
l.Error(errcode.Msg(errcode.Database))
return nil, errcode.Wrap(errcode.Database)
}
return &comment.EmptyResp{}, nil
}
90 changes: 90 additions & 0 deletions service/comment/rpc/internal/test/delete_logic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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/comment"
"github.com/linehk/go-microservices-blogger/service/comment/rpc/internal/logic"
"github.com/linehk/go-microservices-blogger/service/comment/rpc/internal/svc"
"github.com/linehk/go-microservices-blogger/service/comment/rpc/model"
postmodel "github.com/linehk/go-microservices-blogger/service/post/rpc/model"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func TestDelete(t *testing.T) {
ctrl := gomock.NewController(t)
ctx := context.Background()
commentRepo := model.NewMockCommentModel(ctrl)
authorRepo := postmodel.NewMockAuthorModel(ctrl)
imageRepo := postmodel.NewMockImageModel(ctrl)
logicService := logic.NewDeleteLogic(ctx, &svc.ServiceContext{
CommentModel: commentRepo,
AuthorModel: authorRepo,
ImageModel: imageRepo,
})
defer ctrl.Finish()

blogUuid := uuid.NewString()
postUuid := uuid.NewString()
commentUuid := uuid.NewString()
deleteReq := &comment.DeleteReq{
BlogId: blogUuid,
CommentId: commentUuid,
PostId: postUuid,
}

status := "Status"
published := time.Now()
updated := time.Now()
selfLink := "SelfLink"
content := "Content"
var commentPrimaryKey int64 = 1
commentModel := &model.Comment{
Id: commentPrimaryKey,
Uuid: commentUuid,
BlogUuid: sql.NullString{String: blogUuid, Valid: true},
PostUuid: sql.NullString{String: postUuid, Valid: true},
Status: sql.NullString{String: status, Valid: true},
Published: sql.NullTime{Time: published, Valid: true},
Updated: sql.NullTime{Time: updated, Valid: true},
SelfLink: sql.NullString{String: selfLink, Valid: true},
Content: sql.NullString{String: content, Valid: true},
}

expected := &comment.EmptyResp{}

// CommentNotExist
expectedErr := errcode.Wrap(errcode.CommentNotExist)
commentRepo.EXPECT().FindOneByUuid(ctx, commentUuid).Return(nil, model.ErrNotFound)
actual, actualErr := logicService.Delete(deleteReq)
assert.Nil(t, actual)
assert.Equal(t, expectedErr, actualErr)

// Database
expectedErr = errcode.Wrap(errcode.Database)
commentRepo.EXPECT().FindOneByUuid(ctx, commentUuid).Return(nil, expectedErr)
actual, actualErr = logicService.Delete(deleteReq)
assert.Nil(t, actual)
assert.Equal(t, expectedErr, actualErr)

// Database
expectedErr = errcode.Wrap(errcode.Database)
commentRepo.EXPECT().FindOneByUuid(ctx, commentUuid).Return(commentModel, nil)
commentRepo.EXPECT().Delete(ctx, commentPrimaryKey).Return(expectedErr)
actual, actualErr = logicService.Delete(deleteReq)
assert.Nil(t, actual)
assert.Equal(t, expectedErr, actualErr)

// Success
commentRepo.EXPECT().FindOneByUuid(ctx, commentUuid).Return(commentModel, nil)
commentRepo.EXPECT().Delete(ctx, commentPrimaryKey).Return(nil)
actual, actualErr = logicService.Delete(deleteReq)
assert.Equal(t, expected, actual)
assert.Nil(t, actualErr)
}

0 comments on commit b1ebd49

Please sign in to comment.