def post(self): user_dict = get_jwt_identity() user = UserModel.find_by_username(user_dict['username']) item_id = purchase_parser.parse_args()['id'] item = ShopItemModel.find_item_by_id(item_id) if item: new_transaction = TransactionModel( sender_id=user.id, receiver_id=0, amount=item.price, date=datetime.datetime.now(), transaction_type='Purchase' ) if user.current_balance < item.price: return {'message': 'Not enough money'}, 400 try: item.purchase_item(user) user.change_balance(user.current_balance - item.price) new_transaction.save_to_db() return {'message': 'You have successfully bought {}'.format(item.name)}, 200 except: return {'message': 'Something went wrong'}, 500 return {'message': 'Item not found'}, 400
def post(self): data = transaction_parser.parse_args() sender = UserModel.find_by_username(get_jwt_identity()['username']) receiver = UserModel.find_by_username(data['receiver_username']) if not receiver: return {'message': 'Receiver does not exist'}, 500 sender_id = sender.id receiver_id = receiver.id amount = data['amount'] new_transaction = TransactionModel( sender_id=sender_id, receiver_id=receiver_id, amount=amount, date=datetime.datetime.now(), transaction_type='Transfer' ) try: if amount <= 0: return {'message': 'Amount is less or equal to zero'}, 400 if sender_id == receiver_id: return {'message': 'Sender == Receiver'}, 400 if sender.current_balance < amount: return {'message': 'Sender does not have enough unicoins'}, 400 sender.change_balance(sender.current_balance - amount) new_transaction.save_to_db() receiver.change_balance(receiver.current_balance + amount) return { 'message': 'Transaction from {0} to {1}: {2} unicoins'.format(sender.username, receiver.username, amount) }, 200 except: return {'message': 'Something went wrong'}, 500