added update account and client functionality to api

This commit is contained in:
Lucas Mathews
2024-05-17 21:19:05 +02:00
parent 1bf60b91f5
commit e04463733b
3 changed files with 138 additions and 172 deletions

124
api.yml
View File

@@ -157,26 +157,54 @@ paths:
summary: Update an existing client summary: Update an existing client
description: Update an existing client Id description: Update an existing client Id
operationId: manager.update_client operationId: manager.update_client
requestBody: parameters:
description: Update an existing client's details - name: client_id
content: in: query
application/json: description: ID of client to update
schema: required: true
$ref: '#/components/schemas/Client' schema:
required: true type: string
- name: name
in: query
description: Client Name
required: false
schema:
type: string
- name: birthdate
in: query
description: Client Birthdate (dd-mm-yyyy)
required: false
schema:
type: string
- name: address
in: query
description: Client Address
required: false
schema:
type: string
- name: phone_number
in: query
description: Client Phone Number
required: false
schema:
type: string
- name: email
in: query
description: Client Email Address
required: false
schema:
type: string
- name: notes
in: query
description: Notes about client
required: false
schema:
type: string
responses: responses:
'200': '200':
description: Successful operation description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Client'
'400': '400':
description: Invalid Client ID supplied description: Invalid Client ID supplied
'404':
description: Client not found
'422':
description: Validation exception
get: get:
tags: tags:
- client - client
@@ -273,28 +301,31 @@ paths:
description: ID of account to update description: ID of account to update
required: true required: true
schema: schema:
type: integer type: string
format: int32 - name: description
requestBody: in: query
description: Update an existing account description: Account description
content: required: false
application/json: schema:
schema: type: string
$ref: '#/components/schemas/Account' - name: account_type
required: true in: query
description: Type of account
required: false
schema:
type: string
- name: notes
in: query
description: Notes about account
required: false
schema:
type: string
responses: responses:
'200': '200':
description: Successful operation description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Account'
'400': '400':
description: Invalid Account ID supplied description: Invalid Account ID supplied
'404':
description: Account not found
'422':
description: Validation exception
get: get:
tags: tags:
- account - account
@@ -353,8 +384,7 @@ paths:
description: ID of transaction to return description: ID of transaction to return
required: true required: true
schema: schema:
type: integer type: string
format: int32
responses: responses:
'200': '200':
description: Successful operation description: Successful operation
@@ -535,32 +565,6 @@ paths:
description: Invalid input description: Invalid input
'404': '404':
description: No transactions found description: No transactions found
put:
tags:
- manager
summary: Update an existing transaction
description: Update an existing transaction
operationId: manager.update_transaction
requestBody:
description: Update an existing transaction
content:
application/json:
schema:
$ref: '#/components/schemas/Transaction'
required: true
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Transaction'
'400':
description: Invalid Transaction ID supplied
'404':
description: Transaction not found
'422':
description: Validation exception
/Manager/Hash: /Manager/Hash:
get: get:
tags: tags:

BIN
bank.db

Binary file not shown.

View File

