From 23c112c2a9a6620063c58b66f9ed5044085d8ff7 Mon Sep 17 00:00:00 2001 From: sulinehk Date: Sat, 9 Mar 2024 16:52:10 +0800 Subject: [PATCH] feat(page): add Update --- .../page/rpc/internal/logic/update_logic.go | 22 +++++- .../rpc/internal/test/update_logic_test.go | 71 +++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 service/page/rpc/internal/test/update_logic_test.go diff --git a/service/page/rpc/internal/logic/update_logic.go b/service/page/rpc/internal/logic/update_logic.go index f14c7a3..41e5a04 100644 --- a/service/page/rpc/internal/logic/update_logic.go +++ b/service/page/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/page/rpc/internal/svc" + "github.com/linehk/go-microservices-blogger/service/page/rpc/model" "github.com/linehk/go-microservices-blogger/service/page/rpc/page" "github.com/zeromicro/go-zero/core/logx" @@ -24,7 +27,22 @@ func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogi } func (l *UpdateLogic) Update(in *page.UpdateReq) (*page.Page, error) { - // todo: add your logic here and delete this line + pageReq := in.GetPage() + pageModel := &model.Page{ + Uuid: in.GetPageId(), + BlogUuid: sql.NullString{String: in.GetBlogId(), Valid: true}, + Status: sql.NullString{String: pageReq.GetStatus(), Valid: true}, + Published: sql.NullTime{Time: pageReq.GetUpdated().AsTime(), Valid: true}, + Updated: sql.NullTime{Time: pageReq.GetUpdated().AsTime(), Valid: true}, + Url: sql.NullString{String: pageReq.GetUrl(), Valid: true}, + SelfLink: sql.NullString{String: pageReq.GetSelfLink(), Valid: true}, + Title: sql.NullString{String: pageReq.GetTitle(), Valid: true}, + Content: sql.NullString{String: pageReq.GetContent(), Valid: true}, + } + if err := l.svcCtx.PageModel.Update(l.ctx, pageModel); err != nil { + l.Error(errcode.Msg(errcode.Database)) + return nil, errcode.Wrap(errcode.Database) + } - return &page.Page{}, nil + return Get(l.ctx, l.svcCtx, l.Logger, pageModel) } diff --git a/service/page/rpc/internal/test/update_logic_test.go b/service/page/rpc/internal/test/update_logic_test.go new file mode 100644 index 0000000..dc0bf7e --- /dev/null +++ b/service/page/rpc/internal/test/update_logic_test.go @@ -0,0 +1,71 @@ +package test + +import ( + "context" + "testing" + "time" + + "github.com/google/uuid" + "github.com/linehk/go-microservices-blogger/errcode" + "github.com/linehk/go-microservices-blogger/service/page/rpc/internal/logic" + "github.com/linehk/go-microservices-blogger/service/page/rpc/internal/svc" + "github.com/linehk/go-microservices-blogger/service/page/rpc/model" + "github.com/linehk/go-microservices-blogger/service/page/rpc/page" + postmodel "github.com/linehk/go-microservices-blogger/service/post/rpc/model" + "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() + pageRepo := model.NewMockPageModel(ctrl) + authorRepo := postmodel.NewMockAuthorModel(ctrl) + imageRepo := postmodel.NewMockImageModel(ctrl) + logicService := logic.NewUpdateLogic(ctx, &svc.ServiceContext{ + PageModel: pageRepo, + AuthorModel: authorRepo, + ImageModel: imageRepo, + }) + defer ctrl.Finish() + + blogId := uuid.NewString() + pageId := uuid.NewString() + status := "Status" + published := time.Now() + updated := time.Now() + pageUrl := "Url" + selfLink := "SelfLink" + title := "Title" + content := "Content" + + displayName := "DisplayName" + authorUrl := "Url" + imageUrl := "imageUrl" + updateReq := &page.UpdateReq{ + BlogId: blogId, + PageId: pageId, + Page: &page.Page{ + Status: status, + Published: timestamppb.New(published), + Updated: timestamppb.New(updated), + Url: pageUrl, + SelfLink: selfLink, + Title: title, + Content: content, + Author: &page.Author{ + DisplayName: displayName, + Url: authorUrl, + Image: &page.Image{Url: imageUrl}, + }, + }, + } + + // Database + expectedErr := errcode.Wrap(errcode.Database) + pageRepo.EXPECT().Update(ctx, gomock.Any()).Return(expectedErr) + actual, actualErr := logicService.Update(updateReq) + assert.Nil(t, actual) + assert.Equal(t, expectedErr, actualErr) +}