/
patch_tx.go
70 lines (66 loc) · 2.34 KB
/
patch_tx.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
package handlers
import (
"net/http"
"time"
"github.com/golangly/errors"
"github.com/golangly/webutil"
"github.com/neo4j/neo4j-go-driver/neo4j"
)
type (
PatchTransactionRequest struct {
ID string `path:"ID,required"`
Body struct {
IssuedOn *time.Time `json:"issuedOn" yaml:"issuedOn"`
Origin *string `json:"origin" yaml:"origin"`
Amount *float64 `json:"amount" yaml:"amount"`
Comment *string `json:"comment" yaml:"comment"`
} `body:""`
}
PatchTransactionResponse struct {
Transaction Transaction `json:"data"`
}
)
func PatchTransaction() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
req := PatchTransactionRequest{}
if err := webutil.Bind(r, &req); err != nil {
webutil.RenderWithStatusCode(w, r, http.StatusInternalServerError, err)
} else {
PatchNode(w, r, patchTxQuery, map[string]interface{}{
"id": req.ID,
"issuedOn": req.Body.IssuedOn,
"origin": req.Body.Origin,
"amount": req.Body.Amount,
"comment": req.Body.Comment,
}, func(rec neo4j.Record) (interface{}, error) {
if src, ok := rec.Get("src"); !ok {
return nil, errors.Wrap(err, "src not found in results")
} else if srcNode, ok := src.(neo4j.Node); !ok {
return nil, errors.Wrap(err, "src node mismatch")
} else if tx, ok := rec.Get("tx"); !ok {
return nil, errors.Wrap(err, "tx not found in results")
} else if txNode, ok := tx.(neo4j.Node); !ok {
return nil, errors.Wrap(err, "tx node mismatch")
} else if dst, ok := rec.Get("dst"); !ok {
return nil, errors.Wrap(err, "dst not found in results")
} else if dstNode, ok := dst.(neo4j.Node); !ok {
return nil, errors.Wrap(err, "dst node mismatch")
} else {
return PatchTransactionResponse{
Transaction: Transaction{
ID: txNode.Props()["id"].(string),
CreatedOn: txNode.Props()["createdOn"].(time.Time),
UpdatedOn: txNode.Props()["updatedOn"].(*time.Time),
IssuedOn: txNode.Props()["issuedOn"].(time.Time),
Origin: txNode.Props()["origin"].(string),
SourceAccountID: srcNode.Props()["id"].(string),
TargetAccountID: dstNode.Props()["id"].(string),
Amount: txNode.Props()["amount"].(float64),
Comment: txNode.Props()["comment"].(string),
},
}, nil
}
})
}
}
}