diff --git a/controllers/user.py b/controllers/user.py index 57f244c..ff12024 100644 --- a/controllers/user.py +++ b/controllers/user.py @@ -63,7 +63,7 @@ def create_new(self, first_name: str, last_name: str, age: int, e_mail: str, error_message=[f"{get_error(error_suf[1])}"] ) - new_user = self.get_user()[-1] + new_user = self.get_user()["data"][-1] return Schema.api_response( status=200, diff --git a/controllers/withdrawal.py b/controllers/withdrawal.py index e78ebe3..133160b 100644 --- a/controllers/withdrawal.py +++ b/controllers/withdrawal.py @@ -1,6 +1,9 @@ +from cmath import pi from controllers.user import User_Controller from database.db_connector import SQLite_Connector from schemas.schemas import Schema +from src.error_message import Error_Message,get_error +from src.success_message import Success_Message user_controller = User_Controller() @@ -9,19 +12,41 @@ def __init__(self, db_name='database/bank_simulator.db') -> None: super().__init__(db_name) - def get_withdrawal(self, id=None): + def get_money_withdrawal(self, id=None): + + money_withdrawals = None + if id: - return self.execute_sql_query(f"SELECT * FROM money_withdrawal WHERE id={id}", Schema.withdrawal) + money_withdrawals = self.execute_sql_query(f"SELECT * FROM money_withdrawal WHERE id={id}", Schema.withdrawal) + else: + money_withdrawals = self.execute_sql_query(f"SELECT * FROM money_withdrawal", Schema.withdrawal) - return self.execute_sql_query(f"SELECT * FROM money_withdrawal", Schema.withdrawal) + if id and not money_withdrawals: + return Schema.api_response(status=404,error_message=[ + Error_Message.withdrawal_not_exist.value,]) + elif not id and not money_withdrawals: + return Schema.api_response(status=404,error_message=[ + Error_Message.there_not_withdrawals.value]) + return Schema.api_response(status=200,data = money_withdrawals) + def create_new(self, id_user: int, amount: float, withdrawal_date:str ) -> list: - withdrawal_status = self.withdrawal_amount(id_user,amount) + user = user_controller.get_user(id_user)["data"][0] + print(user) + + if not user: + return Schema.api_response(status=404, error_message=[ + Error_Message.user_not_exist.value] + ) + + if user["balance"] < amount: + return Schema.api_response(status=200, error_message=[ + Error_Message.negative_balance.value] + ) - if not withdrawal_status: - print("levantamento regeitado") - return [] + new_user_balance = user["balance"] - amount + user_controller.update_user_balance(user["account_number"], new_user_balance) sql_query = f""" INSERT INTO money_withdrawal @@ -29,21 +54,19 @@ def create_new(self, id_user: int, amount: float, withdrawal_date:str ) -> list: VALUES ({id_user},{amount},'{withdrawal_date}'); """ - self.execute_sql_query(sql_query,Schema.withdrawal) - - return self.get_withdrawal()[-1] - - def withdrawal_amount(self, id_user, amount) -> bool: - user = user_controller.get_user(id_user)[0] - - if not user: - return False - - if user["balance"] < amount: - return False - - new_user_balance = user["balance"] - amount - user_controller.update_user_balance(user["account_number"], new_user_balance) + try: + self.execute_sql_query(sql_query,Schema.withdrawal) + except Exception as error: + error_suf = f"{error}".split(".") + return Schema.api_response( + status=500, + error_message=[f"{get_error(error_suf[1])}"] + ) - return True + new_money_withdrawal = self.get_money_withdrawal()["data"][-1] + return Schema.api_response( + status=200, + data=new_money_withdrawal, + success_message=[Success_Message.new_withdrawal.value] + ) \ No newline at end of file diff --git a/database/bank_simulator.db b/database/bank_simulator.db index bf94c18..1d661ba 100644 Binary files a/database/bank_simulator.db and b/database/bank_simulator.db differ diff --git a/db/bank_simulator.db b/db/bank_simulator.db index 413312c..30e43fc 100644 Binary files a/db/bank_simulator.db and b/db/bank_simulator.db differ diff --git a/routes/withdrawal.py b/routes/withdrawal.py index c784e64..b978c41 100644 --- a/routes/withdrawal.py +++ b/routes/withdrawal.py @@ -17,7 +17,7 @@ def __init__(self) -> None: super().__init__() def get(self): - return jsonify(withdrawal_controller.get_withdrawal()) + return jsonify(withdrawal_controller.get_money_withdrawal()) def post(self): args = request_put_args.parse_args() @@ -34,7 +34,7 @@ def __init__(self) -> None: super().__init__() def get(self, id): - return jsonify(withdrawal_controller.get_withdrawal(id)) + return jsonify(withdrawal_controller.get_money_withdrawal(id)) diff --git a/src/error_message.py b/src/error_message.py index dfaf74e..d6144a9 100644 --- a/src/error_message.py +++ b/src/error_message.py @@ -4,13 +4,16 @@ class Error_Message(Enum): id_not_exist = "This user ID doesn't exist" user_not_exist = "This user doesn't exist" - there_not_existent_users = "There is no registred users on this app" + there_not_existent_users = "There are no registered users on this app" e_mail_already_exist = "This email address is already in use" nif_already_exist = "This NIF is used" - negative_balance = "Amount grather than the account balance" + negative_balance = "Amount greater than the account balance" equal_password = "Old and New password are the both the same" internal_error = "Server internal Error" admin_password_error = "The Admin Password do not match" + withdrawal_not_exist = "This money withdrawal doesn't exist" + there_not_withdrawals = "There are no registered withdrawals on this app" + def get_error(error_suf: str) -> str: diff --git a/src/success_message.py b/src/success_message.py index 03261b6..c459a08 100644 --- a/src/success_message.py +++ b/src/success_message.py @@ -6,6 +6,6 @@ class Success_Message(Enum): password_updated = "Password updated successfully" deleted_user = "User deleted successfully" transfer_success = "Transfer made successfully" - transfer_success_1 = "Transfer made successfully 1" + new_withdrawal = "Successful withdrawal" \ No newline at end of file