-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.go
94 lines (76 loc) · 1.57 KB
/
repository.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"encoding/json"
"io"
"net/url"
"strconv"
"strings"
)
func GetAll(
params url.Values,
collection *[]interface{},
) []interface{} {
result := []interface{}{}
limit, _ := strconv.Atoi(params.Get("_limit"))
if len(params) > 0 {
count := 0
for _, item := range *collection {
hasLimitReached := limit > 0 && count >= limit
if hasLimitReached {
break
}
shouldAdd := true
item := item.(map[string]interface{})
for key, value := range params {
if strings.HasPrefix(key, "_") {
continue
}
if intValue, err := strconv.Atoi(value[0]); err == nil {
if item[key] != intValue {
shouldAdd = false
}
} else if boolValue, err := strconv.ParseBool(value[0]); err == nil {
if item[key] != boolValue {
shouldAdd = false
}
} else {
if item[key] != value[0] {
shouldAdd = false
}
}
}
if shouldAdd {
result = append(result, item)
}
count += 1
}
} else {
result = *collection
}
return result
}
func Create(
payload io.Reader,
collection *[]interface{},
) error {
var body map[string]interface{}
if err := json.NewDecoder(payload).Decode(&body); err != nil {
return err
}
*collection = append(*collection, body)
return nil
}
func Delete(
id string,
collection *[]interface{},
) []interface{} {
parsedId, _ := strconv.Atoi(id)
for index, item := range *collection {
item := item.(map[string]interface{})
if item["id"] == parsedId {
*collection = append((*collection)[:index], (*collection)[index+1:]...)
break
}
}
return *collection
}