forked from mtgban/mtgban-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_suggest.go
More file actions
56 lines (47 loc) · 1.22 KB
/
api_suggest.go
File metadata and controls
56 lines (47 loc) · 1.22 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
package main
import (
"encoding/json"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/mtgban/go-mtgban/mtgmatcher"
)
func SuggestAPI(w http.ResponseWriter, r *http.Request) {
sealed, _ := strconv.ParseBool(r.FormValue("sealed"))
AllNames := mtgmatcher.AllNames("canonical", sealed)
if r.FormValue("all") == "true" {
json.NewEncoder(w).Encode(&AllNames)
return
}
prefix := strings.ToLower(r.FormValue("q"))
if len(prefix) < 3 {
return
}
baseURL := getBaseURL(r)
AllLowerCaseNames := mtgmatcher.AllNames("lowercase", false)
var suggestions []string
var results []string
var links []string
for i, name := range AllLowerCaseNames {
if strings.HasPrefix(name, prefix) {
suggestions = append(suggestions, AllNames[i])
printings, _ := mtgmatcher.Printings4Card(name)
results = append(results, printings2line(printings))
links = append(links, baseURL+"/search?q="+url.QueryEscape(AllNames[i]))
}
}
// This argument is mandatory
if suggestions == nil {
suggestions = append(suggestions, "")
}
out := []any{}
out = append(out, prefix)
for _, tags := range [][]string{suggestions, results, links} {
if tags == nil {
break
}
out = append(out, tags)
}
json.NewEncoder(w).Encode(&out)
}