Skip to content

Commit 0cfa585

Browse files
committed
app 接口完善
1 parent 078736a commit 0cfa585

File tree

6 files changed

+60
-69
lines changed

6 files changed

+60
-69
lines changed

src/http/controller/app/article.go

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,44 +26,20 @@ func (this *ArticleController) RegisterRoute(g *echo.Group) {
2626

2727
// ReadList 网友文章列表页
2828
func (ArticleController) ReadList(ctx echo.Context) error {
29-
limit := 20
29+
curPage := goutils.MustInt(ctx.QueryParam("p"), 1)
30+
paginator := logic.NewPaginatorWithPerPage(curPage, perPage)
3031

31-
lastId := goutils.MustInt(ctx.QueryParam("base"))
32-
articles := logic.DefaultArticle.FindBy(ctx, limit+5, lastId)
33-
if articles == nil {
34-
return fail(ctx, "获取失败")
35-
}
32+
// 置顶的 article
33+
topArticles := logic.DefaultArticle.FindAll(ctx, paginator, "id DESC", "top=1")
3634

37-
articleList := make([]map[string]interface{}, 0, len(articles))
38-
for _, article := range articles {
39-
if lastId > 0 {
40-
if article.Top == 1 {
41-
continue
42-
}
43-
}
44-
articleList = append(articleList, map[string]interface{}{
45-
"id": article.Id,
46-
"name": article.Name,
47-
"title": article.Title,
48-
"pub_date": article.PubDate,
49-
"tags": article.Tags,
50-
"viewnum": article.Viewnum,
51-
"cmtnum": article.Cmtnum,
52-
"likenum": article.Likenum,
53-
"top": article.Top,
54-
"author": article.AuthorTxt,
55-
})
56-
}
35+
articles := logic.DefaultArticle.FindAll(ctx, paginator, "id DESC", "")
5736

58-
hasMore := false
59-
if len(articleList) > limit {
60-
hasMore = true
61-
articleList = articleList[:limit]
62-
}
37+
total := logic.DefaultArticle.Count(ctx, "")
38+
hasMore := paginator.SetTotal(total).HasMorePage()
6339

6440
data := map[string]interface{}{
41+
"articles": append(topArticles, articles...),
6542
"has_more": hasMore,
66-
"articles": articleList,
6743
}
6844

6945
return success(ctx, data)
@@ -85,9 +61,17 @@ func (ArticleController) Detail(ctx echo.Context) error {
8561
// 为了阅读数即时看到
8662
article.Viewnum++
8763

64+
// 回复信息(评论)
65+
replies, _, lastReplyUser := logic.DefaultComment.FindObjComments(ctx, article.Id, model.TypeArticle, 0, article.Lastreplyuid)
66+
// 有人回复
67+
if article.Lastreplyuid != 0 {
68+
article.LastReplyUser = lastReplyUser
69+
}
70+
8871
article.Txt = ""
8972
data := map[string]interface{}{
9073
"article": article,
74+
"replies": replies,
9175
}
9276

9377
// TODO: 暂时不用

src/http/controller/app/index.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type IndexController struct{}
2020
// 注册路由
2121
func (self IndexController) RegisterRoute(g *echo.Group) {
2222
g.GET("/home", self.Home)
23+
g.GET("/stat/site", self.WebsiteStat)
2324
}
2425

2526
// Home 首页
@@ -51,3 +52,26 @@ func (IndexController) Home(ctx echo.Context) error {
5152
}
5253
return success(ctx, nil)
5354
}
55+
56+
// WebsiteStat 网站统计信息
57+
func (IndexController) WebsiteStat(ctx echo.Context) error {
58+
articleTotal := logic.DefaultArticle.Total()
59+
projectTotal := logic.DefaultProject.Total()
60+
topicTotal := logic.DefaultTopic.Total()
61+
cmtTotal := logic.DefaultComment.Total()
62+
resourceTotal := logic.DefaultResource.Total()
63+
bookTotal := logic.DefaultGoBook.Total()
64+
userTotal := logic.DefaultUser.Total()
65+
66+
data := map[string]interface{}{
67+
"article": articleTotal,
68+
"project": projectTotal,
69+
"topic": topicTotal,
70+
"resource": resourceTotal,
71+
"book": bookTotal,
72+
"comment": cmtTotal,
73+
"user": userTotal,
74+
}
75+
76+
return success(ctx, data)
77+
}

src/http/controller/app/project.go

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,17 @@ func (self ProjectController) RegisterRoute(g *echo.Group) {
2626

2727
// ReadList 开源项目列表页
2828
func (ProjectController) ReadList(ctx echo.Context) error {
29-
limit := 20
29+
curPage := goutils.MustInt(ctx.QueryParam("p"), 1)
30+
paginator := logic.NewPaginatorWithPerPage(curPage, perPage)
3031

31-
lastId := goutils.MustInt(ctx.QueryParam("base"))
32-
projects := logic.DefaultProject.FindBy(ctx, limit+5, lastId)
33-
if projects == nil {
34-
return fail(ctx, "获取失败")
35-
}
36-
37-
projectList := make([]map[string]interface{}, 0, len(projects))
38-
for _, project := range projects {
39-
if lastId > 0 {
40-
// TODO: 推荐?
41-
// if project.Top == 1 {
42-
// continue
43-
// }
44-
}
45-
projectList = append(projectList, map[string]interface{}{
46-
"id": project.Id,
47-
"name": project.Name,
48-
"category": project.Category,
49-
"logo": project.Logo,
50-
"tags": project.Tags,
51-
"viewnum": project.Viewnum,
52-
"cmtnum": project.Cmtnum,
53-
"likenum": project.Likenum,
54-
"author": project.Author,
55-
"ctime": project.Ctime,
56-
})
57-
}
32+
projects := logic.DefaultProject.FindAll(ctx, paginator, "id DESC", "")
5833

59-
hasMore := false
60-
if len(projectList) > limit {
61-
hasMore = true
62-
projectList = projectList[:limit]
63-
}
34+
total := logic.DefaultProject.Count(ctx, "")
35+
hasMore := paginator.SetTotal(total).HasMorePage()
6436

6537
data := map[string]interface{}{
38+
"projects": projects,
6639
"has_more": hasMore,
67-
"projects": projectList,
6840
}
6941

7042
return success(ctx, data)
@@ -83,5 +55,15 @@ func (ProjectController) Detail(ctx echo.Context) error {
8355
// 为了阅读数即时看到
8456
project.Viewnum++
8557

86-
return success(ctx, map[string]interface{}{"project": project})
58+
// 回复信息(评论)
59+
replies, _, lastReplyUser := logic.DefaultComment.FindObjComments(ctx, project.Id, model.TypeProject, 0, project.Lastreplyuid)
60+
// 有人回复
61+
if project.Lastreplyuid != 0 {
62+
project.LastReplyUser = lastReplyUser
63+
}
64+
65+
return success(ctx, map[string]interface{}{
66+
"project": project,
67+
"replies": replies,
68+
})
8769
}

src/http/controller/app/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package app
99
import "github.com/labstack/echo"
1010

1111
func RegisterRoutes(g *echo.Group) {
12+
new(IndexController).RegisterRoute(g)
1213
new(ArticleController).RegisterRoute(g)
1314
new(TopicController).RegisterRoute(g)
1415
new(ResourceController).RegisterRoute(g)

src/model/article.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ type Article struct {
5858
User *User `json:"-" xorm:"-"`
5959
// 排行榜阅读量
6060
RankView int `json:"rank_view" xorm:"-"`
61-
LastReplyUser *User `json:"-" xorm:"-"`
61+
LastReplyUser *User `json:"last_reply_user" xorm:"-"`
6262
}
6363

6464
func (this *Article) AfterSet(name string, cell xorm.Cell) {

src/model/openproject.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type OpenProject struct {
4949
User *User `json:"user" xorm:"-"`
5050
// 排行榜阅读量
5151
RankView int `json:"rank_view" xorm:"-"`
52-
LastReplyUser *User `json:"-" xorm:"-"`
52+
LastReplyUser *User `json:"last_reply_user" xorm:"-"`
5353
}
5454

5555
func (this *OpenProject) BeforeInsert() {

0 commit comments

Comments
 (0)