def authenticate(endpoint, headers): reestricted_endpoints = [ 'get_user', 'get_users', 'update_user', 'delete_user', 'add_game', 'update_game', 'delete_game', 'add_order', 'get_order_by_order_number', 'get_order_by_game_id', 'get_order_by_user', 'get_orders', 'update_order' ] admin_endpoints = [ 'get_users', 'delete_user', 'add_game', 'update_game', 'delete_game', 'get_order_by_order_number', 'get_order_by_game_id', 'get_orders', 'update_order' ] if endpoint not in reestricted_endpoints: return MessageService.generate_success_message('', {}) try: token = extract_token_from_header(headers['Authorization']) data = decode_token(token) if endpoint in admin_endpoints: is_admin = __is_user_admin(data['id']) if not is_admin: return MessageService.lack_of_privilege except (jwt.ExpiredSignatureError, jwt.InvalidTokenError, KeyError): return MessageService.authentication_required except Exception: return MessageService.generate_internal_server_error( 'An error has ocurred') return MessageService.generate_success_message('', {})
def get_all_transactions(): try: orders = TransactionDao.get_transactions() if len(orders): return MessageService.generate_success_message('', orders) else: return MessageService.generate_custom_message( 'No orders were found', []) except Exception as e: return MessageService.generate_internal_server_error(e)
def get_game(name): try: game = retrieve_game(name) if game: return MessageService.generate_success_message( '', game.to_dictionary()) else: return MessageService.generate_custom_message( 'The game was not found', {}) except Exception as e: return MessageService.generate_internal_server_error(e)
def get_transactions_by_game_id(game_id): try: orders = TransactionDao.retrieve_transactions_by_game_id(game_id) if len(orders): return MessageService.generate_success_message( '', process_transactions_projection()) else: return MessageService.generate_custom_message( 'No orders were found', []) except Exception as e: return MessageService.generate_internal_server_error(e)
def get_transactions_by_user(token): try: user_data = AuthService.decode_token(token) transactions = TransactionDao.retrieve_transactions_by_user( user_data['id']) if not len(transactions): return MessageService.generate_custom_message( 'No orders were found', []) return MessageService.generate_success_message('', transactions) except Exception as e: return MessageService.generate_internal_server_error(e)
def get_transaction_by_order_number(order_number): try: order = TransactionDao.retrieve_transaction_by_order_number( order_number) if order: return MessageService.generate_success_message( '', order.to_dictionary()) else: return MessageService.generate_custom_message( 'No order with that order number could be found', {}) except Exception as e: return MessageService.generate_internal_server_error(e)
def remove_user(payload): try: if check_fields_existance_in_payload(payload, 'id'): user = UserDao.delete_user(payload['id']) if user: return MessageService.generate_success_message('', user.to_dictionary()) else: return MessageService.generate_custom_message('No user was found', {}) else: return MessageService.missing_fields_request except Exception as e: return MessageService.generate_internal_server_error(e)
def get_all_users(): try: result = UserDao.get_users() response = [] for user in result: response.append(user.to_dictionary()) if len(response): return MessageService.generate_success_message('', response) else: return MessageService.generate_custom_message('No users were found', {}) except Exception as e: return MessageService.generate_internal_server_error(e)
def get_all_games(name): try: result = get_games(name) games = [] for game in result: games.append(game.to_dictionary()) if len(games): return MessageService.generate_success_message('', games) else: return MessageService.generate_custom_message( 'No games were found', []) except Exception as e: return MessageService.generate_internal_server_error(e)
def modify_transaction_status(payload): if not check_fields_existance_in_payload(payload, 'status', 'order_number'): return MessageService.missing_fields_request try: order = TransactionDao.update_transaction(payload) if order: return MessageService.generate_success_message( '', order.to_dictionary()) else: return MessageService.generate_custom_message( 'the order could not be updated', {}) except Exception as e: return MessageService.generate_internal_server_error(e)
def add_order(payload): try: if check_fields_existance_in_payload(payload, 'user_id', 'game_id', 'total'): order = TransactionDao.create_order(payload) if order: return MessageService.generate_success_message('success', {}) else: return MessageService.generate_custom_message( 'The order could not be created', 500, {}) else: return MessageService.missing_fields_request except Exception as e: return MessageService.generate_internal_server_error(e)
def remove_game(payload): try: if check_fields_existance_in_payload(payload, 'id'): game = delete_game(payload['id']) if game: return MessageService.generate_success_message( '', game.to_dictionary()) else: return MessageService.generate_custom_message( 'The game could not be removed', {}) else: return MessageService.missing_fields_request except Exception as e: return MessageService.generate_internal_server_error(e)
def login(payload): if not check_fields_existance_in_payload(payload, 'email', 'password'): return MessageService.missing_fields_request try: result = UserDao.login_user(payload) if not result: return MessageService.authentication_failed token = AuthService.generate_token(result) data = {'token': 'Bearer ' + token} return MessageService.generate_success_message('', data) except Exception as e: return MessageService.generate_internal_server_error(e)
def add_user(payload): try: fields_exist = check_fields_existance_in_payload(payload, 'first_name', 'email', 'gamertag', 'password') if fields_exist: __set_optional_fields(payload, 'last_name', 'address', 'profile_picture', 'role') user = UserDao.create_user(payload) if user: return MessageService.generate_success_message('User created', user.to_dictionary()) else: return MessageService.generate_custom_message('The user could not be added', 500) else: return MessageService.missing_fields_request except Exception as e: return MessageService.generate_internal_server_error(e)
def get_user(gamertag, token): try: data = AuthService.decode_token(token) if data['role'] == 'admin': gamertag_to_look = gamertag else: gamertag_to_look = data['gamertag'] data = UserDao.retrieve_user(gamertag_to_look) if data: return MessageService.generate_success_message('', data.to_dictionary()) else: return MessageService.generate_custom_message('No user was found with that gamertag', {}) except Exception as e: return MessageService.generate_internal_server_error(e)
def modify_game(payload): try: if check_fields_existance_in_payload(payload, 'id', 'name', 'developer', 'publisher', 'description', 'price'): game = update_game(payload) if game: return MessageService.generate_success_message( '', game.to_dictionary()) else: return MessageService.generate_custom_message( 'No game with that id was found', {}) else: return MessageService.missing_fields_request except Exception as e: MessageService.generate_internal_server_error(e)
def add_game(payload): try: if check_fields_existance_in_payload(payload, 'name', 'developer', 'publisher', 'price', 'description', 'image'): game = create_game(payload) if game: return MessageService.generate_success_message( '', game.to_dictionary()) else: return MessageService.generate_custom_message( 'The game could not be created', 500) else: return MessageService.missing_fields_request except Exception as e: return MessageService.generate_internal_server_error(e)
def modify_user(payload): try: result = UserDao.check_email_gamertag_duplication(payload['id'], payload['email'], payload['gamertag']) if result: return MessageService.user_info_duplication if check_fields_existance_in_payload(payload, 'id', 'first_name', 'last_name', 'email', 'address', 'gamertag', 'profile_picture'): user = UserDao.update_user(payload) if user: return MessageService.generate_success_message('', user.to_dictionary()) else: MessageService.generate_custom_message('No user was found', {}) else: return MessageService.missing_fields_request except Exception as e: return MessageService.generate_internal_server_error(e)