Skip to content

Commit

Permalink
Create transaction.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 10, 2024
1 parent fcacf2d commit a993764
Showing 1 changed file with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from flask import Blueprint, request, jsonify
from models import Transaction
from services import TransactionService
from utils import hash_data

transaction_blueprint = Blueprint('transaction', __name__)

@transaction_blueprint.route('/transaction', methods=['GET'])
def get_transactions():
transaction_service = TransactionService()
transactions = transaction_service.get_transactions()
return jsonify([transaction.__dict__ for transaction in transactions])

@transaction_blueprint.route('/transaction', methods=['POST'])
def create_transaction():
data = request.get_json()
sender = data['sender']
recipient = data['recipient']
amount = data['amount']
transaction = Transaction(sender, recipient, amount)
transaction_service = TransactionService()
transaction_service.add_transaction(transaction)
return jsonify(transaction.__dict__), 201

@transaction_blueprint.route('/transaction/<string:transaction_id>', methods=['GET'])
def get_transaction(transaction_id):
transaction_service = TransactionService()
transaction = transaction_service.get_transaction(transaction_id)
if transaction is None:
return jsonify({'error': 'Transaction not found'}), 404
return jsonify(transaction.__dict__)

@transaction_blueprint.route('/transaction/<string:transaction_id>', methods=['PUT'])
def update_transaction(transaction_id):
data = request.get_json()
sender = data['sender']
recipient = data['recipient']
amount = data['amount']
transaction_service = TransactionService()
transaction = transaction_service.get_transaction(transaction_id)
if transaction is None:
return jsonify({'error': 'Transaction not found'}), 404
transaction.sender = sender
transaction.recipient = recipient
transaction.amount = amount
transaction_service.update_transaction(transaction)
return jsonify(transaction.__dict__)

@transaction_blueprint.route('/transaction/<string:transaction_id>', methods=['DELETE'])
def delete_transaction(transaction_id):
transaction_service = TransactionService()
transaction = transaction_service.get_transaction(transaction_id)
if transaction is None:
return jsonify({'error': 'Transaction not found'}), 404
transaction_service.delete_transaction(transaction)
return jsonify({'message': 'Transaction deleted'})

@transaction_blueprint.route('/transaction/validate', methods=['POST'])
def validate_transaction():
data = request.get_json()
sender = data['sender']
recipient = data['recipient']
amount = data['amount']
transaction = Transaction(sender, recipient, amount)
transaction_service = TransactionService()
if not transaction_service.validate_transaction(transaction):
return jsonify({'error': 'Invalid transaction'}), 400
return jsonify({'message': 'Transaction is valid'})

0 comments on commit a993764

Please sign in to comment.