add one to many relationships and various updates surrounding this
This commit is contained in:
21
api.yml
21
api.yml
@@ -5,7 +5,7 @@ info:
|
||||
Lucas Mathews - Fontys Student ID: 5023572
|
||||
contact:
|
||||
email: 522499@student.fontys.nl
|
||||
version: 2.0.0
|
||||
version: 3.0.0
|
||||
servers:
|
||||
- url: http://127.0.0.1:81
|
||||
tags:
|
||||
@@ -231,6 +231,12 @@ paths:
|
||||
description: Add a new account to the system
|
||||
operationId: manager.add_account
|
||||
parameters:
|
||||
- name: client_id
|
||||
in: query
|
||||
description: ID of client to add account to
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
- name: description
|
||||
in: query
|
||||
description: Account description
|
||||
@@ -254,8 +260,8 @@ paths:
|
||||
description: Successful operation
|
||||
'400':
|
||||
description: Invalid input
|
||||
'422':
|
||||
description: Validation exception
|
||||
'404':
|
||||
description: client_id not found
|
||||
put:
|
||||
tags:
|
||||
- account
|
||||
@@ -627,8 +633,6 @@ components:
|
||||
type: boolean
|
||||
administator:
|
||||
type: boolean
|
||||
accounts:
|
||||
type: array
|
||||
example:
|
||||
client_id: 1
|
||||
name: "Lucas Mathews"
|
||||
@@ -641,13 +645,14 @@ components:
|
||||
notes: "This is a test client"
|
||||
enabled: true
|
||||
administator: false
|
||||
accounts: []
|
||||
Account:
|
||||
type: object
|
||||
properties:
|
||||
account_id:
|
||||
type: integer
|
||||
format: int32
|
||||
client_id:
|
||||
type: string
|
||||
decription:
|
||||
type: string
|
||||
opening_timestamp:
|
||||
@@ -664,13 +669,13 @@ components:
|
||||
type: array
|
||||
example:
|
||||
account_id: 1
|
||||
client_id: 1
|
||||
description: "Savings Account"
|
||||
opening_timestamp: "17-04-2022 16:21:12"
|
||||
account_type: "Rachelsmolen 1, 5612MA, Eindhoven"
|
||||
balance: 2314.23
|
||||
enabled: true
|
||||
notes: "This is a savings account"
|
||||
transactions: []
|
||||
Transaction:
|
||||
type: object
|
||||
properties:
|
||||
@@ -686,7 +691,7 @@ components:
|
||||
type: string
|
||||
description:
|
||||
type: string
|
||||
account_number:
|
||||
account_id:
|
||||
type: string
|
||||
recipient_account_number:
|
||||
type: string
|
||||
|
||||
1
bank.ini
1
bank.ini
@@ -1,6 +1,5 @@
|
||||
[database]
|
||||
name=bank.db
|
||||
add_sample_data=True
|
||||
|
||||
[api_file]
|
||||
name=api.yml
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
# Banking System Account Class
|
||||
|
||||
from sqlalchemy import ForeignKey, Column, String, Integer, Boolean
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
|
||||
|
||||
from class_base import Base
|
||||
@@ -9,23 +11,25 @@ from class_base import Base
|
||||
class Account(Base):
|
||||
__tablename__ = 'accounts'
|
||||
account_id = Column("account_id", String, primary_key=True)
|
||||
client_id = Column(String, ForeignKey('clients.client_id'))
|
||||
description = Column("description", String)
|
||||
open_timestamp = Column("open_timestamp", String)
|
||||
account_type = Column("account_type", String)
|
||||
balance = Column("balance", Integer)
|
||||
enabled = Column("enabled", Boolean)
|
||||
notes = Column("notes", String)
|
||||
transactions = ("transactions", String, ForeignKey("transactions.transaction_id"))
|
||||
transactions = relationship("Transaction", backref="account")
|
||||
|
||||
def __init__(self, account_id, description, open_timestamp, account_type, balance, enabled, notes, transactions):
|
||||
def __init__(self, account_id, client_id, description, open_timestamp, account_type, balance, enabled, notes, transactions):
|
||||
self.account_id = account_id
|
||||
self.client_id = client_id
|
||||
self.description = description
|
||||
self.open_timestamp = open_timestamp
|
||||
self.account_type = account_type
|
||||
self.balance = balance
|
||||
self.enabled = enabled
|
||||
self.notes = notes
|
||||
self.transactions = transactions
|
||||
self.transactions = transactions if transactions is not None else []
|
||||
|
||||
def __repr__(self):
|
||||
return f"Account ID: {self.account_id}, Description: {self.description}, Open Timestamp: {self.open_timestamp}, Account Type: {self.account_type}, Balance: {self.balance}, Enabled: {self.enabled}, Notes: {self.notes}, Transactions: {self.transactions}"
|
||||
@@ -1,7 +1,9 @@
|
||||
# Lucas Mathews - Fontys Student ID: 5023572
|
||||
# Banking System Client Class
|
||||
|
||||
from sqlalchemy import Column, String, Boolean
|
||||
from sqlalchemy import Column, String, Boolean, ForeignKey
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
|
||||
from class_base import Base
|
||||
|
||||
@@ -18,7 +20,7 @@ class Client(Base):
|
||||
notes = Column("notes", String)
|
||||
enabled = Column("enabled", Boolean)
|
||||
administrator = Column("administrator", Boolean)
|
||||
accounts = Column("accounts", String)
|
||||
accounts = relationship("Account", backref="client")
|
||||
|
||||
def __init__(self, client_id, name, birthdate, opening_timestamp, address, phone_number, email, hash, notes, enabled, administrator, accounts):
|
||||
self.client_id = client_id
|
||||
@@ -32,7 +34,7 @@ class Client(Base):
|
||||
self.notes = notes
|
||||
self.enabled = enabled
|
||||
self.administrator = administrator
|
||||
self.accounts = accounts
|
||||
self.accounts = accounts if accounts is not None else []
|
||||
|
||||
def __repr__(self):
|
||||
return f"Client ID: {self.client_id}, Name: {self.name}, Birthdate: {self.birthdate}, Address: {self.address}, Phone Number: {self.phone_number}, Email: {self.email}, Enabled: {self.enabled}, Accounts: {self.accounts}"
|
||||
@@ -1,7 +1,7 @@
|
||||
# Lucas Mathews - Fontys Student ID: 5023572
|
||||
# Banking System Transaction Class
|
||||
|
||||
from sqlalchemy import Column, String, Integer
|
||||
from sqlalchemy import Column, String, Integer, ForeignKey
|
||||
|
||||
from class_base import Base
|
||||
|
||||
@@ -12,7 +12,7 @@ class Transaction(Base):
|
||||
amount = Column("amount", Integer)
|
||||
timestamp = Column("timestamp", String)
|
||||
description = Column("description", String)
|
||||
account_number = Column("account_number", Integer)
|
||||
account_id = Column(String, ForeignKey('accounts.account_id'))
|
||||
recipient_account_number = Column("recipient_account_number", Integer)
|
||||
|
||||
def __init__(self, transaction_id, transaction_type, amount, timestamp, description, account_number, recipient_account_number = None):
|
||||
|
||||
22
database.py
22
database.py
@@ -34,25 +34,3 @@ Base.metadata.create_all(bind=engine) # Creates the tables in the database from
|
||||
# Creates a session to interact with the database
|
||||
Session = sessionmaker(bind=engine) # Creates a session to interact with the database
|
||||
session = Session() # Creates a session object
|
||||
|
||||
# Add sample data if enabled in the configuration file if the database is empty
|
||||
if CONFIG["database"]["add_sample_data"] == "True": # Checks if sample data addition is enabled
|
||||
if session.query(Client).count() == 0: # Checks if the database is empty
|
||||
print(f"Sample data addition is disabled because the database is not empty.")
|
||||
print(f"Adding sample data to new database file {CONFIG["database"]["name"]}.")
|
||||
session.add(Client("f9a16945-b570-4153-ba63-413f2cc2768a", "Lucas Mathews", "24/08/1998", "17/04/2024", "Rachelsmolen 1, 5612MA, Eindhoven", "0612345678", "522499@student.fontys.nl", "7835062ec36ed529fe22cc63baf3ec18d347dacb21c9801da8ba0848cc18efdf1e51717dd5b1240f7556aca3947aa0722452858be6002c1d46b1f1c311b0e9d8", "Notes", True, True, "1, 2"))
|
||||
session.add(Client("5be2a74d-d55c-4de6-85a1-2ed6a355f2cd", "Rigby", "16/03/2018", "06/05/2024", "Rachelsmolen 1, 5612MA, Eindhoven", "0612345678", "522499@cat.fontys.nl", "d3e7df3c78682fbb51e8c6110b3926349bb426bc9834c640cd666519901aef3dfab7822d66fb2dd9e39eb5a8492f6801c2e17ba4c16b8fbcd159c036fe27d8bd", "Is a cat", True, False, "3"))
|
||||
session.add(Account("4c227b02-348c-4611-99a2-8967680cdee6", "Savings Account", "17/04/2024", "Savings", 3000, True, "Savings account", "1"))
|
||||
session.add(Account("b9d9b801-eaab-45be-a4d1-1f7b0bbf798f", "Spending Account", "17/04/2024", "Spending", 150, True, "Spending account", "1"))
|
||||
session.add(Account("f182f186-0a88-4f98-8a02-3a568c65aba7", "Cat Account", "06/05/2024", "Cat Account", 497, True, "Food savings", "2"))
|
||||
session.add(Transaction("9d989788-f983-4590-8de2-9aa0c8bec7d2", "Deposit", 5000, "17/04/2024", "Initial Deposit", 1, "23542335"))
|
||||
session.add(Transaction("153cee93-51c7-4114-b9ef-e307fbf0bf87", "Deposit", 100, "17/04/2024", "Initial Deposit", 2, "23542335"))
|
||||
session.add(Transaction("4bec761a-0f36-452f-a48a-127dcf526e47", "Deposit", 500, "06/05/2024", "Initial Deposit", 3, "23542335"))
|
||||
session.add(Transaction("227a2a9e-a13b-484b-b037-78deeeb0258c", "Withdrawal", 2000, "06/05/2024", "Uni Fees", 3, "Fontys University"))
|
||||
session.add(Transaction("7248a706-29a8-478b-a674-c12ebf9a904a", "Withdrawal", 50, "06/05/2024", "Groceries", 3, "Aldi"))
|
||||
session.add(Transaction("ba367c28-41e6-4f8a-9bfa-3819f7b89a58", "Withdrawal", 3, "06/05/2024", "Treats", 3, "ZooPlus"))
|
||||
session.commit()
|
||||
else:
|
||||
print(f"The database is not empty, skipping sample data addition.")
|
||||
else:
|
||||
print(f"Sample data addition is disabled.")
|
||||
10
manager.py
10
manager.py
@@ -110,15 +110,19 @@ def get_account(account_id:int): # Returns a specific account in the database
|
||||
return jsonify({"error": "Account not found"}), 404
|
||||
if account is not None:
|
||||
for account in account:
|
||||
return jsonify({"description": account.description, "account_type": account.account_type, "balance": account.balance, "enabled": account.enabled, "notes": account.notes}), 200
|
||||
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(description:str, account_type, **kwargs): # Adds a new account to 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)
|
||||
new_account = Account(account_id, description, timestamp(), account_type, 0, 1, notes, None)
|
||||
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)
|
||||
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():
|
||||
|
||||
Reference in New Issue
Block a user