Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
6c62f3f6
Commit
6c62f3f6
authored
May 31, 2023
by
Will Cory
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: cleaner implementation
parent
bee93aa3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
43 deletions
+45
-43
api.go
indexer/api/api.go
+45
-43
No files found.
indexer/api/api.go
View file @
6c62f3f6
...
@@ -20,6 +20,7 @@ type WithdrawalDB interface {
...
@@ -20,6 +20,7 @@ type WithdrawalDB interface {
}
}
// Deposit data structure
// Deposit data structure
// TODO this should be coming from the ORM instead
type
Deposit
struct
{
type
Deposit
struct
{
Guid
string
`json:"guid"`
Guid
string
`json:"guid"`
Amount
string
`json:"amount"`
Amount
string
`json:"amount"`
...
@@ -33,6 +34,7 @@ type Deposit struct {
...
@@ -33,6 +34,7 @@ type Deposit struct {
}
}
// Withdrawal data structure
// Withdrawal data structure
// TODO this should be coming from teh ORM instead
type
Withdrawal
struct
{
type
Withdrawal
struct
{
Guid
string
`json:"guid"`
Guid
string
`json:"guid"`
Amount
string
`json:"amount"`
Amount
string
`json:"amount"`
...
@@ -80,49 +82,46 @@ type PaginationResponse struct {
...
@@ -80,49 +82,46 @@ type PaginationResponse struct {
HasNextPage
bool
`json:"hasNextPage"`
HasNextPage
bool
`json:"hasNextPage"`
}
}
func
getDepositsHandler
(
depositDB
DepositDB
)
http
.
HandlerFunc
{
func
(
a
*
Api
)
DepositsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
limit
:=
getIntFromQuery
(
r
,
"limit"
,
10
)
limit
:=
getIntFromQuery
(
r
,
"limit"
,
10
)
cursor
:=
r
.
URL
.
Query
()
.
Get
(
"cursor"
)
cursor
:=
r
.
URL
.
Query
()
.
Get
(
"cursor"
)
sortDirection
:=
r
.
URL
.
Query
()
.
Get
(
"sortDirection"
)
sortDirection
:=
r
.
URL
.
Query
()
.
Get
(
"sortDirection"
)
deposits
,
nextCursor
,
hasNextPage
,
err
:=
depositDB
.
GetDeposits
(
limit
,
cursor
,
sortDirection
)
deposits
,
nextCursor
,
hasNextPage
,
err
:=
a
.
DepositDB
.
GetDeposits
(
limit
,
cursor
,
sortDirection
)
if
err
!=
nil
{
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
return
}
}
response
:=
PaginationResponse
{
response
:=
PaginationResponse
{
Data
:
deposits
,
Data
:
deposits
,
Cursor
:
nextCursor
,
Cursor
:
nextCursor
,
HasNextPage
:
hasNextPage
,
HasNextPage
:
hasNextPage
,
}
jsonResponse
(
w
,
response
,
http
.
StatusOK
)
}
}
jsonResponse
(
w
,
response
,
http
.
StatusOK
)
}
}
func
getWithdrawalsHandler
(
withdrawalDB
WithdrawalDB
)
http
.
HandlerFunc
{
func
(
a
*
Api
)
WithdrawalsHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
limit
:=
getIntFromQuery
(
r
,
"limit"
,
10
)
limit
:=
getIntFromQuery
(
r
,
"limit"
,
10
)
cursor
:=
r
.
URL
.
Query
()
.
Get
(
"cursor"
)
cursor
:=
r
.
URL
.
Query
()
.
Get
(
"cursor"
)
sortDirection
:=
r
.
URL
.
Query
()
.
Get
(
"sortDirection"
)
sortDirection
:=
r
.
URL
.
Query
()
.
Get
(
"sortDirection"
)
sortBy
:=
r
.
URL
.
Query
()
.
Get
(
"sortBy"
)
sortBy
:=
r
.
URL
.
Query
()
.
Get
(
"sortBy"
)
withdrawals
,
nextCursor
,
hasNextPage
,
err
:=
a
.
WithdrawalDB
.
GetWithdrawals
(
limit
,
cursor
,
sortDirection
,
sortBy
)
withdrawals
,
nextCursor
,
hasNextPage
,
err
:=
withdrawalDB
.
GetWithdrawals
(
limit
,
cursor
,
sortDirection
,
sortBy
)
if
err
!=
nil
{
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
return
}
response
:=
PaginationResponse
{
Data
:
withdrawals
,
Cursor
:
nextCursor
,
HasNextPage
:
hasNextPage
,
}
jsonResponse
(
w
,
response
,
http
.
StatusOK
)
}
}
response
:=
PaginationResponse
{
Data
:
withdrawals
,
Cursor
:
nextCursor
,
HasNextPage
:
hasNextPage
,
}
jsonResponse
(
w
,
response
,
http
.
StatusOK
)
}
}
func
getIntFromQuery
(
r
*
http
.
Request
,
key
string
,
defaultValue
int
)
int
{
func
getIntFromQuery
(
r
*
http
.
Request
,
key
string
,
defaultValue
int
)
int
{
...
@@ -152,14 +151,17 @@ type Api struct {
...
@@ -152,14 +151,17 @@ type Api struct {
func
NewApi
(
depositDB
DepositDB
,
withdrawalDB
WithdrawalDB
)
*
Api
{
func
NewApi
(
depositDB
DepositDB
,
withdrawalDB
WithdrawalDB
)
*
Api
{
r
:=
chi
.
NewRouter
()
r
:=
chi
.
NewRouter
()
r
.
Get
(
"/api/v0/deposits"
,
getDepositsHandler
(
depositDB
))
api
:=
&
Api
{
r
.
Get
(
"/api/v0/withdrawals"
,
getWithdrawalsHandler
(
withdrawalDB
))
return
&
Api
{
Router
:
r
,
Router
:
r
,
DepositDB
:
depositDB
,
DepositDB
:
depositDB
,
WithdrawalDB
:
withdrawalDB
,
WithdrawalDB
:
withdrawalDB
,
}
}
r
.
Get
(
"/api/v0/deposits"
,
api
.
DepositsHandler
)
r
.
Get
(
"/api/v0/withdrawals"
,
api
.
WithdrawalsHandler
)
return
api
}
}
func
(
a
*
Api
)
Listen
(
port
string
)
{
func
(
a
*
Api
)
Listen
(
port
string
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment