-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
executable file
·86 lines (70 loc) · 2.59 KB
/
query.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 rest
import (
"encoding/json"
"fmt"
"github.com/commitHub/commitBlockchain/types"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
negotiationTypes "github.com/commitHub/commitBlockchain/modules/negotiation/internal/types"
)
func QueryNegotiationRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx := cliCtx
vars := mux.Vars(r)
negotiationID, err := negotiationTypes.GetNegotiationIDFromString(vars["negotiation-id"])
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
fmt.Sprintf("cannot decode NegotiationID. Error: %s", err.Error()))
return
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s/%s", negotiationTypes.QuerierRoute, "queryNegotiation", negotiationID), nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError,
fmt.Sprintf("couldn't query Negotiation. Error: %s", err.Error()))
return
}
if len(res) == 0 {
w.WriteHeader(http.StatusNoContent)
return
}
var _negotiation negotiationTypes.Negotiation
cliCtx.Codec.MustUnmarshalJSON(res, &_negotiation)
rest.PostProcessResponse(w, cliCtx, _negotiation)
}
}
func GetNegotiationIDHandlerFn() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
from := vars["buyerAddress"]
to := vars["sellerAddress"]
pegHash := vars["pegHash"]
buyerAddress, _, err := context.GetFromFields(from, false)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't query Negotiation. Error: %s", err.Error()))
return
}
sellerAddress, _, err := context.GetFromFields(to, false)
fmt.Println(sellerAddress)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't query Negotiation. Error: %s", err.Error()))
return
}
pegHashHex, err := types.GetAssetPegHashHex(pegHash)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("couldn't query Negotiation. Error: %s", err.Error()))
return
}
negotiation := negotiationTypes.NegotiationID(append(append(buyerAddress.Bytes(), sellerAddress.Bytes()...), pegHashHex.Bytes()...))
output, err := json.Marshal(struct {
NegotiationID string `json:"negotiationID"`
}{NegotiationID: negotiation.String()})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, _ = w.Write(output)
}
}