-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: accounts list Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> * fix: add required fields to OAS Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com> Signed-off-by: Lawrence Zawila <113581282+darkmatterpool@users.noreply.github.com>
- Loading branch information
1 parent
0c89897
commit dea61f0
Showing
5 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/formancehq/payments/internal/app/models" | ||
"github.com/formancehq/payments/internal/app/storage" | ||
|
||
"github.com/formancehq/go-libs/sharedapi" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type listAccountsRepository interface { | ||
ListAccounts(ctx context.Context, sort storage.Sorter, pagination storage.Paginator) ([]*models.Account, error) | ||
} | ||
|
||
type accountResponse struct { | ||
ID string `json:"id"` | ||
Reference string `json:"reference"` | ||
CreatedAt time.Time `json:"createdAt"` | ||
Provider string `json:"provider"` | ||
Type models.AccountType `json:"type"` | ||
} | ||
|
||
func listAccountsHandler(repo listAccountsRepository) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
|
||
var sorter storage.Sorter | ||
|
||
if sortParams := r.URL.Query()["sort"]; sortParams != nil { | ||
for _, s := range sortParams { | ||
parts := strings.SplitN(s, ":", 2) | ||
|
||
var order storage.SortOrder | ||
|
||
if len(parts) > 1 { | ||
//nolint:goconst // allow duplicate string | ||
switch parts[1] { | ||
case "asc", "ASC": | ||
order = storage.SortOrderAsc | ||
case "dsc", "desc", "DSC", "DESC": | ||
order = storage.SortOrderDesc | ||
default: | ||
handleValidationError(w, r, errors.New("sort order not well specified, got "+parts[1])) | ||
|
||
return | ||
} | ||
} | ||
|
||
column := parts[0] | ||
|
||
sorter.Add(column, order) | ||
} | ||
} | ||
|
||
skip, err := integerWithDefault(r, "skip", 0) | ||
if err != nil { | ||
handleValidationError(w, r, err) | ||
|
||
return | ||
} | ||
|
||
limit, err := integerWithDefault(r, "limit", maxPerPage) | ||
if err != nil { | ||
handleValidationError(w, r, err) | ||
|
||
return | ||
} | ||
|
||
if limit > maxPerPage { | ||
limit = maxPerPage | ||
} | ||
|
||
ret, err := repo.ListAccounts(r.Context(), sorter, storage.Paginate(skip, limit)) | ||
if err != nil { | ||
handleServerError(w, r, err) | ||
|
||
return | ||
} | ||
|
||
data := make([]*accountResponse, len(ret)) | ||
|
||
for i := range ret { | ||
data[i] = &accountResponse{ | ||
ID: ret[i].ID.String(), | ||
Reference: ret[i].Reference, | ||
CreatedAt: ret[i].CreatedAt, | ||
Provider: ret[i].Provider, | ||
Type: ret[i].Type, | ||
} | ||
} | ||
|
||
err = json.NewEncoder(w).Encode(sharedapi.BaseResponse[[]*accountResponse]{ | ||
Data: &data, | ||
}) | ||
if err != nil { | ||
handleServerError(w, r, err) | ||
|
||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters