/
topic_tag_repository.go
90 lines (73 loc) · 2.34 KB
/
topic_tag_repository.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
package repositories
import (
"github.com/jimersylee/go-bbs/model"
"github.com/jimersylee/go-bbs/utils/simple"
"github.com/jinzhu/gorm"
)
var TopicTagRepository = newTopicTagRepository()
func newTopicTagRepository() *topicTagRepository {
return &topicTagRepository{}
}
type topicTagRepository struct {
}
func (this *topicTagRepository) Get(db *gorm.DB, id int64) *model.TopicTag {
ret := &model.TopicTag{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (this *topicTagRepository) Take(db *gorm.DB, where ...interface{}) *model.TopicTag {
ret := &model.TopicTag{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (this *topicTagRepository) QueryCnd(db *gorm.DB, cnd *simple.QueryCnd) (list []model.TopicTag, err error) {
err = cnd.DoQuery(db).Find(&list).Error
return
}
func (this *topicTagRepository) Query(db *gorm.DB, queries *simple.ParamQueries) (list []model.TopicTag, paging *simple.Paging) {
queries.StartQuery(db).Find(&list)
queries.StartCount(db).Model(&model.TopicTag{}).Count(&queries.Paging.Total)
paging = queries.Paging
return
}
func (this *topicTagRepository) Create(db *gorm.DB, t *model.TopicTag) (err error) {
err = db.Create(t).Error
return
}
func (this *topicTagRepository) Update(db *gorm.DB, t *model.TopicTag) (err error) {
err = db.Save(t).Error
return
}
func (this *topicTagRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&model.TopicTag{}).Where("id = ?", id).Updates(columns).Error
return
}
func (this *topicTagRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&model.TopicTag{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (this *topicTagRepository) Delete(db *gorm.DB, id int64) {
db.Model(&model.TopicTag{}).Delete("id", id)
}
func (this *topicTagRepository) AddTopicTags(db *gorm.DB, topicId int64, tagIds []int64) {
if topicId <= 0 || len(tagIds) == 0 {
return
}
for _, tagId := range tagIds {
_ = this.Create(db, &model.TopicTag{
TopicId: topicId,
TagId: tagId,
CreateTime: simple.NowTimestamp(),
})
}
}
func (this *topicTagRepository) RemoveTopicTags(db *gorm.DB, topicId int64) {
if topicId <= 0 {
return
}
db.Where("topic_id = ?", topicId).Delete(model.TopicTag{})
}