This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
/
routes_lens.go
86 lines (78 loc) · 2.11 KB
/
routes_lens.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
package v2
import (
"errors"
"net/http"
"github.com/RTradeLtd/Temporal/eh"
pb "github.com/RTradeLtd/grpc/lensv2"
"github.com/gin-gonic/gin"
gocid "github.com/ipfs/go-cid"
)
func (api *API) submitIndexRequest(c *gin.Context) {
// extract post forms
forms, missingField := api.extractPostForms(c, "object_type", "object_identifier")
if missingField != "" {
FailWithMissingField(c, missingField)
return
}
var indexType pb.IndexReq_Type
// ensure the type being requested is supported
switch forms["object_type"] {
case "ipld":
// validate the object identifier
if _, err := gocid.Decode(forms["object_identifier"]); err != nil {
Fail(c, err)
return
}
indexType = pb.IndexReq_IPLD
default:
Fail(c, errors.New(eh.InvalidObjectTypeError))
return
}
resp, err := api.lens.Index(c, &pb.IndexReq{
Type: indexType,
Hash: forms["object_identifier"],
Options: &pb.IndexReq_Options{
Reindex: c.PostForm("reindex") == "true",
},
})
if err != nil {
api.LogError(c, err, eh.FailedToIndexError)(http.StatusBadRequest)
return
}
Respond(c, http.StatusOK, gin.H{"response": resp.GetDoc()})
}
func (api *API) submitSearchRequest(c *gin.Context) {
// query we will use to perform as the main search
query, exists := c.GetPostForm("query")
if !exists {
FailWithMissingField(c, "query")
return
}
var (
tags, _ = c.GetPostFormArray("tags")
categories, _ = c.GetPostFormArray("categories")
mimeTypes, _ = c.GetPostFormArray("mime_types")
hashes, _ = c.GetPostFormArray("hashes")
required, _ = c.GetPostFormArray("required")
)
resp, err := api.lens.Search(c, &pb.SearchReq{
Query: query,
Options: &pb.SearchReq_Options{
Tags: tags,
Categories: categories,
MimeTypes: mimeTypes,
Hashes: hashes,
Required: required,
},
})
if err != nil {
api.LogError(c, err, eh.FailedToSearchError)(http.StatusBadRequest)
return
}
// check to ensure some objects were found, otherwise log a warning
if len(resp.Results) == 0 {
Respond(c, http.StatusBadRequest, gin.H{"response": "no results found"})
return
}
Respond(c, http.StatusOK, gin.H{"response": resp})
}