Skip to content

Commit

Permalink
feat(page): add Update
Browse files Browse the repository at this point in the history
  • Loading branch information
linehk committed Mar 9, 2024
1 parent b827c41 commit 23c112c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
22 changes: 20 additions & 2 deletions service/page/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/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"
Expand All @@ -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)
}
71 changes: 71 additions & 0 deletions service/page/rpc/internal/test/update_logic_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 23c112c

Please sign in to comment.