From c6dbf1d347ea163fbef92105657b6f6d4f63e014 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 22 Sep 2024 14:15:13 +0700 Subject: [PATCH] Create banking.py --- .../banks/banking_app/app/banking/banking.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 bank_integration/banks/banking_app/app/banking/banking.py diff --git a/bank_integration/banks/banking_app/app/banking/banking.py b/bank_integration/banks/banking_app/app/banking/banking.py new file mode 100644 index 000000000..aa970673b --- /dev/null +++ b/bank_integration/banks/banking_app/app/banking/banking.py @@ -0,0 +1,47 @@ +from flask import request, jsonify, current_app +from flask_jwt_extended import jwt_required +from .models import Account +from .schemas import AccountSchema + +@banking_blueprint.route("/accounts", methods=["GET"]) +@jwt_required +def get_accounts(): + accounts = Account.query.all() + return jsonify([AccountSchema().dump(account) for account in accounts]) + +@banking_blueprint.route("/accounts", methods=["POST"]) +@jwt_required +def create_account(): + account_number = request.json.get("account_number") + account = Account(account_number=account_number) + db.session.add(account) + db.session.commit() + return jsonify(message="Account created successfully") + +@banking_blueprint.route("/accounts/", methods=["GET"]) +@jwt_required +def get_account(account_id): + account = Account.query.get(account_id) + if account: + return jsonify(AccountSchema().dump(account)) + return jsonify(error="Account not found"), 404 + +@banking_blueprint.route("/accounts/", methods=["PUT"]) +@jwt_required +def update_account(account_id): + account = Account.query.get(account_id) + if account: + account.account_number = request.json.get("account_number") + db.session.commit() + return jsonify(message="Account updated successfully") + return jsonify(error="Account not found"), 404 + +@banking_blueprint.route("/accounts/", methods=["DELETE"]) +@jwt_required +def delete_account(account_id): + account = Account.query.get(account_id) + if account: + db.session.delete(account) + db.session.commit() + return jsonify(message="Account deleted successfully") + return jsonify(error="Account not found"), 404