Exemplo n.º 1
0
def save_user():
    """Saves the incoming user profile"""
    schema = User.schema

    validator = Validator(schema)

    if validator.validate(request.get_json()):
        user = User.from_dict(validator.document)

        try:
            session.add(user)
            session.commit()

            payload = user.to_dict()
            status = 200
        except IntegrityError:
            status = 400
            payload = {'username': ['Username already exists']}

    else:

        status = 400
        payload = validator.errors

    response = make_response(jsonify(payload), status)

    session.flush()
    session.close()

    return response
Exemplo n.º 2
0
def update_account(code):
    account = Account.get(code)

    if account is None:
        response = make_response(jsonify({'message': 'No Account was found'}),
                                 404)
        return response

    validator = Validator(Account.schema)

    payload = request.get_json()

    if not validator.validate(payload):
        response = make_response(jsonify(validator.errors), 400)
        return response

    account = Account.from_dict(validator.document, account)

    session.add(account)
    session.commit()
    session.flush()

    payload = account.to_dict()

    session.close()

    response = make_response(jsonify(payload), 200)

    return response
Exemplo n.º 3
0
def update_product(code):
    """Updates the Product"""

    payload = request.get_json()

    validator = Validator(Product.schema)

    if not validator.validate(payload):
        return make_response(jsonify(validator.errors), 400)

    product = Product.get(code)

    if product is None:
        return make_response(
            jsonify({'errors': ['No product was found with {}'.format(code)]}),
            404)

    product = Product.from_json(payload, product)

    session.add(product)

    session.commit()

    session.flush()

    return make_response(jsonify(product.to_json()), 200)
Exemplo n.º 4
0
def deliver_transaction(code):
    transaction = Transaction.get(code)

    transaction.delivered = True

    session.add(transaction)
    session.commit()
    session.flush()

    return make_response(jsonify(transaction.to_json()), 200)
Exemplo n.º 5
0
def deactivate_product(code):
    product = Product.get(code)

    if product is None:
        return make_response(
            jsonify({'errors': ['No product was found with {}'.format(code)]}),
            404)

    session.delete(product)

    session.commit()

    session.flush()

    return make_response(jsonify({'message': 'Product was deleted'}), 200)
Exemplo n.º 6
0
def delete_user(user_code, user):
    """Deletes the user"""
    if user is None:
        response = make_response(
            'No user found with code {}'.format(user_code), 404)
        return response

    session.delete(user)
    session.commit()
    session.flush()

    session.close()

    response = make_response('User was deleted', 200)

    return response
Exemplo n.º 7
0
def update_transaction(code):
    validator = Validator(Transaction.schema)

    payload = request.get_json()

    if not validator.validate(payload):
        return make_response(jsonify(validator.errors), 400)

    transaction = Transaction.get(code)

    transaction = Transaction.from_json(payload, transaction)

    session.add(transaction)
    session.commit()
    session.flush()

    return make_response(jsonify(transaction.to_json()), 200)
Exemplo n.º 8
0
def delete_account(code):
    account = Account.get(code)

    if account is None:
        response = make_response(jsonify({'message': 'No account was found'}),
                                 404)
        return response

    session.delete(account)

    session.commit()
    session.flush()

    session.close()

    response = make_response(
        jsonify({'message': 'Account {} was deleted'.format(code)}), 200)

    return response
Exemplo n.º 9
0
def register_product():
    """Registers the product in the database"""
    payload = request.get_json()

    validator = Validator(Product.schema)

    if not validator.validate(payload):
        return make_response(jsonify(validator.errors), 400)

    product = Product.from_json(payload)

    session.add(product)

    session.commit()

    session.flush()

    payload = product.to_json()

    return make_response(jsonify(payload), 201)
Exemplo n.º 10
0
def update_user(user_code, user):
    """Updates the user with the given code"""
    schema = User.schema

    validator = Validator(schema)

    if not validator.validate(request.get_json()):
        response = make_response(jsonify(validator.errors), 400)
        return response

    if user is None:
        response = make_response('No user found', 404)
        return response

    user = User.from_dict(validator.document, user)

    session.commit()
    session.flush()
    session.close()

    response = make_response('User was updated', 204)

    return response
Exemplo n.º 11
0
def save_account():
    payload = request.get_json()

    validator = Validator(Account.schema)

    if not validator.validate(payload):
        response = make_response(jsonify(validator.errors), 400)
        return response

    document = validator.document

    account = Account.from_dict(document)

    session.add(account)

    session.commit()
    session.flush()

    response = make_response(jsonify(account.to_dict()), 201)

    session.close()

    return response