-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest_put_asset.go
More file actions
82 lines (67 loc) · 1.89 KB
/
Copy pathrest_put_asset.go
File metadata and controls
82 lines (67 loc) · 1.89 KB
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
package main
import (
"knowlgraph.com/ent/article"
"knowlgraph.com/ent/asset"
"knowlgraph.com/ent/user"
)
func putAsset(c *Context) error {
var _query struct {
ArticleID int `form:"articleId" binding:"required"`
VersionID int `form:"versionId"`
Status asset.Status `form:"status" binding:"required"`
}
err := c.ShouldBindQuery(&_query)
if err != nil {
return c.BadRequest(err.Error())
}
if asset.StatusSelf == _query.Status {
return c.Forbidden("Forbid adding articles to my list")
}
_article, err := client.Article.Get(ctx, _query.ArticleID)
if err != nil {
return c.NotFound(err.Error())
}
_userID, _ := c.Get(GinKeyUserID)
if _article.Status == article.StatusPrivate {
// 如果文章是私有的,则判断该文章是否为用户所有
ok, _ := client.Asset.
Query().
Where(
asset.HasArticleWith(article.ID(_query.ArticleID)),
asset.HasUserWith(user.ID(_userID.(int))),
asset.StatusEQ(asset.StatusSelf)).
Exist(ctx)
if !ok {
return c.Unauthorized("No access to private article")
}
}
if asset.StatusBrowse != _query.Status {
// 已关注或收藏的文章,无需再次关注或收藏
ok, _ := client.Asset.
Query().
Where(
asset.HasArticleWith(article.ID(_query.ArticleID)),
asset.HasUserWith(user.ID(_userID.(int))),
asset.StatusEQ(_query.Status)).
Exist(ctx)
if ok {
return c.NoContent()
}
}
_assetCreate := client.Asset.Create().SetArticle(_article).SetUserID(_userID.(int)).SetStatus(_query.Status)
if 0 < _query.VersionID {
_assetCreate.SetVersionID(_query.VersionID)
}
_asset, err := _assetCreate.Save(ctx)
// _asset, err := client.Asset.
// Create().
// SetArticle(_article).
// SetVersionID(_query.VersionID).
// SetUserID(_userID.(int)).
// SetStatus(_query.Status).
// Save(ctx)
if err != nil {
return c.InternalServerError(err.Error())
}
return c.Ok(_asset)
}