Updating relationships and further corrections

This commit is contained in:
Lucas Mathews
2024-05-17 20:44:56 +02:00
parent ba634e4e49
commit 1bf60b91f5
5 changed files with 83 additions and 33 deletions

29
api.yml
View File

@@ -134,7 +134,7 @@ paths:
type: string
- name: password
in: query
description: Client Email Address
description: Client Password
required: true
schema:
type: string
@@ -189,8 +189,7 @@ paths:
description: ID of client to return
required: true
schema:
type: integer
format: int32
type: string
responses:
'200':
description: Successful operation
@@ -308,8 +307,7 @@ paths:
description: ID of account to return
required: true
schema:
type: integer
format: int32
type: string
responses:
'200':
description: Successful operation
@@ -382,15 +380,15 @@ paths:
schema:
type: integer
format: int32
- name: account_from
- name: account_id
in: query
description: Account number the money paid from
required: true
schema:
type: string
- name: account_to
- name: recipient_account_id
in: query
description: Recipient account number
description: Recipient account_id
required: true
schema:
type: string
@@ -405,8 +403,10 @@ paths:
description: Successful operation
'400':
description: Invalid input
'422':
description: Validation exception
'401':
description: Insufficient funds
'404':
description: account_id not Found
/Transaction/History:
get:
tags:
@@ -420,8 +420,7 @@ paths:
description: ID of account to return
required: true
schema:
type: integer
format: int32
type: string
responses:
'200':
description: Successful operation
@@ -691,7 +690,7 @@ components:
type: string
description:
type: string
account_id:
account_to:
type: string
recipient_account_number:
type: string
@@ -701,6 +700,6 @@ components:
amount: 100.00
timestamp: "17-04-2022 16:21:12"
description: "Deposit to Savings Account"
account_number: "NL12ABNA0123456789"
recipient_account_number: "NL12ABNA1234567890"
account_id: "NL12ABNA0123456789"
recipient_account_id: "NL12ABNA1234567890"

BIN
bank.db

Binary file not shown.

View File

@@ -18,7 +18,7 @@ class Account(Base):
balance = Column("balance", Integer)
enabled = Column("enabled", Boolean)
notes = Column("notes", String)
transactions = relationship("Transaction", backref="account")
transactions = relationship("Transaction", foreign_keys='Transaction.account_id', backref="account")
def __init__(self, account_id, client_id, description, open_timestamp, account_type, balance, enabled, notes, transactions):
self.account_id = account_id

View File

@@ -13,16 +13,16 @@ class Transaction(Base):
timestamp = Column("timestamp", String)
description = Column("description", String)
account_id = Column(String, ForeignKey('accounts.account_id'))
recipient_account_number = Column("recipient_account_number", Integer)
recipient_account_id = Column(String, ForeignKey('accounts.account_id'))
def __init__(self, transaction_id, transaction_type, amount, timestamp, description, account_number, recipient_account_number = None):
def __init__(self, transaction_id, transaction_type, amount, timestamp, description, account_id, recipient_account_id = None):
self.transaction_id = transaction_id
self.transaction_type = transaction_type
self.amount = amount
self.timestamp = timestamp
self.description = description
self.account_number = account_number
self.recipient_account_number = recipient_account_number
self.account_id = account_id
self.recipient_account_id = recipient_account_id
def __repr__(self):
return f"Transaction ID: {self.transaction_id}, Transaction Type: {self.transaction_type}, Amount: {self.amount}, Timestamp: {self.timestamp}, Description: {self.description} From Account Number: {self.account_number}, Recipient Account Number: {self.recipient_account_number}"
return f"Transaction ID: {self.transaction_id}, Transaction Type: {self.transaction_type}, Amount: {self.amount}, Timestamp: {self.timestamp}, Description: {self.description} From Account Number: {self.account_id}, Recipient Account Number: {self.recipient_account_id}"

View File

