-
Notifications
You must be signed in to change notification settings - Fork 263
/
content_repo.go
executable file
·125 lines (107 loc) · 2.96 KB
/
content_repo.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Copyright 2015 @ 56x.net.
* name : content_rep
* author : jarryliu
* date : -- :
* description :
* history :
*/
package repos
import (
contentImpl "github.com/ixre/go2o/core/domain/content"
"github.com/ixre/go2o/core/domain/interface/content"
"github.com/ixre/gof/db"
"github.com/ixre/gof/db/orm"
)
var _ content.IArchiveRepo = new(contentRepo)
type contentRepo struct {
db.Connector
o orm.Orm
}
// 内容仓储
func NewContentRepo(o orm.Orm) content.IArchiveRepo {
return &contentRepo{
Connector: o.Connector(),
o: o,
}
}
// 获取内容
func (c *contentRepo) GetContent(userId int64) content.IContent {
return contentImpl.NewContent(userId, c)
}
// 根据编号获取页面
func (c *contentRepo) GetPageById(mchId, id int32) *content.Page {
var e content.Page
if err := c.o.Get(id, &e); err == nil && e.UserId == int(mchId) {
return &e
}
return nil
}
// 根据标识获取页面
func (c *contentRepo) GetPageByCode(userId int, code string) *content.Page {
var e content.Page
if err := c.o.GetBy(&e, "user_id= $1 and code= $2", userId, code); err == nil {
return &e
}
return nil
}
// 删除页面
func (c *contentRepo) DeletePage(userId, id int32) error {
_, err := c.o.Delete(content.Page{}, "user_id= $1 AND id= $2", userId, id)
return err
}
// 保存页面
func (c *contentRepo) SavePage(userId int32, v *content.Page) (int32, error) {
return orm.I32(orm.Save(c.o, v, int(v.Id)))
}
// 获取文章数量
func (c *contentRepo) GetArticleNumByCategory(categoryId int32) int {
num := 0
c.Connector.ExecScalar("SELECT COUNT(1) FROM article_list WHERE cat_id= $1",
&num, categoryId)
return num
}
// 获取栏目
func (c *contentRepo) GetAllArticleCategory() []*content.ArticleCategory {
var list []*content.ArticleCategory
c.o.Select(&list, "")
return list
}
// 判断栏目是否存在
func (c *contentRepo) CategoryExists(alias string, id int32) bool {
num := 0
c.Connector.ExecScalar("SELECT COUNT(1) FROM article_category WHERE cat_alias= $1 and id <> $2",
&num, alias, id)
return num > 0
}
// 保存栏目
func (c *contentRepo) SaveCategory(v *content.ArticleCategory) (int32, error) {
return orm.I32(orm.Save(c.o, v, int(v.ID)))
}
// 删除栏目
func (c *contentRepo) DeleteCategory(id int32) error {
return c.o.DeleteByPk(&content.ArticleCategory{}, id)
}
// 获取文章
func (c *contentRepo) GetArticleById(id int32) *content.Article {
e := content.Article{}
if c.o.Get(id, &e) == nil {
return &e
}
return nil
}
// 获取文章列表
func (c *contentRepo) GetArticleList(categoryId int32, begin int, end int) []*content.Article {
list := []*content.Article{}
c.o.SelectByQuery(&content.Article{},
"cat_id= $1 LIMIT $3 OFFSET $2", categoryId, begin, end-begin)
return list
}
// 保存文章
func (c *contentRepo) SaveArticle(v *content.Article) (int32, error) {
return orm.I32(orm.Save(c.o, v, int(v.ID)))
}
// 删除文章
func (c *contentRepo) DeleteArticle(id int32) error {
return c.o.DeleteByPk(&content.Article{}, id)
}