-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_get_words.go
62 lines (51 loc) · 1.28 KB
/
rest_get_words.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
package main
import (
"knowlgraph.com/ent"
"knowlgraph.com/ent/user"
"knowlgraph.com/ent/word"
)
func getKeywords(c *Context) error {
_status := c.Query("status")
_userID, ok := c.Get(GinKeyUserID)
if _status == word.StatusPrivate.String() && !ok {
return c.Unauthorized("Unauthorized")
}
_publicCreate := client.Word.Query().Where(word.StatusEQ(word.StatusPublic))
var _privateCreate *ent.WordQuery
if ok {
_privateCreate = client.User.Query().
Where(user.IDEQ(_userID.(int))).
QueryDict().
QueryWord()
}
var _words []*ent.Word
var err error
switch _status {
case word.StatusPrivate.String():
_words, err = _privateCreate.All(ctx)
case word.StatusPublic.String():
_words, err = _publicCreate.All(ctx)
default:
if _words, err = _publicCreate.All(ctx); err == nil && ok {
if _privateWords, err1 := _privateCreate.All(ctx); err1 == nil {
_words = mergeArrAndUnique(_words, _privateWords)
}
}
}
if err != nil {
return c.NotFound(err.Error())
}
return c.Ok(_words)
}
func mergeArrAndUnique(a []*ent.Word, b []*ent.Word) []*ent.Word {
check := make(map[int]*ent.Word)
d := append(a, b...)
res := make([]*ent.Word, 0)
for _, val := range d {
check[val.ID] = val
}
for letter := range check {
res = append(res, check[letter])
}
return res
}