/
dao.go
249 lines (200 loc) · 5.47 KB
/
dao.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package post
import (
"github.com/andycai/werite/library/database"
"github.com/andycai/werite/model"
"gorm.io/gorm"
)
type PostDao struct{}
var Dao = new(PostDao)
//#region Post
func (pd PostDao) GetBySlug(slug string) (*model.Post, error) {
var post model.Post
err := db.Model(&post).
Where("slug = ?", slug).
Find(&post).Error
return &post, err
}
func (pd PostDao) GetByID(id uint) (*model.Post, error) {
var post model.Post
err := db.Model(&post).
Where("id = ?", id).
Preload("Tags", func(db *gorm.DB) *gorm.DB {
return db.Order("tags.name asc")
}).
Find(&post).Error
return &post, err
}
func (pd PostDao) CountByPublished() int64 {
return pd.countByDraft(0)
}
func (pd PostDao) CountByDraft() int64 {
return pd.countByDraft(1)
}
func (pd PostDao) countByDraft(draft int) int64 {
var post model.Post
var count int64
db.Model(&post).Where("is_draft = ?", draft).Count(&count)
return count
}
func (pd PostDao) CountByTrash() int64 {
var post model.Post
var count int64
db.Model(&post).Unscoped().Where("deleted_at IS NOT NULL").Count(&count)
return count
}
func (pd PostDao) Count() int64 {
var post model.Post
var count int64
db.Model(&post).Count(&count)
return count
}
func (pd PostDao) GetAllByPage(page, numPerPage int) []model.Post {
return pd.GetListByPage(page, numPerPage, 0, "")
}
func (pd PostDao) GetListByPage(page, numPerPage int, categoryID int, q string) []model.Post {
var posts []model.Post
tx := db.Model(&posts).
Preload("Tags", func(db *gorm.DB) *gorm.DB {
return db.Order("tags.name asc")
}).
Preload("User").
Preload("Category")
tx = database.DecorateEqualInt(tx, "category_id", categoryID)
tx = database.DecorateLike(tx, "title", q)
tx.Limit(numPerPage).
Offset(page * numPerPage).
Order("created_at desc").
Find(&posts)
return posts
}
func (pd PostDao) GetPublishedListByPage(page, numPerPage int, categoryID int, q string) []model.Post {
var posts []model.Post
tx := db.Model(&posts).
Preload("Tags", func(db *gorm.DB) *gorm.DB {
return db.Order("tags.name asc")
}).
Preload("User").
Preload("Category").
Where("is_draft = ?", 0)
tx = database.DecorateEqualInt(tx, "category_id", categoryID)
tx = database.DecorateLike(tx, "title", q)
tx.Limit(numPerPage).
Offset(page * numPerPage).
Order("created_at desc").
Find(&posts)
return posts
}
func (pd PostDao) GetDraftListByPage(page, numPerPage int, categoryID int, q string) []model.Post {
var posts []model.Post
tx := db.Model(&posts).
Preload("Tags", func(db *gorm.DB) *gorm.DB {
return db.Order("tags.name asc")
}).
Preload("User").
Preload("Category").
Where("is_draft = ?", 1)
tx = database.DecorateEqualInt(tx, "category_id", categoryID)
tx = database.DecorateLike(tx, "title", q)
tx.Limit(numPerPage).
Offset(page * numPerPage).
Order("created_at desc").
Find(&posts)
return posts
}
func (pd PostDao) GetTrashListByPage(page, numPerPage int, categoryID int, q string) []model.Post {
var posts []model.Post
tx := db.Model(&posts).
Preload("Tags", func(db *gorm.DB) *gorm.DB {
return db.Order("tags.name asc")
}).
Preload("User").
Preload("Category").
Unscoped().
Where("deleted_at IS NOT NULL")
tx = database.DecorateEqualInt(tx, "category_id", categoryID)
tx = database.DecorateLike(tx, "title", q)
tx.Limit(numPerPage).
Offset(page * numPerPage).
Order("created_at desc").
Find(&posts)
return posts
}
func (pd PostDao) UpdateDraft(value uint) {
db.Model(&model.Post{}).Update("is_draft", value)
}
func (pd PostDao) DeleteByIds(ids []uint) {
var post model.Post
db.Where(ids).Delete(&post)
}
func (pd PostDao) DeletePermanetlyByIds(ids []uint) {
var post model.Post
db.Unscoped().Where(ids).Delete(&post)
}
func (pd PostDao) RestoreByIds(ids []uint) {
var post model.Post
db.Unscoped().Model(&post).Where("id IN ?", ids).Update("deleted_at", nil)
}
//#endregion
//#region Category
func (pd PostDao) CountCatgegory() int64 {
var category model.Category
var count int64
db.Model(&category).Count(&count)
return count
}
func (pd PostDao) GetCategories() []model.Category {
var categories []model.Category
db.Model(&categories).
Find(&categories)
return categories
}
func (pd PostDao) GetCategoriesByPage(page, numPerPage int) []model.Category {
var categories []model.Category
db.Model(&categories).
Limit(numPerPage).
Offset(page * numPerPage).
Find(&categories)
return categories
}
func (pd PostDao) GetCategoryByID(id uint) (*model.Category, error) {
var categoryVo model.Category
err := db.Model(&categoryVo).Where("id = ?", id).Find(&categoryVo).Error
return &categoryVo, err
}
func (pd PostDao) DeleteCategoriesByIds(ids []uint) {
var category model.Category
// categories := make([]model.Category, len(ids))
// for i, v := range ids {
// categories[i].ID = v
// }
db.Where(ids).Delete(&category)
}
//#region Tag
func (pd PostDao) CountTag() int64 {
var tag model.Tag
var count int64
db.Model(&tag).Count(&count)
return count
}
func (pd PostDao) GetTagsByPage(page, numPerPage int) []model.Tag {
var tags []model.Tag
db.Model(&tags).
Limit(numPerPage).
Offset(page * numPerPage).
Find(&tags)
return tags
}
func (pd PostDao) GetTagByID(id uint) (*model.Tag, error) {
var tagVo model.Tag
err := db.Model(&tagVo).Where("id = ?", id).Find(&tagVo).Error
return &tagVo, err
}
func (pd PostDao) DeleteTagsByIds(ids []uint) {
var tag model.Tag
// tags := make([]model.Tag, len(ids))
// for i, v := range ids {
// tags[i].ID = v
// }
db.Where(ids).Delete(&tag)
}
//#endregion