@@ -32,24 +32,12 @@ def generate_uuid_short(): # Generates a short uuid
### Client ### ### Client ###
############## ##############
def get_client(client_id:int): # Returns a specific client in the database def get_client(client_id:str): # Returns a specific client in the database
client = session.query(Client).filter_by(client_id=client_id).one_or_none() client = session.query(Client).filter_by(client_id=client_id).one_or_none()
if client is None:
return jsonify({"error": "Client not found"}), 404
if client is not None:
return jsonify({"name": client.name, "birthdate": client.birthdate, "opening_timestamp": client.opening_timestamp, "address": client.address, "phone_number": client.phone_number, "email": client.email}), 200
def change_password(client_id, password:str, new_password:str): # Changes the password of a client
old_hash = password_hash(password)
new_hash = password_hash(new_password)
for client in session.query(Client).all(): for client in session.query(Client).all():
if client.client_id == client_id: if client.client_id == client_id:
if client.hash == old_hash: return jsonify({"name": client.name, "birthdate": client.birthdate, "opening_timestamp": client.opening_timestamp, "address": client.address, "phone_number": client.phone_number, "email": client.email}), 200
client.hash = new_hash return jsonify({"error": "Client not found"}), 404
session.commit()
return "Password changed successfully.", 200
return "Incorrect old password.", 400
return f"client_id: {client_id} is not found.", 404
def add_client(name:str, birthdate:str, address:str, phone_number:str, email:str, password:str, **kwargs): # Adds a new client to the database def add_client(name:str, birthdate:str, address:str, phone_number:str, email:str, password:str, **kwargs): # Adds a new client to the database
client_id = generate_uuid_short() client_id = generate_uuid_short()
@@ -59,7 +47,7 @@ def add_client(name:str, birthdate:str, address:str, phone_number:str, email:str
session.commit() session.commit()
return f"New client has been added: name: {name}, uuid: {client_id} ", 200 return f"New client has been added: name: {name}, uuid: {client_id} ", 200
def delete_client(client_id): # Deletes a client from the database def delete_client(client_id:str): # Deletes a client from the database
for client in session.query(Client).all(): for client in session.query(Client).all():
if client.client_id == client_id: if client.client_id == client_id:
if client.accounts == None: if client.accounts == None:
@@ -70,7 +58,42 @@ def delete_client(client_id): # Deletes a client from the database
return f"client_id: {client_id} has active accounts and can not be removed.", 400 return f"client_id: {client_id} has active accounts and can not be removed.", 400
return f"client_id: {client_id} is not found.", 404 return f"client_id: {client_id} is not found.", 404
def update_client(client_id:str, **kwargs): # Updates a client in the database
for client in session.query(Client).all():
if client.client_id == client_id:
name = kwargs.get("name", None)
birthdate = kwargs.get("birthdate", None)
address = kwargs.get("address", None)
phone_number = kwargs.get("phone_number", None)
email = kwargs.get("email", None)
notes = kwargs.get("notes", None)
if name:
client.name = name
if birthdate:
client.birthdate = birthdate
if address:
client.address = address
if phone_number:
client.phone_number = phone_number
if email:
client.email = email
if notes:
client.notes = notes
session.commit()
return f"client_id: {client_id} has been updated.", 299
return f"Client ID: {client_id} is not found." , 400
def change_password(client_id:str, password:str, new_password:str): # Changes the password of a client
old_hash = password_hash(password)
new_hash = password_hash(new_password)
for client in session.query(Client).all():
if client.client_id == client_id:
if client.hash == old_hash:
client.hash = new_hash
session.commit()
return "Password changed successfully.", 200
return "Incorrect old password.", 400
return f"client_id: {client_id} is not found.", 404
@@ -85,18 +108,7 @@ def logout_user():
def update_client(client_id, name, birthdate, address, phone_number, email, notes):
for client in session.query(Client).all():
if client.client_id == client_id:
client.name = name
client.birthdate = birthdate
client.address = address
client.phone_number = phone_number
client.email = email
client.notes = notes
session.commit()
return f"client_id: {client_id} has been updated."
return f"Client ID: {client_id} is not found."
@@ -104,15 +116,14 @@ def update_client(client_id, name, birthdate, address, phone_number, email, note
### Account ### ### Account ###
############### ###############
def get_account(account_id:int): # Returns a specific account in the database def get_account(account_id:str): # Returns a specific account in the database
account = session.query(Account).filter_by(account_id=account_id).one_or_none() account = session.query(Account).filter_by(account_id=account_id).one_or_none()
if account is None: for account in session.query(Account).all():
return jsonify({"error": "Account not found"}), 404 if account.account_id == account_id:
if account is not None: return jsonify({"client_id": account.client_id, "description": account.description, "account_type": account.account_type, "balance": account.balance, "enabled": account.enabled, "notes": account.notes}), 200
for account in account: return jsonify({"error": "Account not found"}), 404
return jsonify({"client_id": account.client.id, "description": account.description, "account_type": account.account_type, "balance": account.balance, "enabled": account.enabled, "notes": account.notes}), 200
def add_account(client_id, description:str, account_type, **kwargs): # Adds a new account to the database def add_account(client_id:str, description:str, account_type:str, **kwargs): # Adds a new account to the database
account_id = generate_uuid_short() account_id = generate_uuid_short()
notes = kwargs.get("notes", None) notes = kwargs.get("notes", None)
client_found = None client_found = None
@@ -121,27 +132,16 @@ def add_account(client_id, description:str, account_type, **kwargs): # Adds a ne
if client.client_id == client_id: if client.client_id == client_id:
client_found = client client_found = client
break break
# Check if client was found # Check if client was found
if client_found is None: if client_found is None:
return f"client_id: {client_id} is not found.", 422 return f"client_id: {client_id} is not found.", 422
# Add the new account # Add the new account
new_account = Account(account_id, client_id, description, timestamp(), account_type, 0, 1, notes, None) new_account = Account(account_id, client_id, description, timestamp(), account_type, 0, 1, notes, None)
session.add(new_account) session.add(new_account)
session.commit() session.commit()
return f"New account has been added: description: {description}, uuid: {account_id} ", 200 return f"New account has been added: description: {description}, uuid: {account_id} ", 200
"""
for client in session.query(Client).all(): def delete_account(account_id:str): # Deletes an account from the database
if client.client_id == client_id:
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
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(): for account in session.query(Account).all():
if account.account_id == account_id: if account.account_id == account_id:
if account.balance == 0: if account.balance == 0:
@@ -152,20 +152,27 @@ def delete_account(account_id): # Deletes an account from the database
return f"account_id: {account_id} has a balance and can not be removed.", 400 return f"account_id: {account_id} has a balance and can not be removed.", 400
return f"account_id: {account_id} is not found.", 404 return f"account_id: {account_id} is not found.", 404
def update_account(account_id:str, **kwargs): # Updates an account in the database
def update_account(account_id:int, update:dict):
for account in session.query(Account).all(): for account in session.query(Account).all():
if account.account_id == account_id: if account.account_id == account_id:
account.description = update["description"] description = kwargs.get("description", None)
account.account_type = update["account_type"] account_type = kwargs.get("account_type", None)
account.balance = update["balance"] balance = kwargs.get("balance", None)
account.enabled = update["enabled"] enabled = kwargs.get("enabled", None)
account.notes = update["notes"] notes = kwargs.get("notes", None)
if description:
account.description = description
if account_type:
account.account_type = account_type
if balance:
account.balance = balance
if enabled:
account.enabled = enabled
if notes:
account.notes = notes
session.commit() session.commit()
return f"account_id: {update['account_id']} has been updated." return f"account_id: {account_id} has been updated.", 200
return f"account_id: {update['account_id']} is not found." return f"account_id: {account_id} is not found.", 400
@@ -176,14 +183,10 @@ def update_account(account_id:int, update:dict):
def get_transaction(transaction_id:int): # Returns a specific transaction in the database def get_transaction(transaction_id:int): # Returns a specific transaction in the database
transaction = session.query(Transaction).filter_by(transaction_id=transaction_id).one_or_none() transaction = session.query(Transaction).filter_by(transaction_id=transaction_id).one_or_none()
if transaction is None: for transaction in session.query(Transaction).all():
return jsonify({"error": "Transaction not found"}), 404 if transaction.transaction_id == transaction_id:
if transaction is not None: return jsonify({"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}), 200
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 return jsonify({"error": "Transaction not found"}), 404
def transaction_history(account_id:int): # Returns all transactions for a specific account
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_id, recipient_account_id, **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() transaction_id = generate_uuid()
@@ -192,53 +195,24 @@ def add_transaction(amount:int, account_id, recipient_account_id, **kwargs): # A
account_from = account account_from = account
if account.account_id == recipient_account_id: if account.account_id == recipient_account_id:
account_dest = account account_dest = account
# Check if account has enough funds # Check if account has enough funds
if account_from.balance < amount: if account_from.balance < amount:
return f"Account ID: {account_id} does not have enough funds to transfer {amount}.", 401 return f"Account ID: {account_id} does not have enough funds to transfer {amount}.", 401
# Perform the transaction # Perform the transaction
account_from.balance -= amount account_from.balance -= amount
account_dest.balance += amount account_dest.balance += amount
transaction_type = "transfer" transaction_type = "transfer"
session.commit() session.commit()
# Create the transaction record # Create the transaction record
description = kwargs.get("description", None) description = kwargs.get("description", None)
new_transaction = Transaction(transaction_id, transaction_type, amount, timestamp(), description, account_id, recipient_account_id) new_transaction = Transaction(transaction_id, transaction_type, amount, timestamp(), description, account_id, recipient_account_id)
session.add(new_transaction) session.add(new_transaction)
session.commit() session.commit()
return f"New transaction has been added: description: {description}, uuid: {transaction_id} ", 200 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_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_id, account_to)
session.add(new_transaction)
session.commit()
return f"New transaction has been added: description: {description}, uuid: {transaction_id} ", 200
""" def transaction_history(account_id:int): # Returns all transactions for a specific account
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_id, "recipient_account_number": transaction.recipient_account_id} for transaction in result]), 200
##################### #####################
### Administrator ### ### Administrator ###
@@ -257,19 +231,7 @@ def get_all_transactions(): # Returns all transactions in the database
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]) 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_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_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."
def apply_interest(account_id:int, interest_rate:float): def apply_interest(account_id:int, interest_rate:float):
for account in session.query(Account).filter(Account.account_id == account_id): for account in session.query(Account).filter(Account.account_id == account_id):
if account.account_id == account_id: if account.account_id == account_id: