forked from zxysilent/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcate.go
134 lines (124 loc) · 2.87 KB
/
cate.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
126
127
128
129
130
131
132
133
134
package model
// Cate 分类
type Cate struct {
Id int `xorm:"pk autoincr INT(11) not null" json:"id" form:"id"`
Name string `xorm:"unique VARCHAR(64) default('''') not null" json:"name" form:"name"`
Intro string `xorm:"VARCHAR(64) default('''') not null" json:"intro,omitempty" form:"intro"`
}
// CateIds 通过id返回新闻类别信息集合
func cateIds(ids []int) map[int]*Cate {
mods := make([]Cate, 0, 6)
Db.In("id", ids).Find(&mods)
if len(mods) > 0 {
mapSet := make(map[int]*Cate, len(mods))
for idx := range mods {
mods[idx].Intro = ""
mapSet[mods[idx].Id] = &mods[idx]
}
return mapSet
}
return nil
}
//CateGet 一个分类
func CateGet(id int) (*Cate, bool) {
mod := &Cate{
Id: id,
}
has, _ := Db.Get(mod)
return mod, has
}
// CateName 通过name 查询分类
func CateName(nam string) (*Cate, bool) {
mod := &Cate{
Name: nam,
}
has, _ := Db.Get(mod)
return mod, has
}
// CateAll 所有分类
func CateAll() ([]Cate, error) {
mods := make([]Cate, 0, 4)
err := Db.Asc("id").Find(&mods)
return mods, err
}
// CateAdd 添加分类
func CateAdd(mod *Cate) bool {
sess := Db.NewSession()
defer sess.Close()
sess.Begin()
affect, _ := sess.InsertOne(mod)
if affect != 1 {
sess.Rollback()
return false
}
sess.Commit()
return true
}
// CateEdit 修改分类
func CateEdit(mod *Cate) bool {
sess := Db.NewSession()
defer sess.Close()
sess.Begin()
affect, err := sess.ID(mod.Id).Cols("Name", "Intro").Update(mod)
if affect >= 0 && err == nil {
sess.Commit()
return true
}
sess.Rollback()
return false
}
// CateDel 删除分类
func CateDel(id int) bool {
sess := Db.NewSession()
defer sess.Close()
sess.Begin()
if affect, err := sess.ID(id).Delete(&Cate{}); affect > 0 && err == nil {
sess.Commit()
Db.ClearCacheBean(&Cate{}, string(id))
return true
}
sess.Rollback()
return false
}
// CatePostCount 通过标签查询文章分页总数
// lmt 是否前台限制
func CatePostCount(cid int, lmt bool) int {
sess := Db.NewSession()
defer sess.Close()
if cid > 0 {
sess.Where("Cate_id = ? ", cid)
}
if lmt {
sess.Where("Is_Public = 1 and Status = 3 ")
}
sess.Where("Type = 0")
mod := &Post{}
count, _ := sess.Count(mod)
return int(count)
}
// CatePostList 通过分类查询文章分页
// lmt 是否前台限制
func CatePostList(cid, pi, ps int, lmt bool) ([]Post, error) {
mods := make([]Post, 0, ps)
sess := Db.NewSession()
defer sess.Close()
if cid > 0 {
sess.Where("Cate_id = ? ", cid)
}
if lmt {
sess.Where("Is_Public = 1 and Status = 3 ")
}
sess.Where("Type = 0")
err := sess.Cols("id", "title", "path", "create_time", "summary", "comment_num", "options").Desc("create_time").Limit(ps, (pi-1)*ps).Find(&mods)
if len(mods) > 0 {
for idx := range mods {
if !lmt {
mods[idx].Summary = ""
}
mods[idx].Options = ""
mods[idx].Content = ""
mods[idx].MarkdownContent = ""
}
}
return mods, err
}