-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest_put_article_content.go
More file actions
51 lines (41 loc) · 1 KB
/
rest_put_article_content.go
File metadata and controls
51 lines (41 loc) · 1 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
package main
import (
"knowlgraph.com/ent/draft"
"knowlgraph.com/ent/user"
)
func putArticleContent(c *Context) error {
var _data struct {
Body string `json:"body"`
LastID int `json:"last_id"`
DraftID int `json:"draft_id" binding:"required"`
}
err := c.ShouldBindJSON(&_data)
if err != nil {
return c.BadRequest(err.Error())
}
_userID, _ := c.Get(GinKeyUserID)
_draft, err := client.User.Query().
Where(user.ID(_userID.(int))).
QueryDrafts().
Where(draft.ID(_data.DraftID)).
Only(ctx)
if err != nil {
return c.BadRequest(err.Error())
}
if _draft.State == draft.StateRead {
return c.Unauthorized("Cannot be modified because it is read-only")
}
_contentCreate := client.Content.Create()
// todo LastID 有何作用?
if 0 != _data.LastID {
_contentCreate.SetLastID(_data.LastID)
}
_content, err := _contentCreate.
SetBody(_data.Body).
SetBrancheID(_data.DraftID).
Save(ctx)
if err != nil {
return c.InternalServerError(err.Error())
}
return c.Ok(_content)
}