-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapis.go
More file actions
116 lines (91 loc) · 2.56 KB
/
apis.go
File metadata and controls
116 lines (91 loc) · 2.56 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"net/http"
"strconv"
"strings"
"sort"
"github.com/gin-gonic/gin"
)
var postIDMap = make(map[string][]comment)
var idMap = make(map[string]comment)
var nameMap = make(map[string][]comment)
var emailMap = make(map[string][]comment)
var bodyMap = make(map[string][]comment)
func indexComments() {
commentsRes, _ := client.R().
SetResult(&[]comment{}).
Get("https://jsonplaceholder.typicode.com/comments")
comments := commentsRes.Result().(*[]comment)
for _,c := range *comments {
idMap[strconv.Itoa(c.ID)] = c
postIDString := strconv.Itoa(c.PostID)
postIDMap[postIDString] = append(postIDMap[postIDString], c)
emailMap[c.Email] = append(emailMap[c.Email], c)
nameMap[c.Name] = append(nameMap[c.Name], c)
for _,name := range strings.Fields(c.Name) {
nameMap[name] = append(nameMap[name], c)
}
bodyMap[c.Body] = append(bodyMap[c.Body], c)
for _,body := range strings.Fields(c.Body) {
bodyMap[body] = append(bodyMap[body], c)
}
}
}
func getTopPosts(c *gin.Context) {
postsRes, _ := client.R().
SetResult(&[]post{}).
Get("https://jsonplaceholder.typicode.com/posts")
posts := postsRes.Result().(*[]post)
var postsDto []postDto
for _,p := range *posts {
postsDto = append(postsDto, postDto{
PostID: p.ID,
PostTitle: p.Title,
PostBody: p.Body,
NumberOfComments: len(postIDMap[strconv.Itoa(p.ID)]),
})
}
sort.Slice(postsDto, func(i, j int) bool {
return postsDto[i].NumberOfComments > postsDto[j].NumberOfComments
})
c.IndentedJSON(http.StatusOK, postsDto)
}
func search(c *gin.Context) {
query := c.Query("query")
result := []comment{}
if cmt,ok := idMap[query]; ok {
result = append(result, cmt)
}
if cmt, ok := postIDMap[query]; ok {
result = append(result, cmt...)
}
if cmt, ok := emailMap[query]; ok {
result = append(result, cmt...)
}
if cmt, ok := nameMap[query]; ok {
result = append(result, cmt...)
}
if cmt, ok := bodyMap[query]; ok {
result = append(result, cmt...)
}
if len(c.Query("pageNum")) > 0 &&
len(c.Query("pageSize")) > 0 {
pageNum,_ := strconv.Atoi(c.Query("pageNum"))
pageSize,_ := strconv.Atoi(c.Query("pageSize"))
start, end := paginate(pageNum, pageSize, len(result))
c.IndentedJSON(http.StatusOK, result[start:end])
} else {
c.IndentedJSON(http.StatusOK, result)
}
}
func paginate(pageNum int, pageSize int, sliceLength int) (int, int) {
start := pageNum * pageSize
if start > sliceLength {
start = sliceLength
}
end := start + pageSize
if end > sliceLength {
end = sliceLength
}
return start, end
}