-
Notifications
You must be signed in to change notification settings - Fork 1
/
indexRoutes.go
120 lines (107 loc) · 3 KB
/
indexRoutes.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"strings"
"github.com/gin-gonic/gin"
)
type QueryBody struct {
Query string `json:"query"`
Fields []string `json:"fields"`
BeginsWith bool `json:"beginsWith"`
}
type AddMultipleIndex struct {
Items []map[string]interface{} `json:"items"`
}
type DeleteDocumentRequestBody struct {
DocID uint64 `json:"docID"`
}
type UpdateDocumentRequestBody struct {
DocID uint64 `json:"docID"`
}
func HandleIndexRoutes(r *gin.Engine, app *appIndexes) {
r.GET("/index/listIndexes", func(c *gin.Context) {
list := app.listIndexes()
for listItem, _ := range list {
list[listItem].Name = strings.ReplaceAll(list[listItem].Name, "\\.", ".")
}
c.JSON(200, list)
})
r.POST("/index/search", func(c *gin.Context) {
// data, _ := ioutil.ReadAll(c.Request.Body)
// jDat, _ := parseArbJSON(string(data))
body := QueryBody{BeginsWith: false} // Default false if not included
c.BindJSON(&body)
var output []uint64 // temporary, will turn into documents later
// query := jDat["query"].(string)
query := body.Query
fields := body.Fields
bw := body.BeginsWith
var responseJSON SearchResponse
var res []uint64
if fields != nil { // Field(s) specified
res, responseJSON = app.search(query, fields, bw)
output = append(output, res...)
} else {
res, responseJSON = app.search(query, make([]string, 0), bw)
output = append(output, res...)
}
for responseItem, _ := range responseJSON.Items {
responseJSON.Items[responseItem].Data, _ = nestJSON(responseJSON.Items[responseItem].Data)
}
c.JSON(200, responseJSON)
})
r.POST("/index/add", func(c *gin.Context) {
data, _ := ioutil.ReadAll(c.Request.Body)
jDat, _ := parseArbJSON(string(data))
flatDat, _ := flattenJSON(jDat)
docID := app.addIndex(flatDat, false)
c.JSON(200, gin.H{
"msg": "Added Index",
"docID": docID,
})
})
r.POST("/index/addMultiple", func(c *gin.Context) {
data, _ := ioutil.ReadAll(c.Request.Body)
jDat := AddMultipleIndex{}
json.Unmarshal([]byte(data), &jDat)
docIDs := make([]uint64, 0)
for _, item := range jDat.Items {
docIDs = append(docIDs, app.addIndex(item, false))
}
c.JSON(200, gin.H{
"msg": "Added Indexes",
"docIDs": docIDs,
})
})
r.POST("/index/delete", func(c *gin.Context) {
body := DeleteDocumentRequestBody{}
c.BindJSON(&body)
err := app.deleteIndex(body.DocID)
if err != nil {
log.Printf("%v\n", err)
c.String(500, fmt.Sprintf("Internal Error: %v", err))
} else {
c.String(200, fmt.Sprintf("Deleted document %v", body.DocID))
}
})
r.POST("/index/update", func(c *gin.Context) {
d := json.NewDecoder(c.Request.Body)
d.UseNumber()
var jDat map[string]interface{}
if err := d.Decode(&jDat); err != nil {
log.Fatal(err)
}
err, created := app.updateIndex(jDat)
if err != nil {
log.Println("Error:", err)
c.String(500, "Internal Error, check logs")
} else if created {
c.String(200, "Created new Index")
} else {
c.String(200, "Updated Index")
}
})
}