Skip to content

Commit

Permalink
feat(post): add Update
Browse files Browse the repository at this point in the history
  • Loading branch information
linehk committed Mar 8, 2024
1 parent 359f292 commit 9434757
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 2 deletions.
24 changes: 22 additions & 2 deletions service/post/rpc/internal/logic/update_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
140 changes: 140 additions & 0 deletions service/post/rpc/internal/test/update_logic_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9434757

Please sign in to comment.