-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_get_article.go
130 lines (107 loc) · 2.69 KB
/
rest_get_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
package main
import (
"net/http"
"github.com/pkg/errors"
"knowlgraph.com/ent"
"knowlgraph.com/ent/archive"
"knowlgraph.com/ent/article"
"knowlgraph.com/ent/asset"
"knowlgraph.com/ent/node"
"knowlgraph.com/ent/user"
"knowlgraph.com/ent/version"
)
func getArticle(c *Context) error {
var _query struct {
ID int `form:"id" binding:"required"`
Lang string `form:"lang"`
VersionID int `form:"versionID"`
}
err := c.ShouldBindQuery(&_query)
if err != nil {
return c.BadRequest(err.Error())
}
_userID := c.GetInt(GinKeyUserID)
_article, status, err := GetArticle(
_userID,
_query.ID,
_query.VersionID)
switch status {
case http.StatusOK:
return c.Ok(&_article)
case http.StatusNotFound:
return c.NotFound(err.Error())
case http.StatusUnauthorized:
return c.Unauthorized(err.Error())
}
return c.NotFound(err.Error())
}
// GetArticle is get article
func GetArticle(_userID int, articleID int, versionID int) (*ent.Article, int, error) {
_article, err := client.Article.
Query().
Where(article.ID(articleID)).
First(ctx)
if err != nil {
return nil, http.StatusNotFound, err
}
isLogin := 0 < _userID
if _article.Status == article.StatusPrivate {
if !isLogin {
return nil, http.StatusUnauthorized, errors.New("Unauthorized")
}
ok, _ := _article.QueryAssets().Where(asset.HasUserWith(user.ID(_userID))).Exist(ctx)
if !ok {
return nil, http.StatusUnauthorized, errors.New("Unauthorized")
}
}
vq := _article.QueryVersions()
if 0 < versionID {
vq.Where(version.ID(versionID))
} else {
vq.Order(ent.Desc(version.FieldCreatedAt))
}
_version, err := vq.WithContent().WithKeywords().First(ctx)
if err != nil {
return nil, http.StatusNotFound, err
}
_reactions, _ := _article.QueryReactions().All(ctx)
_nodesWhere := node.StatusEQ(node.StatusPublic)
if isLogin {
// 已登录用户的私有节点查询
_nodesWhere = node.Or(
_nodesWhere,
node.HasArchivesWith(
archive.HasUserWith(
user.ID(_userID))))
}
_nodes, _ := _article.
QueryNodes().
Where(_nodesWhere).
WithWord().
WithPath(func(nq *ent.NodeQuery) {
nq.WithWord()
}).
All(ctx)
if isLogin {
_assets, _ := _article.
QueryAssets().
Where(
asset.HasUserWith(user.ID(_userID)),
asset.StatusNEQ(asset.StatusBrowse),
).
All(ctx)
_article.Edges.Assets = _assets
_archives, _ := _article.
QueryArchives().
Where(archive.HasUserWith(user.ID(_userID))).
WithNode(func(nq *ent.NodeQuery) {
nq.WithWord()
}).
All(ctx)
_article.Edges.Archives = _archives
}
_article.Edges.Versions = []*ent.Version{_version}
_article.Edges.Reactions = _reactions
_article.Edges.Nodes = _nodes
return _article, http.StatusOK, err
}