This repository has been archived by the owner on Aug 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
pairs.go
68 lines (66 loc) · 1.76 KB
/
pairs.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
package search
import (
"encoding/json"
"github.com/jinzhu/gorm"
"github.com/notegio/openrelay/common"
dbModule "github.com/notegio/openrelay/db"
"net/http"
"strconv"
)
func PairHandler(db *gorm.DB) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
queryObject := r.URL.Query()
tokenAString := queryObject.Get("assetDataA")
tokenBString := queryObject.Get("assetDataB")
networkID, err := strconv.Atoi(queryObject.Get("networkId"))
if err != nil {
networkID = 1
}
if tokenAString == "" && tokenBString != "" {
tokenAString, tokenBString = tokenBString, ""
}
pageInt, perPageInt, err := getPages(queryObject)
offset := (pageInt - 1) * perPageInt
if err != nil {
returnError(w, err, 400)
return
}
var pairs []dbModule.Pair
var count int
if tokenAString == "" {
pairs, count, err = dbModule.GetAllTokenPairs(db, offset, perPageInt, networkID)
if err != nil {
returnError(w, err, 400)
return
}
} else {
assetDataA, err := common.HexToAssetData(tokenAString)
if err != nil {
returnError(w, err, 400)
return
}
if tokenBString == "" {
pairs, count, err = dbModule.GetTokenAPairs(db, assetDataA, offset, perPageInt, networkID)
} else {
assetDataB, err := common.HexToAssetData(tokenBString)
if err != nil {
returnError(w, err, 400)
return
}
pairs, count, err = dbModule.GetTokenABPairs(db, assetDataA, assetDataB, networkID)
}
if err != nil {
returnError(w, err, 400)
return
}
}
response, err := json.Marshal(GetPagedResult(count, pageInt, perPageInt, pairs))
if err != nil {
returnError(w, err, 500)
return
}
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
}