-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_put_reaction.go
70 lines (58 loc) · 1.31 KB
/
rest_put_reaction.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
package main
import (
"knowlgraph.com/ent/article"
"knowlgraph.com/ent/asset"
"knowlgraph.com/ent/reaction"
"knowlgraph.com/ent/user"
)
func putReaction(c *Context) error {
var _query struct {
ArticleID int `form:"articleId" binding:"required"`
Reaction reaction.Status `form:"reaction" binding:"required"`
}
err := c.ShouldBindQuery(&_query)
if err != nil {
return c.BadRequest(err.Error())
}
_userID, ok := c.Get(GinKeyUserID)
_where := article.StatusNEQ(article.StatusPrivate)
if ok {
_where = article.Or(
_where,
article.HasAssetsWith(
asset.HasUserWith(user.ID(_userID.(int)))))
}
ok, err = client.Article.
Query().
Where(_where).
Exist(ctx)
if err != nil {
return c.InternalServerError(err.Error())
}
if !ok {
return c.NotFound("NotFound")
}
_reaction, err := client.Reaction.
Query().
Where(
reaction.StatusEQ(_query.Reaction),
reaction.HasArticleWith(article.ID(_query.ArticleID))).
Only(ctx)
if err != nil {
_reaction, err = client.Reaction.
Create().
SetArticleID(_query.ArticleID).
SetStatus(_query.Reaction).
SetCount(1).
Save(ctx)
} else {
_reaction, err = client.Reaction.
UpdateOne(_reaction).
AddCount(1).
Save(ctx)
}
if err != nil {
return c.InternalServerError(err.Error())
}
return c.Ok(_reaction)
}