Skip to content

Commit 98d43b8

Browse files
authored
feat: add new template functions (#316)
* Fix update menu report update menu failed because of updateResult.RowsAffected=0 * Add template function * Add template function
1 parent 2998933 commit 98d43b8

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func InitApp() *fx.App {
6565
extension.RegisterToolFunc,
6666
extension.RegisterPaginationFunc,
6767
extension.RegisterPostFunc,
68+
extension.RegisterStatisticFunc,
6869
func(s *handler.Server) {
6970
s.RegisterRouters()
7071
},

template/extension/post.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func RegisterPostFunc(template *template.Template, postService service.PostServi
3939
p.addListPostByCategorySlug()
4040
p.addListPostByTagID()
4141
p.addListPostByTagSlug()
42+
p.addListMostPopularPost()
4243
}
4344

4445
func (p *postExtension) addListLatestPost() {
@@ -62,6 +63,27 @@ func (p *postExtension) addListLatestPost() {
6263
p.Template.AddFunc("listLatestPost", listLatestPostFunc)
6364
}
6465

66+
func (p *postExtension) addListMostPopularPost() {
67+
listMostPopularPost := func(top int) ([]*vo.Post, error) {
68+
ctx := context.Background()
69+
posts, _, err := p.PostService.Page(ctx, param.PostQuery{
70+
Page: param.Page{
71+
PageNum: 0,
72+
PageSize: top,
73+
},
74+
Sort: &param.Sort{
75+
Fields: []string{"visits,desc"},
76+
},
77+
Statuses: []*consts.PostStatus{consts.PostStatusPublished.Ptr()},
78+
})
79+
if err != nil {
80+
return nil, err
81+
}
82+
return p.PostAssembler.ConvertToListVO(ctx, posts)
83+
}
84+
p.Template.AddFunc("listMostPopularPost", listMostPopularPost)
85+
}
86+
6587
func (p *postExtension) addGetPostCount() {
6688
getPostCountFunc := func() (int64, error) {
6789
ctx := context.Background()

template/extension/statistic.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package extension
2+
3+
import (
4+
"context"
5+
6+
"github.com/go-sonic/sonic/model/dto"
7+
"github.com/go-sonic/sonic/service"
8+
"github.com/go-sonic/sonic/template"
9+
)
10+
11+
type statisticExtension struct {
12+
Template *template.Template
13+
StatisticService service.StatisticService
14+
}
15+
16+
func RegisterStatisticFunc(template *template.Template, statisticService service.StatisticService) {
17+
s := &statisticExtension{
18+
Template: template,
19+
StatisticService: statisticService,
20+
}
21+
s.addGetStatisticsData()
22+
}
23+
24+
func (s *statisticExtension) addGetStatisticsData() {
25+
getStatisticsDataFunc := func() (*dto.Statistic, error) {
26+
ctx := context.Background()
27+
statistic, err := s.StatisticService.Statistic(ctx)
28+
if err != nil {
29+
return nil, err
30+
}
31+
return statistic, nil
32+
}
33+
s.Template.AddFunc("getStatisticsData", getStatisticsDataFunc)
34+
}

0 commit comments

Comments
 (0)