From 1bf60b91f50b4605e0281f866ff6b3328d693c3a Mon Sep 17 00:00:00 2001 From: Lucas Mathews Date: Fri, 17 May 2024 20:44:56 +0200 Subject: [PATCH] Updating relationships and further corrections --- api.yml | 29 ++++++++--------- bank.db | Bin 28672 -> 28672 bytes class_account.py | 2 +- class_transaction.py | 10 +++--- manager.py | 75 ++++++++++++++++++++++++++++++++++++------- 5 files changed, 83 insertions(+), 33 deletions(-) diff --git a/api.yml b/api.yml index 45dbab7..611b2fe 100644 --- a/api.yml +++ b/api.yml @@ -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" diff --git a/bank.db b/bank.db index cc676775c96afc9350351058773652d1434d54e2..03becb765417072b506622e5999dfebd37d05fa9 100644 GIT binary patch literal 28672 zcmeI)Z)@8|90zd8N}bq=qd^E}1cUk@G%oWh>Ganym>Z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7KLS@R!V0GDoLZf_#2n};ROK*KmY;|fB*y_009U<00Izz z!2c@n4ZpNs;|W)**`Dm_swOuAJ@6WusoM=Z`JIXerHX2K#MFH`nEildKqtbvhZ0Cr yC+Q?WIy1oT|MTLXT=It(1Rwwb2tWV=5P$##AOHafKmYQTg3o}$A5=H0xWjhs8p^8vd6+zXZWvP7UvlIzZ2rh+% zw51DMt89$?3;8Ruz}~4_{{aR@?5IYHKr9_jr}yrC?|t8W@9;u9ywFY;G~?{f>Ma92 zeE-FQzd+d>0s9P&td_lPjx1wXoqJ)r$B(9G8jho#Y^0HRr6MW!_Pgo3;w}$Ytb5vY zx!hQF20!4LiOCWXIt&3?ac>n#po&7 zdDBtdboZ*=?%y@fH`g{EZ|=D3?H%`i+Kal`=(Mxf-R;E9O4FYzH`W)&gQuD{b2WxmP?jXtCM=Yery3_R`6K)ijba44^%J32-79RmOS6}!WQ;h z9?#Rok#XQhGC(+?DpV>)m=K`?FuZ#_WmjGVEpTp|P2%eqJ-@TzbTH!;<)E%QxWo7n(xvM&_T$u>% z{8fhufZyR)m}K^D!#>sx$ElIptN}QuTnUjB&9V=ebeDQ9n!Afje=tZ8&cZ1CABNxz zgR~4oYR~|HGENoaB3IDNS7~wzVc8nxh@8C+&&Sf?`B+plOXcDVs(Kz29L7IUH2)2g Cm-CJQ diff --git a/class_account.py b/class_account.py index 173b2ed..51e86b0 100644 --- a/class_account.py +++ b/class_account.py @@ -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 diff --git a/class_transaction.py b/class_transaction.py index 421d530..2b938a4 100644 --- a/class_transaction.py +++ b/class_transaction.py @@ -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}" \ No newline at end of file + 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}" \ No newline at end of file diff --git a/manager.py b/manager.py index 37dcf77..f655df4 100644 --- a/manager.py +++ b/manager.py @@ -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."