-
Notifications
You must be signed in to change notification settings - Fork 250
/
tag.go
84 lines (78 loc) · 2.44 KB
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package model
import (
"context"
"github.com/go-sonic/sonic/consts"
"github.com/go-sonic/sonic/model/dto"
"github.com/go-sonic/sonic/model/param"
"github.com/go-sonic/sonic/model/property"
"github.com/go-sonic/sonic/service"
"github.com/go-sonic/sonic/service/assembler"
"github.com/go-sonic/sonic/template"
)
func NewTagModel(optionService service.OptionService,
themeService service.ThemeService,
tagService service.TagService,
postTagService service.PostTagService,
postAssembler assembler.PostAssembler,
) *TagModel {
return &TagModel{
OptionService: optionService,
ThemeService: themeService,
TagService: tagService,
PostAssembler: postAssembler,
PostTagService: postTagService,
}
}
type TagModel struct {
TagService service.TagService
OptionService service.OptionService
ThemeService service.ThemeService
PostTagService service.PostTagService
MetaService service.MetaService
PostAssembler assembler.PostAssembler
}
func (t *TagModel) Tags(ctx context.Context, model template.Model) (string, error) {
model["is_tags"] = true
model["meta_keywords"] = t.OptionService.GetOrByDefault(ctx, property.SeoKeywords)
model["meta_description"] = t.OptionService.GetOrByDefault(ctx, property.SeoDescription)
return t.ThemeService.Render(ctx, "tags")
}
func (t *TagModel) TagPosts(ctx context.Context, model template.Model, slug string, page int) (string, error) {
tag, err := t.TagService.GetBySlug(ctx, slug)
if err != nil {
return "", err
}
tagDTO, err := t.TagService.ConvertToDTO(ctx, tag)
if err != nil {
return "", err
}
pageSize := t.OptionService.GetOrByDefault(ctx, property.ArchivePageSize).(int)
posts, totalPage, err := t.PostTagService.PagePost(ctx, param.PostQuery{
Page: param.Page{
PageNum: page,
PageSize: pageSize,
},
Sort: ¶m.Sort{
Fields: []string{"createTime,desc"},
},
Statuses: []*consts.PostStatus{consts.PostStatusPublished.Ptr()},
TagID: &tag.ID,
})
if err != nil {
return "", err
}
postVOs, err := t.PostAssembler.ConvertToListVO(ctx, posts)
if err != nil {
return "", err
}
postPage := dto.NewPage(postVOs, totalPage, param.Page{
PageNum: page,
PageSize: pageSize,
})
model["is_tag"] = true
model["posts"] = postPage
model["tag"] = tagDTO
model["meta_keywords"] = t.OptionService.GetOrByDefault(ctx, property.SeoKeywords)
model["meta_description"] = t.OptionService.GetOrByDefault(ctx, property.SeoDescription)
return t.ThemeService.Render(ctx, "tag")
}