def post(cls): data = cls.parser.parse_args() try: with dbCursor() as cursor: user = UserModel.find_by_username(cursor, data['username']) if user is None: return {'message': "Invalid credentials"}, 401 elif user.role == "banned": return {'message': "You are banned"}, 401 result = verifyHash_base64(data['password'], user.password_hash, user.salt) if result: claims = {'role': user.role} access_token = create_access_token(identity=user.id, user_claims=claims, fresh=True, expires_delta=False) #refresh_token = create_refresh_token(user.id) return {'access_token': access_token, 'user': user.json()}, 200 return {'message': "Invalid credentials"}, 401 except mysql.connector.Error as e: raise InternalServerError(e) except Exception as e: raise InternalServerError(e)
def get(cls): try: with dbCursor() as cursor: age_categories = GameModel.get_age_categories(cursor) return {"age_categories": age_categories} except Exception as e: raise InternalServerError(e)
def post(cls): data = cls.parser.parse_args() key_str = data['key'] game_id = data['game_id'] try: with dbCursor() as cursor: game = GameModel.find_by_id(cursor, game_id) if game is None: return { 'message': "Error when appending a key, game id not found" }, 404 if game.is_digital == False: return { 'message': "Error when appending a key, game of id {game_id} is not digital" .format(game_id=game_id) }, 400 key = KeyModel.find_by_key(cursor, game_id, key_str) if key: return { 'message': "Error when appending a key, already in database", "key": key.json() }, 409 key = KeyModel(game_id, key_str) key.save_to_db(cursor) return {'message': 'Key added sucessfully.'}, 201 except Exception as e: raise InternalServerError(e)
def put(cls): data = cls.parser.parse_args() user_id = data['user_id'] new_username = data['newusername'] new_pass = data['newpass'] new_email = data['newemail'] new_role = data['newrole'] try: with dbCursor() as cursor: user = UserModel.find_by_id(cursor, user_id) if user: if new_username: user.username = new_username if new_pass: password_hash, salt = encrypt_base64(new_pass) user.password_hash = password_hash user.salt = salt if new_role: user.role = new_role if new_email: user.email = new_email if new_role: user.role = new_role user.update(cursor) return {"message": "Password changed succesfully"}, 200 else: return {"message": "User doesnt exist"}, 401 except Exception as e: raise InternalServerError(e)
def get(cls): try: with dbCursor() as cursor: all_users = UserModel.find_all(cursor) return {'users': [user.json() for user in all_users]} except Exception as e: raise InternalServerError(e)
def get(cls): try: with dbCursor() as cursor: categories = CategoryModel.find_all(cursor) return { 'categories': [category.json() for category in categories] } except Exception as e: raise InternalServerError(e)
def get(cls): try: with dbCursor() as cursor: platforms = PlatformModel.find_all(cursor) return { 'platforms': [platform.json() for platform in platforms] } except Exception as e: raise InternalServerError(e)
def post(cls): data = cls.parser.parse_args() try: with dbCursor() as cursor: if UserModel.find_by_username(cursor, data["username"]): return {"message": "User already exists"}, 409 if UserModel.find_by_email(cursor, data["email"]): return {"message": "This email is already taken"}, 409 password_hash, salt = encrypt_base64(data['password']) role = 'admin' user = UserModel(data['username'], data['email'], role, password_hash, salt) user.save_to_db(cursor) except mysql.connector.Error as e: raise InternalServerError(e) except Exception as e: raise InternalServerError(e) return {'message': 'Admin created successfully.'}, 201
def post(cls): data = cls.parser.parse_args() try: #create and and save GameModel now = datetime.utcnow() data['release_date'] = now.strftime('%Y-%m-%d %H:%M:%S') categories = data.pop('categories') with dbCursor() as cursor: for category_id in categories: category = CategoryModel.find_by_id(cursor, category_id) if category is None: return { 'message': 'Category with id {id} not found'.format( id=category_id) }, 404 platform_id = data['platform_id'] platform = PlatformModel.find_by_id(cursor, platform_id) if platform is None: return { 'message': 'Platform with id {id} not found'.format( id=platform_id) }, 404 game = GameModel(**data) game.save_to_db(cursor) #create categories for the game.id for category_id in categories: game_category = GameCategoryModel(game.id, category_id) game_category.save_to_db(cursor) except mysql.connector.Error as e: raise InternalServerError(e) except ValueError as e: raise BadRequestError() except Exception as e: raise InternalServerError(e) return {'message': 'Game added successfully.'}, 201
def get(cls, game_id): try: with dbCursor() as cursor: game = GameModel.find_by_id(cursor, game_id) if game is None: return { "message": "Game of id {_id} not found.".format(_id=game_id) }, 404 game_json = game.json(cursor) return {"game": game_json} except Exception as e: raise InternalServerError(e)
def put(cls): data = cls.parser.parse_args() try: with dbCursor() as cursor: user = UserModel.find_by_id(cursor, data['user_id']) if user: user.role = "banned" user.update(cursor) return {"message": "User banned succesfully"}, 200 else: return {"message": "User doesnt exist"}, 401 except Exception as e: raise InternalServerError(e)
def post(cls): data = cls.parser.parse_args() try: with dbCursor() as cursor: #verify data if len(data['shopping_cart']) == 0: return {"message": "Shopping cannot be empty"}, 400 for entry in data['shopping_cart']: game_id = entry['game_id'] quantity = entry['quantity'] game = GameModel.find_by_id(cursor, game_id) if game is None: return { "message": "Game of id {id} not found.".format(id=game_id) }, 404 quantity_in_db = game.get_quantity(cursor) if quantity_in_db < quantity: return { "message": "Game of id {id} is available only in {qua} pieces." .format(id=game_id, qua=quantity_in_db) }, 404 #data is partially verified, now some bad asynch stuff might happen user_id = get_jwt_identity() date = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S') userTransaction = UserTransactionModel(user_id, date) userTransaction.save_to_db(cursor) user_transaction_id = userTransaction.id # this is not user_id, TODO: change its name for less ambigous for entry in data['shopping_cart']: game_id = entry['game_id'] quantity = entry['quantity'] game = GameModel.find_by_id(cursor, game_id) keyId = None for i in range(0, quantity): if game.is_digital: key = KeyModel.find_any_not_used(cursor, game_id) keyId = key.id gameTransaction = GameTransactionModel( user_transaction_id, game_id, keyId) gameTransaction.save_to_db(cursor) return {'message': 'Games purchase successful'}, 201 except Exception as e: raise InternalServerError(e)
def get(cls): data = cls.parser.parse_args() game_id = data['game_id'] try: with dbCursor() as cursor: game = GameModel.find_by_id(cursor, game_id) if game: keys = KeyModel.find_all_by_game_id(cursor, game_id) return {'keys': [key.json() for key in keys]} else: return {'message': "Error, incorrect game id"}, 404 except Exception as e: raise InternalServerError(e)
def put(cls): data = cls.parser.parse_args() try: with dbCursor() as cursor: new_email = data['newemail'] user_id = get_jwt_identity() user = UserModel.find_by_id(cursor, user_id) if user: user.email = new_email user.update(cursor) return {"message": "Email changed succesfully"}, 200 except Exception as e: raise InternalServerError(e)
def delete(cls, name): try: with dbCursor() as cursor: platform = PlatformModel.find_by_name(cursor, name) if platform: platform = PlatformModel(name) platform.delete_from_db(cursor) else: return { 'message': "Error when deleting a platform {n}, platform doesnt exist" .format(n=name) }, 404 return {'message': 'Category deleted sucessfully.'}, 201 except Exception as e: raise InternalServerError(e)
def put(cls, name): try: with dbCursor() as cursor: platform = PlatformModel.find_by_name(cursor, name) if platform: return { 'message': "Error when appending a platform {n}, already in database" .format(n=name) }, 409 else: platform = PlatformModel(name) platform.save_to_db(cursor) return {'message': 'Platform added sucessfully.'}, 201 except Exception as e: raise InternalServerError(e)
def put(cls, name): try: with dbCursor() as cursor: category = CategoryModel.find_by_name(cursor, name) if category: return { 'message': "Error when appending a category {n}, already in database" .format(n=name) }, 409 else: category = CategoryModel(name) category.save_to_db(cursor) return {'message': 'Category added sucessfully.'}, 201 except Exception as e: raise InternalServerError(e)
def get(cls): try: with dbCursor() as cursor: all_user_transactions = UserTransactionModel.find_all(cursor) results = [] for user_tr in all_user_transactions: user = UserModel.find_by_id(cursor, user_tr.user_id).json() res = [] game_transactions = GameTransactionModel.find_by_user_transaction_id( cursor, user_tr.id) for game_tr in game_transactions: res.append(game_tr.json_adv(cursor)) result = user_tr.json() result['username'] = user['username'] result['games_transactions'] = res results.append(result) return {"transactions": results} except Exception as e: raise InternalServerError(e)
def post(cls): data = cls.parser.parse_args() try: if 'search_filter' not in data or data['search_filter'] is None: return {'message': 'Search filter cannot be empty'}, 400 data = data['search_filter'] RESULTS_PER_PAGE = 12 with dbCursor() as cursor: page_number = data.get('page_number') name = data.get( 'name' ) #sets 'name' value with None is 'name' is not present in data categories_id = data.get('categories_id') platforms_id = data.get('platforms_id') order_by = data.get('order_by') order_rule = data.get('order_rule') digital = data.get('digital') games = GameModel.find_many_by_filter(cursor, RESULTS_PER_PAGE, page_number, name, categories_id, platforms_id, order_by, order_rule, digital) #get entry count using SQL_CALC_FOUND_ROWS cursor.execute("SELECT FOUND_ROWS()") (entry_count, ) = cursor.fetchone() games_json = [game.json(cursor) for game in games] return { 'total_number': entry_count, 'returned_number': len(games), 'results_per_page': RESULTS_PER_PAGE, 'games': games_json }, 200 except Exception as e: raise InternalServerError(e)
def get(cls): try: with dbCursor() as cursor: user_id = get_jwt_identity() user_transactions = UserTransactionModel.find_by_user_id( cursor, user_id) print(user_transactions) results = [] for user_tr in user_transactions: res = [] game_transactions = GameTransactionModel.find_by_user_transaction_id( cursor, user_tr.id) for game_tr in game_transactions: res.append(game_tr.json_adv(cursor)) result = user_tr.json() result['games_transactions'] = res results.append(result) return {"transactions": results} except Exception as e: raise InternalServerError(e)
def put(cls): data = cls.parser.parse_args() try: with dbCursor() as cursor: user_id = get_jwt_identity() user = UserModel.find_by_id(cursor, user_id) result = verifyHash_base64(data['oldpass'], user.password_hash, user.salt) if not result: return { "message": "Error when changing password, invalid credientials" }, 401 if result: password_hash, salt = encrypt_base64(data['newpass']) user.password_hash = password_hash user.salt = salt user.update(cursor) return {"message": "Password changed succesfully"}, 200 except Exception as e: raise InternalServerError(e)
def post(cls): data = cls.parser.parse_args() new_name = data["name"] new_price = data["price"] new_quantity = data["quantity"] new_descr = data["description"] new_rel_date = data["release_date"] new_is_digital = data["is_digital"] new_platform = data["platform_id"] new_age = data["age_category"] new_categories = data["categories"] game_id = data["game_id"] try: # create and and save GameModel now = datetime.utcnow() new_rel_date = now.strftime('%Y-%m-%d %H:%M:%S') with dbCursor() as cursor: game = GameModel.find_by_id(cursor, game_id) if game: if new_name: game.name = new_name if new_price: game.price = new_price if new_descr: game.description = new_descr if new_descr: game.description = new_descr if new_rel_date: game.release_date = new_rel_date if new_is_digital is not None: if KeyModel.find_all_by_game_id( cursor, game_id) and new_is_digital == 0: return { 'message': 'Cant change to box, keys for the game exists' }, 404 else: game.is_digital = new_is_digital if new_quantity: if game.is_digital: return { 'message': 'Cant change quantity of digital game' }, 404 else: game.quantity = new_quantity if new_platform: if PlatformModel.find_by_id(cursor, new_platform) is not None: game.platform_id = new_platform else: return {'message': 'Platform doesnt exist'}, 404 if new_age: game.age_category = new_age if new_categories: for category_id in new_categories: category = CategoryModel.find_by_id( cursor, category_id) if category is None: return { 'message': 'Category with id {id} not found'.format( id=category_id) }, 404 GameCategoryModel.delete_by_game_id(cursor, game_id) for category_id in new_categories: game_category = GameCategoryModel( game_id, category_id) game_category.save_to_db(cursor) game.update(cursor) else: return { 'message': 'Game with id {id} not found'.format(id=game_id) }, 404 except mysql.connector.Error as e: raise InternalServerError(e) except ValueError as e: raise BadRequestError() except Exception as e: raise InternalServerError(e) return {'message': 'Game edited successfully.'}, 201