-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_get_drafts.go
56 lines (44 loc) · 1.11 KB
/
rest_get_drafts.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
package main
import (
"sort"
"knowlgraph.com/ent"
"knowlgraph.com/ent/content"
"knowlgraph.com/ent/draft"
"knowlgraph.com/ent/user"
)
// Drafts is user draft list
type Drafts []*ent.Draft
func (d Drafts) Len() int { return len(d) }
func (d Drafts) Less(i, j int) bool {
return d[i].Edges.Snapshots[0].CreatedAt.After(d[j].Edges.Snapshots[0].CreatedAt)
}
func (d Drafts) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
func getDrafts(c *Context) error {
_userID, _ := c.Get(GinKeyUserID)
_drafts, err := client.User.Query().
Where(user.ID(_userID.(int))).
QueryDrafts().
Where(draft.And(
draft.StateEQ(draft.StateWrite),
draft.HasSnapshots())).
WithSnapshots(func(cq *ent.ContentQuery) {
cq.Order(ent.Desc(content.FieldID))
}).
WithArticle().
All(ctx)
if err != nil {
return c.NotFound(err.Error())
}
for _, v := range _drafts {
_snapshots := make([]*ent.Content, 0)
if 0 < len(v.Edges.Snapshots) {
_snapshots = v.Edges.Snapshots[:1]
}
v.Edges.Snapshots = _snapshots
}
if 0 == len(_drafts) {
return c.NotFound("No more drafts")
}
sort.Sort(Drafts(_drafts))
return c.Ok(&_drafts)
}