-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
139 lines (108 loc) · 3.32 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
137
138
139
package srv
import (
"errors"
"time"
"github.com/davidddw/gopj/gocms/app/dao"
"github.com/davidddw/gopj/gocms/app/domain"
)
type ArticleSrv struct {
}
func NewArticleSrv() *ArticleSrv {
return &ArticleSrv{}
}
func (this *ArticleSrv) Add(article *domain.Article) (err error) {
articleTypeDao := dao.NewArticleTypeDao()
t, err := articleTypeDao.Get(article.Type)
if t == nil || err != nil {
err = errors.New("this type is not exists:" + article.Type)
return
}
// if the article is exists, return
articleDao := dao.NewArticleDao()
_, err = articleDao.Get(article.Type, article.Name, "")
if err == nil {
err = errors.New("this article is already exists:" + article.Name)
return
}
article.CreatedAt = time.Now()
article.Hits = 0
err = articleDao.Insert("", article)
return
}
func (this *ArticleSrv) AddChapter(article string, chapter *domain.Article) (err error) {
articleDao := dao.NewArticleDao()
// check the article
_, err = articleDao.Get(chapter.Type, article, "")
if err != nil {
err = errors.New("the article is not exists:" + article)
return
}
// check the chapter
_, err = articleDao.Get(chapter.Type, article, chapter.Name)
if err == nil {
err = errors.New("the article is exists:" + chapter.Name)
return
}
chapter.Type = ""
chapter.CreatedAt = time.Now()
chapter.Hits = 0
err = articleDao.Insert(article, chapter)
return
}
func (this *ArticleSrv) Get(ty, article, chapter string) (a *domain.Article, err error) {
articleDao := dao.NewArticleDao()
// check the article is exists
a, err = articleDao.Get(ty, article, chapter)
if err != nil {
err = errors.New("the article is not exists")
return
}
return
}
func (this *ArticleSrv) Edit(oldType, oldName string, article *domain.Article) (err error) {
articleTypeDao := dao.NewArticleTypeDao()
t, err := articleTypeDao.Get(article.Type)
if t == nil || err != nil {
err = errors.New("this type is not exists:" + article.Type)
return
}
// if the article is exists, return
articleDao := dao.NewArticleDao()
if oldName != article.Name {
_, err = articleDao.Get(article.Type, article.Name, "")
if err == nil {
err = errors.New("this article is already exists:" + article.Name)
return
}
}
err = articleDao.UpdateArticle(oldType, oldName, article)
return
}
func (this *ArticleSrv) EditChapter(oldType, oldName, oldChapter string, chapter *domain.Article) (err error) {
articleDao := dao.NewArticleDao()
// check the article
_, err = articleDao.Get(oldType, oldName, oldChapter)
if err != nil {
err = errors.New("the article is not exists:" + oldChapter)
return
}
// check the chapter
if oldChapter != chapter.Name {
_, err = articleDao.Get(oldType, oldName, chapter.Name)
if err == nil {
err = errors.New("the chapter is exists:" + chapter.Name)
return
}
}
chapter.Type = ""
err = articleDao.UpdateChapter(oldType, oldName, oldChapter, chapter)
return
}
func (this *ArticleSrv) Delete(typeName, articleName string) (err error) {
dao.NewArticleDao().Delete(typeName, articleName)
return
}
func (this *ArticleSrv) DeleteChapter(typeName, articleName, chapterName string) (err error) {
err = dao.NewArticleDao().DeleteChapter(typeName, articleName, chapterName)
return
}