Skip to content

Commit

Permalink
Merge pull request #36 from zhoushuguang/dev
Browse files Browse the repository at this point in the history
add route
  • Loading branch information
zhoushuguang authored Nov 28, 2023
2 parents e6ea165 + 646b410 commit 5963712
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
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.

0 comments on commit 5963712

Please sign in to comment.