@@ -115,6 +115,23 @@ def get_account(account_id:int): # Returns a specific account in the database
def add_account(client_id, description:str, account_type, **kwargs): # Adds a new account to the database
account_id = generate_uuid_short()
notes = kwargs.get("notes", None)
client_found = None
# Find the client
for client in session.query(Client).all():
if client.client_id == client_id:
client_found = client
break
# Check if client was found
if client_found is None:
return f"client_id: {client_id} is not found.", 422
# Add the new account
new_account = Account(account_id, client_id, description, timestamp(), account_type, 0, 1, notes, None)
session.add(new_account)
session.commit()
return f"New account has been added: description: {description}, uuid: {account_id} ", 200
"""
for client in session.query(Client).all():
if client.client_id == client_id:
new_account = Account(account_id, client_id, description, timestamp(), account_type, 0, 1, notes, None)
@@ -123,7 +140,7 @@ def add_account(client_id, description:str, account_type, **kwargs): # Adds a ne
return f"New account has been added: description: {description}, uuid: {account_id} ", 200
else:
return f"client_id: {client_id} is not found.", 422
"""
def delete_account(account_id): # Deletes an account from the database
for account in session.query(Account).all():
if account.account_id == account_id:
@@ -165,29 +182,63 @@ def get_transaction(transaction_id:int): # Returns a specific transaction in the
return jsonify({"transaction_type": transaction.transaction_type, "amount": transaction.amount, "timestamp": transaction.timestamp, "description": transaction.description, "account_number": transaction.account_number, "recipient_account_number": transaction.recipient_account_number}), 200
def transaction_history(account_id:int): # Returns all transactions for a specific account
result = session.query(Transaction).filter(Transaction.account_number == account_id)
result = session.query(Transaction).filter(Transaction.account_id == account_id)
return jsonify([{"transaction_id": transaction.transaction_id, "transaction_type": transaction.transaction_type, "amount": transaction.amount, "timestamp": transaction.timestamp, "description": transaction.description, "account_number": transaction.account_number, "recipient_account_number": transaction.recipient_account_number} for transaction in result]), 200
def add_transaction(amount:int, account_from, account_to, **kwargs): # Adds a new transaction to the database
def add_transaction(amount:int, account_id, recipient_account_id, **kwargs): # Adds a new transaction to the database
transaction_id = generate_uuid()
for account in session.query(Account).all():
if account.account_id == account_from:
if account.account_id == account_id:
account_from = account
if account.account_id == recipient_account_id:
account_dest = account
# Check if account has enough funds
if account_from.balance < amount:
return f"Account ID: {account_id} does not have enough funds to transfer {amount}.", 401
# Perform the transaction
account_from.balance -= amount
account_dest.balance += amount
transaction_type = "transfer"
session.commit()
# Create the transaction record
description = kwargs.get("description", None)
new_transaction = Transaction(transaction_id, transaction_type, amount, timestamp(), description, account_id, recipient_account_id)
session.add(new_transaction)
session.commit()
return f"New transaction has been added: description: {description}, uuid: {transaction_id} ", 200
"""
if account_from is None:
return f"Account ID: {account_id} is not found.", 404
if account_dest is None:
return f"Account ID: {account_to} is not found.", 404
for account in session.query(Account).all():
if account.account_id == account_id:
if account.balance < amount:
return f"Account ID: {account_from} does not have enough funds to transfer {amount}.", 401
return f"Account ID: {account_id} does not have enough funds to transfer {amount}.", 401
account.balance -= amount
transaction_type = "withdraw"
session.commit()
return
else:
return f"Account ID: {account_id} is not found.", 404
for account in session.query(Account).all():
if account.account_id == account_to:
account.balance += amount
transaction_type = "transfer"
session.commit()
description = kwargs.get("description", None)
new_transaction = Transaction(transaction_id, transaction_type, amount, timestamp(), description, account_from, account_to)
new_transaction = Transaction(transaction_id, transaction_type, amount, timestamp(), description, account_id, account_to)
session.add(new_transaction)
session.commit()
return f"New transaction has been added: description: {description}, uuid: {transaction_id} ", 200
"""
#####################
### Administrator ###
@@ -199,22 +250,22 @@ def get_all_clients(): # Returns all clients in the database
def get_all_accounts(): # Returns all accounts in the database
accounts = session.query(Account).all()
return jsonify([{"account_id": account.account_id, "description": account.description, "open_timestamp": account.open_timestamp, "account_type": account.account_type, "balance": account.balance, "enabled": account.enabled, "notes": account.notes} for account in accounts])
return jsonify([{"account_id": account.account_id, "client_id": account.client_id, "description": account.description, "open_timestamp": account.open_timestamp, "account_type": account.account_type, "balance": account.balance, "enabled": account.enabled, "notes": account.notes} for account in accounts])
def get_all_transactions(): # Returns all transactions in the database
transactions = session.query(Transaction).all()
return jsonify([{"transaction_id": transaction.transaction_id, "transaction_type": transaction.transaction_type, "amount": transaction.amount, "timestamp": transaction.timestamp, "description": transaction.description, "account_number": transaction.account_number, "recipient_account_number": transaction.recipient_account_number} for transaction in transactions])
return jsonify([{"transaction_id": transaction.transaction_id, "transaction_type": transaction.transaction_type, "amount": transaction.amount, "timestamp": transaction.timestamp, "description": transaction.description, "account_id": transaction.account_id, "recipient_account_id": transaction.recipient_account_id} for transaction in transactions])
def update_transaction(transaction_id, transaction_type, amount, description, account_number, recipient_account_number):
def update_transaction(transaction_id, transaction_type, amount, description, account_id, recipient_account_id):
for transaction in session.query(Transaction).all():
if transaction.transaction_id == transaction_id:
transaction.transaction_type = transaction_type
transaction.amount = amount
transaction.description = description
transaction.account_number = account_number
transaction.recipient_account_number = recipient_account_number
transaction.account_id = account_id
transaction.recipient_account_id = recipient_account_id
session.commit()
return f"Transaction ID: {transaction_id} has been updated."
return f"Transaction ID: {transaction_id} is not found."