Skip to content

Commit

Permalink
feat(post): add Search
Browse files Browse the repository at this point in the history
  • Loading branch information
linehk committed Mar 8, 2024
1 parent 00ca2b3 commit 359f292
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 2 deletions.
26 changes: 24 additions & 2 deletions service/post/rpc/internal/logic/search_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,26 @@ func NewSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchLogi
}

func (l *SearchLogic) Search(in *post.SearchReq) (*post.SearchResp, error) {
// todo: add your logic here and delete this line
postModelList, err := l.svcCtx.PostModel.SearchByTitle(l.ctx, in.GetBlogId(), in.GetQ())
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)
}

var searchResp post.SearchResp
searchResp.Kind = "blogger#postList"
searchResp.NextPageToken = ""
for _, postModel := range postModelList {
postResp, err := Get(l.ctx, l.svcCtx, l.Logger, postModel)
if err != nil {
return nil, err
}
searchResp.Items = append(searchResp.Items, postResp)
}

return &post.SearchResp{}, nil
return &searchResp, nil
}
57 changes: 57 additions & 0 deletions service/post/rpc/internal/test/search_logic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package test

import (
"context"
"testing"

"github.com/google/uuid"
"github.com/linehk/go-microservices-blogger/errcode"
"github.com/linehk/go-microservices-blogger/service/comment/rpc/commentservice"
"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 TestSearch(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)
logicService := logic.NewSearchLogic(ctx, &svc.ServiceContext{
AuthorModel: authorRepo,
ImageModel: imageRepo,
LabelModel: labelRepo,
LocationModel: locationRepo,
PostModel: postRepo,
CommentService: commentService,
})
defer ctrl.Finish()

blogId := uuid.NewString()
title := "title"
searchReq := &post.SearchReq{
BlogId: blogId,
Q: title,
}

// PostNotExist
expectedErr := errcode.Wrap(errcode.PostNotExist)
postRepo.EXPECT().SearchByTitle(ctx, blogId, title).Return(nil, model.ErrNotFound)
actual, actualErr := logicService.Search(searchReq)
assert.Nil(t, actual)
assert.Equal(t, expectedErr, actualErr)

// Database
expectedErr = errcode.Wrap(errcode.Database)
postRepo.EXPECT().SearchByTitle(ctx, blogId, title).Return(nil, expectedErr)
actual, actualErr = logicService.Search(searchReq)
assert.Nil(t, actual)
assert.Equal(t, expectedErr, actualErr)
}
15 changes: 15 additions & 0 deletions service/post/rpc/model/mock_post_model.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions service/post/rpc/model/post_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type (
postModel
FindOneByBlogUuidAndPostUuid(ctx context.Context, blogUuid, postUuid string) (*Post, error)
ListByBlogUuid(ctx context.Context, blogUuid string) ([]*Post, error)
SearchByTitle(ctx context.Context, blogUuid, title string) ([]*Post, error)
}

customPostModel struct {
Expand All @@ -36,6 +37,7 @@ func NewPostModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) Po
var (
cachePublicPostBlogUuidAndPostUuid = "cache:public:post:blogUuid:%s:postUuid:%s"
cachePublicPostBlogUuidPrefix = "cache:public:post:blogUuid:"
cachePublicPostBlogUuidAndTitle = "cache:public:post:blogUuid:%s:title:%s"
)

func (c *customPostModel) FindOneByBlogUuidAndPostUuid(ctx context.Context, blogUuid, postUuid string) (*Post, error) {
Expand Down Expand Up @@ -77,3 +79,23 @@ func (c *customPostModel) ListByBlogUuid(ctx context.Context, blogUuid string) (
return nil, err
}
}

func (c *customPostModel) SearchByTitle(ctx context.Context, blogUuid, title string) ([]*Post, error) {
publicPostBlogUuidAndTitleKey := fmt.Sprintf(cachePublicPostBlogUuidAndTitle, blogUuid, title)
var resp []*Post
err := c.QueryRowIndexCtx(ctx, &resp, publicPostBlogUuidAndTitleKey, c.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where blog_uuid = $1 and title like $2", postRows, c.table)
if err := conn.QueryRowCtx(ctx, &resp, query, blogUuid, "'"+title+"%'"); err != nil {
return nil, err
}
return resp[0].Id, nil
}, c.queryPrimary)
switch err {
case nil:
return resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}

0 comments on commit 359f292

Please sign in to comment.