From 80dd89212f99c861376b5a09bb508a04e92d2b5a Mon Sep 17 00:00:00 2001 From: Thastertyn Date: Mon, 6 May 2024 00:52:03 +0200 Subject: [PATCH] Unify json naming --- app/api/routes/error_routes.py | 14 +++++----- app/services/cart_service.py | 4 +-- app/services/product_service.py | 12 ++++----- app/services/user_service.py | 46 ++++++++++++++++----------------- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/app/api/routes/error_routes.py b/app/api/routes/error_routes.py index e2b442d..53672ea 100644 --- a/app/api/routes/error_routes.py +++ b/app/api/routes/error_routes.py @@ -2,28 +2,28 @@ from app.api import bp_errors @bp_errors.app_errorhandler(400) def bad_request(e): - return {"Bad Request": "The request was incorrectly formatted, or contained invalid data"}, 400 + return {"msg": "The request was incorrectly formatted, or contained invalid data"}, 400 @bp_errors.app_errorhandler(401) def unauthorized(e): - return {"Unauthorized": "Failed to authorize the request"}, 401 + return {"msg": "Failed to authorize the request"}, 401 @bp_errors.app_errorhandler(403) def forbidden(e): - return {"Forbidden": "You shall not pass"}, 403 + return {"msg": "You shall not pass"}, 403 @bp_errors.app_errorhandler(404) def not_found(e): - return {"Not Found": "The requested resource was not found"}, 404 + return {"msg": "The requested resource was not found"}, 404 @bp_errors.app_errorhandler(405) def method_not_allowed(e): - return {"Method Not Allowed": "The method used is not allowed in current context"}, 405 + return {"msg": "The method used is not allowed in current context"}, 405 @bp_errors.app_errorhandler(500) def internal_error(e): - return {"Internal Server Error": "An error occurred on he server"}, 500 + return {"msg": "An error occurred on he server"}, 500 @bp_errors.app_errorhandler(501) def internal_error(e): - return {"Not Implemented": "This function has not been implemented yet. Check back soon!"}, 501 \ No newline at end of file + return {"msg": "This function has not been implemented yet. Check back soon!"}, 501 \ No newline at end of file diff --git a/app/services/cart_service.py b/app/services/cart_service.py index d6c81ce..887e047 100644 --- a/app/services/cart_service.py +++ b/app/services/cart_service.py @@ -176,7 +176,7 @@ class CartService: # clear cart except Error as e: - return {"Failed": f"Failed to load cart. Reason: {e}"}, 500 + return {"msg": f"Failed to load cart. Reason: {e}"}, 500 - return {"Success": "Successfully purchased"}, 200 + return {"msg": "Successfully purchased"}, 200 diff --git a/app/services/product_service.py b/app/services/product_service.py index ad5b4ab..a60dab0 100644 --- a/app/services/product_service.py +++ b/app/services/product_service.py @@ -26,7 +26,7 @@ class ProductService: results = cursor.fetchall() if len(results) < 1: - return {"Failed": "Failed to fetch products. You've probably selected too far with pages"}, 400 + return {"msg": "Failed to fetch products. You've probably selected too far with pages"}, 400 result_obj = [] for row in results: @@ -42,7 +42,7 @@ class ProductService: return result_obj, 200 except Error as e: - return {"Failed": f"Failed to fetch products. Error: {e}"}, 500 + return {"msg": f"Failed to fetch products. Error: {e}"}, 500 @staticmethod def get_product_info(fields: list[str], product_id: int): @@ -79,7 +79,7 @@ class ProductService: result = cursor.fetchone() if cursor.rowcount != 1: - return {"Failed": "Failed to fetch product. Product likely doesn't exist"}, 400 + return {"msg": "Failed to fetch product. Product likely doesn't exist"}, 400 result_obj = {} @@ -91,7 +91,7 @@ class ProductService: return result_obj, 200 except Error as e: - return {"Failed": f"Failed to fetch product info. Error: {e}"}, 500 + return {"msg": f"Failed to fetch product info. Error: {e}"}, 500 @staticmethod def create_listing(seller_id: str, name: str, price: float): @@ -113,9 +113,9 @@ class ProductService: cursor.execute("insert into product(seller_id, name, price_pc) values (%s, %s, %s)", (seller_id, name, Decimal(str(rounded_price)))) db_connection.commit() except Error as e: - return {"Failed": f"Failed to create product. {e}"}, 400 + return {"msg": f"Failed to create product. {e}"}, 400 - return {"Success": "Successfully created new product listing"}, 200 + return {"msg": "Successfully created new product listing"}, 200 @staticmethod def __is_base64_jpg(decoded_string): diff --git a/app/services/user_service.py b/app/services/user_service.py index 0c56c22..10b23ca 100644 --- a/app/services/user_service.py +++ b/app/services/user_service.py @@ -42,16 +42,16 @@ class UserService: try: if not UserService.__verify_username(username): - return {"Failed": "Failed to verify username. Try another username"}, 400 + return {"msg": "Failed to verify username. Try another username"}, 400 if not UserService.__verify_displayname(displayname): - return {"Failed": "Failed to verify display name. Try another name"}, 400 + return {"msg": "Failed to verify display name. Try another name"}, 400 if not UserService.__verify_email(email): - return {"Failed": "Failed to verify email. Try another email"}, 400 + return {"msg": "Failed to verify email. Try another email"}, 400 if not UserService.__verify_password(password): - return {"Failed": "Failed to verify password. Try another (stronger) password"}, 400 + return {"msg": "Failed to verify password. Try another (stronger) password"}, 400 hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) @@ -60,11 +60,11 @@ class UserService: db_connection.commit() except Error as e: print(f"Error: {e}") - return {"Failed": "Failed to insert into database. Username or email are likely in use already"}, 500 + return {"msg": "Failed to insert into database. Username or email are likely in use already"}, 500 UserService.__send_email("register", email=email) - return {"Success": "User created successfully"}, 200 + return {"msg": "User created successfully"}, 200 @staticmethod def login(username: str, password: str) -> Tuple[Union[dict, str], int]: @@ -89,10 +89,10 @@ class UserService: password_hash = result['password'] if user_id is None: - return {"Failed": "Username not found"}, 400 + return {"msg": "Username not found"}, 400 if not bcrypt.checkpw(password.encode('utf-8'), password_hash.encode('utf-8')): - return {"Failed": "Incorrect password"}, 401 + return {"msg": "Incorrect password"}, 401 expire = datetime.timedelta(hours=1) @@ -103,7 +103,7 @@ class UserService: return {"token": token}, 200 except Error as e: - return {"Failed": f"Failed to login. Error: {e}"}, 500 + return {"msg": f"Failed to login. Error: {e}"}, 500 @staticmethod def logout(jwt_token, user_id) -> Tuple[Union[dict, str], int]: @@ -124,7 +124,7 @@ class UserService: UserService.__invalidate_token(jti, exp) UserService.__send_email("logout", id=user_id) - return {"Success": "Successfully logged out"}, 200 + return {"msg": "Successfully logged out"}, 200 @staticmethod def delete_user(user_id: str) -> Tuple[Union[dict, str], int]: @@ -144,9 +144,9 @@ class UserService: cursor.execute("delete from user where id = %s", (user_id,)) db_connection.commit() except Error as e: - return {"Failed": f"Failed to delete user. {e}"}, 500 + return {"msg": f"Failed to delete user. {e}"}, 500 - return {"Success": "User successfully deleted"}, 200 + return {"msg": "User successfully deleted"}, 200 @staticmethod def update_email(user_id: str, new_email: str) -> Tuple[Union[dict, str], int]: @@ -163,15 +163,15 @@ class UserService: try: if not UserService.__verify_email(new_email): - return {"Failed": "Failed to verify email. Try another email"}, 400 + return {"msg": "Failed to verify email. Try another email"}, 400 with db_connection.cursor() as cursor: cursor.execute("update user set email = %s where id = %s", (new_email, user_id)) db_connection.commit() except Error as e: - return {"Failed": f"Failed to update email. Email is likely in use already. Error: {e}"}, 500 + return {"msg": f"Failed to update email. Email is likely in use already. Error: {e}"}, 500 - return {"Success": "Email successfully updated"}, 200 + return {"msg": "Email successfully updated"}, 200 @staticmethod def update_username(user_id: str, new_username: str) -> Tuple[Union[dict, str], int]: @@ -188,15 +188,15 @@ class UserService: try: if not UserService.__verify_name(new_username): - return {"Failed": "Failed to verify username. Try another one"}, 400 + return {"msg": "Failed to verify username. Try another one"}, 400 with db_connection.cursor() as cursor: cursor.execute("update user set username = %s where id = %s", (new_username, user_id)) db_connection.commit() except Error as e: - return {"Failed": f"Failed to update username. Username is likely in use already. Error: {e}"}, 500 + return {"msg": f"Failed to update username. Username is likely in use already. Error: {e}"}, 500 - return {"Success": "Username successfully updated"}, 200 + return {"msg": "Username successfully updated"}, 200 @staticmethod def update_password(user_id: str, new_password: str) -> Tuple[Union[dict, str], int]: @@ -213,7 +213,7 @@ class UserService: try: if not UserService.__verify_password(new_password): - return {"Failed": "Failed to verify password. Try another (stronger) one"}, 400 + return {"msg": "Failed to verify password. Try another (stronger) one"}, 400 hashed_password = bcrypt.hashpw(new_password.encode('utf-8'), bcrypt.gensalt()) @@ -221,9 +221,9 @@ class UserService: cursor.execute("update user set password = %s where id = %s", (new_username, user_id)) db_connection.commit() except Error as e: - return {"Failed": f"Failed to update password. Error: {e}"}, 500 + return {"msg": f"Failed to update password. Error: {e}"}, 500 - return {"Success": "Password successfully updated"}, 200 + return {"msg": "Password successfully updated"}, 200 @staticmethod def __send_email(message: str, username: str = None, id: str = None, email: str = None): @@ -240,7 +240,7 @@ class UserService: send_mail(message, email) except Error as e: - return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500 + return {"msg": f"Failed to fetch some data. Error: {e}"}, 500 return if id is not None: @@ -252,7 +252,7 @@ class UserService: send_mail(message, email) except Error as e: - return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500 + return {"msg": f"Failed to fetch some data. Error: {e}"}, 500 return raise ValueError("Invalid input data to send mail")