-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_put_quote.go
67 lines (61 loc) · 1.61 KB
/
rest_put_quote.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
package main
import (
"knowlgraph.com/ent/article"
"knowlgraph.com/ent/asset"
"knowlgraph.com/ent/user"
)
func putQuote(c *Context) error {
var _query struct {
Text string `json:"text"`
Context string `json:"context"`
Highlight int `json:"highlight"`
ArticleID int `json:"articleId" binding:"required"`
VersionID int `json:"versionId"`
ResponseID int `json:"responseId" binding:"required"`
}
err := c.ShouldBindJSON(&_query)
if err != nil {
return c.BadRequest(err.Error())
}
_userID, _ := c.Get(GinKeyUserID)
ok, err := client.Article.Query().
Where(
article.And(
article.And(
article.ID(_query.ResponseID),
article.Or(
article.StatusNEQ(article.StatusPrivate),
article.And(
article.StatusEQ(article.StatusPrivate),
article.HasAssetsWith(asset.HasUserWith(user.ID(_userID.(int)))),
)),
),
article.And(
article.ID(_query.ArticleID),
article.Or(
article.StatusNEQ(article.StatusPrivate),
article.And(
article.StatusEQ(article.StatusPrivate),
article.HasAssetsWith(asset.HasUserWith(user.ID(_userID.(int)))),
)),
),
),
).Exist(ctx)
if err != nil {
return c.InternalServerError(err.Error())
} else if !ok {
return c.Unauthorized("Unauthorized")
}
_quote, err := client.Quote.Create().
SetText(_query.Text).
SetContext(_query.Context).
SetHighlight(_query.Highlight).
SetArticleID(_query.ArticleID).
SetVersionID(_query.VersionID).
SetResponseID(_query.ResponseID).
Save(ctx)
if err != nil {
return c.InternalServerError(err.Error())
}
return c.Ok(_quote.ID)
}