Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add route #36

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions application/article/api/article.api
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ type (
AuthorId string `json:"author_id"`
AuthorName string `json:"author_name"`
}

ArticleListRequest {
AuthorId int64 `form:"author_id"`
Cursor int64 `form:"cursor"`
PageSize int64 `form:"page_size"`
SortType int32 `form:"sort_type"`
ArticleId int64 `form:"article_id"`
}

ArticleInfo {
ArticleId int64 `json:"article_id"`
Title string `json:"title"`
Content string `json:"content"`
Description string `json:"description"`
Cover string `json:"cover"`
}

ArticleListResponse {
Articles []ArticleInfo `json:"articles"`
}
)

@server (
Expand All @@ -41,4 +61,6 @@ service article-api {
post /publish (PublishRequest) returns (PublishResponse)
@handler ArticleDetailHandler
get /detail (ArticleDetailRequest) returns (ArticleDetailResponse)
@handler ArticleListHandler
get /list (ArticleListRequest) returns (ArticleListResponse)
}
28 changes: 28 additions & 0 deletions application/article/api/internal/handler/articlelisthandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package handler

import (
"net/http"

"beyond/application/article/api/internal/logic"
"beyond/application/article/api/internal/svc"
"beyond/application/article/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)

func ArticleListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ArticleListRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}

l := logic.NewArticleListLogic(r.Context(), svcCtx)
resp, err := l.ArticleList(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
5 changes: 5 additions & 0 deletions application/article/api/internal/handler/routes.go

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

55 changes: 55 additions & 0 deletions application/article/api/internal/logic/articlelistlogic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package logic

import (
"context"

"beyond/application/article/api/internal/svc"
"beyond/application/article/api/internal/types"
"beyond/application/article/rpc/article"

"github.com/zeromicro/go-zero/core/logx"
)

type ArticleListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewArticleListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ArticleListLogic {
return &ArticleListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *ArticleListLogic) ArticleList(req *types.ArticleListRequest) (resp *types.ArticleListResponse, err error) {
articles, err := l.svcCtx.ArticleRPC.Articles(l.ctx, &article.ArticlesRequest{
UserId: req.AuthorId,
Cursor: req.Cursor,
PageSize: req.PageSize,
SortType: req.SortType,
ArticleId: req.ArticleId,
})
if err != nil {
logx.Errorf("get articles req: %v err: %v", req, err)
return nil, err
}
if articles == nil || len(articles.Articles) == 0 {
return &types.ArticleListResponse{}, nil
}
articleInfos := make([]types.ArticleInfo, 0, len(articles.Articles))
for _, a := range articles.Articles {
articleInfos = append(articleInfos, types.ArticleInfo{
ArticleId: a.Id,
Title: a.Title,
Description: a.Description,
Cover: a.Cover,
})
}

return &types.ArticleListResponse{
Articles: articleInfos,
}, nil
}
20 changes: 20 additions & 0 deletions application/article/api/internal/types/types.go

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