-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_single_reference.go
54 lines (44 loc) · 1.06 KB
/
actions_single_reference.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
package horizon
import (
"gitlab.com/tokend/horizon/db2/core"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
)
type SingleReferenceAction struct {
Action
accountID string
reference string
record core.Reference
}
func (action *SingleReferenceAction) JSON() {
action.Do(
action.loadParams,
action.loadRecords,
func() {
response := map[string]interface{}{
"data": action.record,
}
hal.Render(action.W, response)
},
)
}
func (action *SingleReferenceAction) loadParams() {
action.accountID = action.GetNonEmptyString("account_id")
action.reference = action.GetNonEmptyString("reference")
}
func (action *SingleReferenceAction) loadRecords() {
q := action.CoreQ().References().
ForAccount(action.accountID).
ByReference(action.reference)
records, err := q.Select()
if err != nil {
action.Log.WithError(err).Error("Failed to get References from core DB")
action.Err = &problem.ServerError
return
}
if len(records) == 0 {
action.Err = &problem.NotFound
return
}
action.record = records[0]
}