Commit 6c62f3f6 authored by Will Cory's avatar Will Cory

refactor: cleaner implementation

parent bee93aa3
...@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment