Exemple #1
0
def display_balance(user_id):
    user = User.query.filter_by(id=user_id).first()
    if user:
        user_schema = UserSchema()
        response = user_schema.dump(user.balance).data
        return jsonify(response)
    else:
        abort(404, description="User does not have an account yet")
Exemple #2
0
def update_user(user_id):
    data = request.get_json(force=True)
    new_name = data['name']
    new_balance = data['balance']
    new_pin = data['pin']
    user = User.query.get_or_404(user_id)
    user.name = new_name
    user.pin = new_pin
    user.balance = new_balance
    db.session.commit()
    user_schema = UserSchema()
    response = user_schema.dump(user).data   
    return jsonify(response)
Exemple #3
0
def deposit_money(user_id):
    data = request.get_json(force=True)
    amount = data['amount']
    pin = data['pin']
    if amount > 3000:
        abort(400, description="You cannot deposit more than the 3000 Euro a day.")
    else:
        user = User.query.get_or_404(user_id, "User does not exist.")
        if pin == int(user.pin):
            user.balance += amount
            db.session.commit()
            user_schema = UserSchema()
            response = user_schema.dump(user).data
            return jsonify(response)
        else:
            abort(400, description="The pin was incorrect.")
Exemple #4
0
def withdraw(user_id):
    data = request.get_json(force=True)
    amount = data["amount"]
    pin = data["pin"]
    if amount > 2000:
        abort(400, description='You are not allowed to go over 2000 euro daily limit') 
    else:
        user = User.query.get_or_404(user_id, "User does not exist")
        if pin == int(user.pin): 
            if amount <= user.balance:
                user.balance -= amount
                db.session.commit()
                user_schema = UserSchema()
                response = user_schema.dump(user).data
                return jsonify(response)       
            else:
                abort(400, description="You are not allowed to withdraw more money than you have on your account!")
        else:
            abort(400, description="Pin is not correct")
Exemple #5
0
def create_user():
    data = request.get_json(force=True)
    new_name = data['name']
    new_pin = int(data['pin'])
    new_balance = int(data['balance'])
    if not new_name and new_pin and new_balance:
        abort(400, description="Bad request")
    else:
        # check if user already exists
        user = User.query.filter_by(name=new_name).first()
        if user:
            abort(400, description="User already exists.")
        else:
            new_user = User(name=new_name, pin=new_pin, balance=new_balance)
            db.session.add(new_user)
            db.session.commit()
            user_schema = UserSchema()
            response = user_schema.dump(new_user).data    #to serialize we use Marshmallow library
            return jsonify(response)
Exemple #6
0
def transfer_money(user_id):
    data = request.get_json(force=True)
    amount = data['amount']
    pin = data['pin']
    receiver_id = data['receiver_id']
    sender = User.query.get_or_404(user_id, description="Account holder does not exist.")
    if pin == int(sender.pin):
        if amount <= sender.balance:
            receiver = User.query.get_or_404(receiver_id, "Receiver does not exist.")
            sender.balance -= amount
            receiver.balance += amount
            db.session.commit()
            user_schema = UserSchema()
            response_sender = user_schema.dump(sender).data
            response_receiver = user_schema.dump(receiver).data
            return jsonify(response_sender, response_receiver)
        else:
            abort(400, description="You are not allowed to withdraw more money than you have in your bank account.")
    else:
        abort(400, description="The pin was incorrect.")
Exemple #7
0
def user_info(user_id):
    user = User.query.get_or_404(user_id)
    user_schema = UserSchema()   #
    response = user_schema.dump(user).data
    return jsonify(response)
Exemple #8
0
def get_users():
    user_schema = UserSchema(many=True) #expects a list with all the users (many=True)
    users = User.query.all()
    response = user_schema.dump(users).data
    return  jsonify(response)