forked from eddycjy/go-gin-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
136 lines (114 loc) · 2.71 KB
/
article.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
package article_service
import (
"encoding/json"
"github.com/lnnujxxy/go-gin-example/models"
"github.com/lnnujxxy/go-gin-example/pkg/gredis"
"github.com/lnnujxxy/go-gin-example/pkg/logging"
"github.com/lnnujxxy/go-gin-example/service/cache_service"
)
type Article struct {
ID int
TagID int
Title string
Desc string
Content string
CoverImageUrl string
State int
CreatedBy string
ModifiedBy string
PageNum int
PageSize int
}
func (a *Article) Add() error {
article := map[string]interface{}{
"tag_id": a.TagID,
"title": a.Title,
"desc": a.Desc,
"content": a.Content,
"created_by": a.CreatedBy,
"cover_image_url": a.CoverImageUrl,
"state": a.State,
}
if err := models.AddArticle(article); err != nil {
return err
}
return nil
}
func (a *Article) Edit() error {
return models.EditArticle(a.ID, map[string]interface{}{
"tag_id": a.TagID,
"title": a.Title,
"desc": a.Desc,
"content": a.Content,
"cover_image_url": a.CoverImageUrl,
"state": a.State,
"modified_by": a.ModifiedBy,
})
}
func (a *Article) Get() (*models.Article, error) {
var cacheArticle *models.Article
cache := cache_service.Article{ID: a.ID}
key := cache.GetArticleKey()
if gredis.Exists(key) {
data, err := gredis.Get(key)
if err != nil {
logging.Info(err)
} else {
json.Unmarshal(data, &cacheArticle)
return cacheArticle, nil
}
}
article, err := models.GetArticle(a.ID)
if err != nil {
return nil, err
}
gredis.Set(key, article, 3600)
return article, nil
}
func (a *Article) GetAll() ([]*models.Article, error) {
var (
articles, cacheArticles []*models.Article
)
cache := cache_service.Article{
TagID: a.TagID,
State: a.State,
PageNum: a.PageNum,
PageSize: a.PageSize,
}
key := cache.GetArticlesKey()
if gredis.Exists(key) {
data, err := gredis.Get(key)
if err != nil {
logging.Info(err)
} else {
json.Unmarshal(data, &cacheArticles)
return cacheArticles, nil
}
}
articles, err := models.GetArticles(a.PageNum, a.PageSize, a.getMaps())
if err != nil {
return nil, err
}
gredis.Set(key, articles, 3600)
return articles, nil
}
func (a *Article) Delete() error {
return models.DeleteArticle(a.ID)
}
func (a *Article) ExistByID() (bool, error) {
return models.ExistArticleByID(a.ID)
}
func (a *Article) Count() (int, error) {
return models.GetArticleTotal(a.getMaps())
}
func (a *Article) getMaps() map[string]interface{} {
maps := make(map[string]interface{})
maps["deleted_on"] = 0
if a.State != -1 {
maps["state"] = a.State
}
if a.TagID != -1 {
maps["tag_id"] = a.TagID
}
return maps
}