mirror of
https://github.com/dancojocaru2000/foxbank.git
synced 2025-02-22 23:39:36 +02:00
Added GET /accounts endpoint
This commit is contained in:
parent
f19aad8d3e
commit
9ded9cc604
1 changed files with 37 additions and 18 deletions
|
@ -1,4 +1,5 @@
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
from flask.views import MethodView
|
||||||
from flask_smorest import Blueprint, abort
|
from flask_smorest import Blueprint, abort
|
||||||
from marshmallow import Schema, fields
|
from marshmallow import Schema, fields
|
||||||
from ..decorators import ensure_logged_in
|
from ..decorators import ensure_logged_in
|
||||||
|
@ -34,6 +35,7 @@ def get_valid_account_types():
|
||||||
|
|
||||||
@bp.get('/<int:account_id>')
|
@bp.get('/<int:account_id>')
|
||||||
@ensure_logged_in
|
@ensure_logged_in
|
||||||
|
@bp.response(401, returns.ErrorSchema, description='Login failure')
|
||||||
@bp.doc(security=[{'Token': []}])
|
@bp.doc(security=[{'Token': []}])
|
||||||
def get_account_id(account_id: int):
|
def get_account_id(account_id: int):
|
||||||
account = db_utils.get_account(account_id=account_id)
|
account = db_utils.get_account(account_id=account_id)
|
||||||
|
@ -47,6 +49,7 @@ def get_account_id(account_id: int):
|
||||||
|
|
||||||
@bp.get('/IBAN_<iban>')
|
@bp.get('/IBAN_<iban>')
|
||||||
@ensure_logged_in
|
@ensure_logged_in
|
||||||
|
@bp.response(401, returns.ErrorSchema, description='Login failure')
|
||||||
@bp.doc(security=[{'Token': []}])
|
@bp.doc(security=[{'Token': []}])
|
||||||
def get_account_iban(iban: str):
|
def get_account_iban(iban: str):
|
||||||
account = db_utils.get_account(iban=iban)
|
account = db_utils.get_account(iban=iban)
|
||||||
|
@ -58,25 +61,41 @@ def get_account_iban(iban: str):
|
||||||
return returns.success(account=account)
|
return returns.success(account=account)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route('/')
|
||||||
|
class AccountsList(MethodView):
|
||||||
|
class CreateAccountParams(Schema):
|
||||||
|
currency = fields.String()
|
||||||
|
account_type = fields.String(data_key='accountType')
|
||||||
|
custom_name = fields.String(data_key='customName')
|
||||||
|
|
||||||
class CreateAccountParams(Schema):
|
class CreateAccountResponseSchema(returns.SuccessSchema):
|
||||||
currency = fields.String()
|
account = fields.Nested(Account.Schema)
|
||||||
account_type = fields.String(data_key='accountType')
|
|
||||||
custom_name = fields.String(data_key='customName')
|
|
||||||
|
|
||||||
@bp.post('/')
|
@ensure_logged_in
|
||||||
@ensure_logged_in
|
@bp.response(401, returns.ErrorSchema, description='Login failure')
|
||||||
@bp.arguments(CreateAccountParams, as_kwargs=True)
|
@bp.doc(security=[{'Token': []}])
|
||||||
@bp.response(200, Account.Schema)
|
@bp.arguments(CreateAccountParams, as_kwargs=True)
|
||||||
@bp.response(HTTPStatus.UNPROCESSABLE_ENTITY, description='Invalid currency or account type')
|
@bp.response(200, CreateAccountResponseSchema)
|
||||||
@bp.doc(security=[{'Token': []}])
|
@bp.response(HTTPStatus.UNPROCESSABLE_ENTITY, description='Invalid currency or account type')
|
||||||
def create_account(currency: str, account_type: str, custom_name: str):
|
def post(self, currency: str, account_type: str, custom_name: str):
|
||||||
if currency not in VALID_CURRENCIES:
|
"""Create account"""
|
||||||
abort(HTTPStatus.UNPROCESSABLE_ENTITY)
|
if currency not in VALID_CURRENCIES:
|
||||||
if account_type not in ACCOUNT_TYPES:
|
abort(HTTPStatus.UNPROCESSABLE_ENTITY)
|
||||||
abort(HTTPStatus.UNPROCESSABLE_ENTITY)
|
if account_type not in ACCOUNT_TYPES:
|
||||||
|
abort(HTTPStatus.UNPROCESSABLE_ENTITY)
|
||||||
|
|
||||||
account = Account(-1, '', currency, account_type, custom_name or '')
|
account = Account(-1, '', currency, account_type, custom_name or '')
|
||||||
db_utils.insert_account(decorators.user_id, account)
|
db_utils.insert_account(decorators.user_id, account)
|
||||||
return account.to_json()
|
return returns.success(account=account.to_json())
|
||||||
|
|
||||||
|
class AccountsResponseSchema(returns.SuccessSchema):
|
||||||
|
accounts = fields.List(fields.Nested(Account.Schema))
|
||||||
|
|
||||||
|
@ensure_logged_in
|
||||||
|
@bp.response(401, returns.ErrorSchema, description='Login failure')
|
||||||
|
@bp.doc(security=[{'Token': []}])
|
||||||
|
@bp.response(200, AccountsResponseSchema)
|
||||||
|
def get(self):
|
||||||
|
"""Get all accounts of user"""
|
||||||
|
return returns.success(accounts=db_utils.get_accounts(decorators.user_id))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue