-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest_get_draft.go
More file actions
61 lines (53 loc) · 1.39 KB
/
rest_get_draft.go
File metadata and controls
61 lines (53 loc) · 1.39 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
package main
import (
"knowlgraph.com/ent"
"knowlgraph.com/ent/content"
"knowlgraph.com/ent/draft"
"knowlgraph.com/ent/user"
)
func getDraft(c *Context) error {
var _query struct {
ID int `form:"id" binding:"required"`
NeedHistory bool `form:"needHistory"`
HistoryID int `form:"historyID"`
}
err := c.ShouldBindQuery(&_query)
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.And(draft.ID(_query.ID), draft.StateIn(draft.StateWrite))).
WithSnapshots(func(cq *ent.ContentQuery) {
if 0 < _query.HistoryID {
cq.Where(content.ID(_query.HistoryID))
} else if _query.NeedHistory {
cq.Order(ent.Desc(content.FieldCreatedAt))
} else {
cq.Order(ent.Desc(content.FieldCreatedAt)).Limit(1)
}
}).
WithArticle().
WithOriginal(func(vq *ent.VersionQuery) {
vq.WithContent().WithKeywords()
}).
Only(ctx)
if err != nil {
return c.NotFound(err.Error())
}
snapshots := _draft.Edges.Snapshots
if nil == snapshots {
snapshots = make([]*ent.Content, 0, 1)
}
if 0 == len(snapshots) {
if nil != _draft.Edges.Original {
snapshots = append(snapshots, _draft.Edges.Original.Edges.Content)
} else {
snapshots = append(snapshots, &ent.Content{Body: ""})
}
_draft.Edges.Snapshots = snapshots
}
return c.Ok(&_draft)
}