Exemplo n.º 1
0
def do_transaction(origin, target, operation):

    response = None

    try:

        emitedTransaction = Transaction(operation, OperationType.DEBIT)

        operation.origin_account.transactions.append(emitedTransaction)
        operation.origin_account.apply_transaction(emitedTransaction)

        receivedTransaction = Transaction(operation, OperationType.CREDIT)

        operation.target_account.transactions.append(receivedTransaction)
        operation.target_account.apply_transaction(receivedTransaction)

        db.commit()

        return emitedTransaction.toJSON()
    except DecreaceAmountError as err:
        db.rollback()
        app.logger.error('An error occurred: {0}'.format(err.message))
        raise DecreaceAmountError(operation)
    except Exception as err:
        db.rollback()
        app.logger.error('An error occurred: {0}'.format(err.message))
        raise Error()
    finally:
        pass

    return response
Exemplo n.º 2
0
def new_transaction(origin_id, target_id):

    app.logger.info('creating a transaction fron user: {0} to: {1}'.format(
        origin_id, target_id))

    response = None

    if not request.json:
        raise BadRequestError(request)
    else:
        try:
            origin = db.query(User).filter(User.id == origin_id).first()
            target = db.query(User).filter(User.id == target_id).first()

            origin_account = db.query(Account).filter(
                Account.id == request.json['origin_account']['id']).first()
            target_account = db.query(Account).filter(
                Account.id == request.json['target_account']['id']).first()

            operation_date = request.json['date']
            operation_date = datetime.strptime(operation_date,
                                               '%Y-%m-%dT%H:%M:%S.%fZ')

            operation = Operation(request.json['amount'], operation_date,
                                  origin_account, target_account)

            response = do_transaction(origin, target, operation)
        except Exception as err:
            db.rollback()
            app.logger.error('An error occurred: {0}'.format(err.message))
            raise Error(request=request)
    return jsonify(response)
Exemplo n.º 3
0
def create_account(user_id):

    app.logger.info('creating a account for a user')

    response = None

    if not request.json:
        raise BadRequestError(request)
    else:
        try:
            currency = db.query(Currency).filter(
                Currency.id == request.json['currency']['id']).first()
            account = Account.fromJson(request.json, currency)

            user = db.query(User).filter(User.id == user_id).first()
            user.accounts.append(account)

            db.add(user)
            db.commit()
            response = account.toJSON()
        except Exception as err:
            db.rollback()
            app.logger.error('An error occurred: {0}'.format(err.message))
            raise Error(request=request)
    return jsonify(response)
Exemplo n.º 4
0
def create_user():

    app.logger.info('creating a user')
    response = None

    if not request.json:
        raise BadRequestError(request)
    else:
        try:
            new_user = User(request.json['name'], request.json['email'])

            db.add(new_user)
            db.commit()
            response = new_user.toJSON()
        except Exception as err:
            db.rollback()
            app.logger.error('An error occurred: {0}'.format(err.message))
            raise Error(request=request)
    return jsonify(response)
def update_currency(currency_id):

    app.logger.info('updating a currency')
    response = None

    if not request.json:
        raise BadRequestError(request)
    else:
        try:
            currency = db.query(Currency).filter(
                Currency.id == currency_id).first()
            currency.name = request.json['name']
            currency.symbol = request.json['symbol']
            currency.enable = request.json['enable']

            db.commit()
            response = currency.toJSON()
        except Exception as err:
            db.rollback()
            app.logger.error('An error occurred: {0}'.format(err.message))
            raise Error(request=request)
    return jsonify(response)
Exemplo n.º 6
0
def update_user(user_id):

    app.logger.info('updating a user')
    response = None

    if not request.json:
        raise BadRequestError(request)
    else:
        try:
            user = db.query(User).filter(User.id == user_id).first()
            user.name = request.json['name']
            user.email = request.json['email']
            user.enable = request.json['enable']
            #user.__dict__.update(request.json)

            #db.add(user)
            db.commit()
            response = user.toJSON()
        except Exception as err:
            db.rollback()
            app.logger.error('An error occurred: {0}'.format(err.message))
            raise Error(request=request)
    return jsonify(response)
Exemplo n.º 7
0
def update_account(user_id, account_id):

    app.logger.info('updating a account for a user')

    response = None

    if not request.json:
        raise BadRequestError(request)
    else:
        try:
            account = db.query(Account).filter(
                Account.id == account_id).first()
            account.name = request.json['name']
            account.amount = request.json['amount']
            account.enable = request.json['enable']

            db.commit()
            response = account.toJSON()
        except Exception as err:
            db.rollback()
            app.logger.error('An error occurred: {0}'.format(err.message))
            raise Error(request=request)
    return jsonify(response)