Browse Source

Added GET /accounts endpoint

pull/5/head
Kenneth Bruen 3 years ago
parent
commit
9ded9cc604
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 61
      server/foxbank_server/apis/accounts.py

61
server/foxbank_server/apis/accounts.py

@ -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 CreateAccountParams(Schema): class AccountsList(MethodView):
currency = fields.String() class CreateAccountParams(Schema):
account_type = fields.String(data_key='accountType') currency = fields.String()
custom_name = fields.String(data_key='customName') account_type = fields.String(data_key='accountType')
custom_name = fields.String(data_key='customName')
@bp.post('/')
@ensure_logged_in class CreateAccountResponseSchema(returns.SuccessSchema):
@bp.arguments(CreateAccountParams, as_kwargs=True) account = fields.Nested(Account.Schema)
@bp.response(200, Account.Schema)
@bp.response(HTTPStatus.UNPROCESSABLE_ENTITY, description='Invalid currency or account type') @ensure_logged_in
@bp.doc(security=[{'Token': []}]) @bp.response(401, returns.ErrorSchema, description='Login failure')
def create_account(currency: str, account_type: str, custom_name: str): @bp.doc(security=[{'Token': []}])
if currency not in VALID_CURRENCIES: @bp.arguments(CreateAccountParams, as_kwargs=True)
abort(HTTPStatus.UNPROCESSABLE_ENTITY) @bp.response(200, CreateAccountResponseSchema)
if account_type not in ACCOUNT_TYPES: @bp.response(HTTPStatus.UNPROCESSABLE_ENTITY, description='Invalid currency or account type')
abort(HTTPStatus.UNPROCESSABLE_ENTITY) def post(self, currency: str, account_type: str, custom_name: str):
"""Create account"""
account = Account(-1, '', currency, account_type, custom_name or '') if currency not in VALID_CURRENCIES:
db_utils.insert_account(decorators.user_id, account) abort(HTTPStatus.UNPROCESSABLE_ENTITY)
return account.to_json() if account_type not in ACCOUNT_TYPES:
abort(HTTPStatus.UNPROCESSABLE_ENTITY)
account = Account(-1, '', currency, account_type, custom_name or '')
db_utils.insert_account(decorators.user_id, account)
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…
Cancel
Save