feat: add new template functions (#316)

* Fix update menu report update menu failed because of updateResult.RowsAffected=0

* Add template function

* Add template function
pull/317/head
Meepoljdx 1 year ago committed by GitHub
parent 2998933cee
commit 98d43b885d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -65,6 +65,7 @@ func InitApp() *fx.App {
extension.RegisterToolFunc, extension.RegisterToolFunc,
extension.RegisterPaginationFunc, extension.RegisterPaginationFunc,
extension.RegisterPostFunc, extension.RegisterPostFunc,
extension.RegisterStatisticFunc,
func(s *handler.Server) { func(s *handler.Server) {
s.RegisterRouters() s.RegisterRouters()
}, },

@ -39,6 +39,7 @@ func RegisterPostFunc(template *template.Template, postService service.PostServi
p.addListPostByCategorySlug() p.addListPostByCategorySlug()
p.addListPostByTagID() p.addListPostByTagID()
p.addListPostByTagSlug() p.addListPostByTagSlug()
p.addListMostPopularPost()
} }
func (p *postExtension) addListLatestPost() { func (p *postExtension) addListLatestPost() {
@ -62,6 +63,27 @@ func (p *postExtension) addListLatestPost() {
p.Template.AddFunc("listLatestPost", listLatestPostFunc) p.Template.AddFunc("listLatestPost", listLatestPostFunc)
} }
func (p *postExtension) addListMostPopularPost() {
listMostPopularPost := func(top int) ([]*vo.Post, error) {
ctx := context.Background()
posts, _, err := p.PostService.Page(ctx, param.PostQuery{
Page: param.Page{
PageNum: 0,
PageSize: top,
},
Sort: &param.Sort{
Fields: []string{"visits,desc"},
},
Statuses: []*consts.PostStatus{consts.PostStatusPublished.Ptr()},
})
if err != nil {
return nil, err
}
return p.PostAssembler.ConvertToListVO(ctx, posts)
}
p.Template.AddFunc("listMostPopularPost", listMostPopularPost)
}
func (p *postExtension) addGetPostCount() { func (p *postExtension) addGetPostCount() {
getPostCountFunc := func() (int64, error) { getPostCountFunc := func() (int64, error) {
ctx := context.Background() ctx := context.Background()

@ -0,0 +1,34 @@
package extension
import (
"context"
"github.com/go-sonic/sonic/model/dto"
"github.com/go-sonic/sonic/service"
"github.com/go-sonic/sonic/template"
)
type statisticExtension struct {
Template *template.Template
StatisticService service.StatisticService
}
func RegisterStatisticFunc(template *template.Template, statisticService service.StatisticService) {
s := &statisticExtension{
Template: template,
StatisticService: statisticService,
}
s.addGetStatisticsData()
}
func (s *statisticExtension) addGetStatisticsData() {
getStatisticsDataFunc := func() (*dto.Statistic, error) {
ctx := context.Background()
statistic, err := s.StatisticService.Statistic(ctx)
if err != nil {
return nil, err
}
return statistic, nil
}
s.Template.AddFunc("getStatisticsData", getStatisticsDataFunc)
}
Loading…
Cancel
Save