예제 #1
0
def list_user_currencies(user_id: int) -> List[UserCurrencyResponseBody]:
    with create_session() as session:
        user = session.query(User).get(user_id)
        if not user:
            abort(404)
        return [
            UserCurrencyResponseBody.from_orm(currency)
            for currency in user.currencies
        ]
예제 #2
0
def list_transaction(query: TransactionListQuery) -> List[TransactionResponseBody]:
    with create_session() as session:
        transactions = (
            session.query(Transaction)
            .filter(Transaction.id > query.last_id)
            .limit(current_app.config['TRANSACTIONS_PER_PAGE'])
        )
        return [
            TransactionResponseBody.from_orm(transaction)
            for transaction in transactions
        ]
예제 #3
0
def update_currencies() -> None:
    with create_session() as session:
        currencies = session.query(Currency).all()
        for currency in currencies:
            currency.selling_rate += randint(-10,
                                             10) * currency.selling_rate / 100
            currency.buying_rate += randint(-10,
                                            10) * currency.buying_rate / 100
            currency.updated_at = datetime.utcnow()
            session.add(currency)
        session.commit()
        print('Currencies have been updated')
예제 #4
0
def create_transaction(body: CreateTransactionBody) -> TransactionResponseBody:
    with create_session() as session:
        # check if ids are valid
        user: User = session.query(User).get(body.user_id)
        if not user:
            abort(404, 'No user with such id')
        currency: Currency = session.query(Currency).get(body.currency_id)
        if not currency:
            abort(400, 'No currency with such id')

        if currency.updated_at != body.currency_updated_at:
            abort(400, 'Currency timestamp is outdated')

        # get user-currency association object
        uc = (
            session.query(UserCurrency)
            .filter(UserCurrency.currency == currency, UserCurrency.user == user)
            .first()
        )
        if not uc:
            uc = UserCurrency(user=user, currency=currency)
            session.add(uc)
            session.flush()

        if body.type == TransactionType.BUY:
            cost = currency.buying_rate * body.amount
            # check if user has enough money
            if cost > user.balance:
                abort(400, 'Not enough money')

            # update balance
            uc.amount += body.amount
            user.balance -= cost

        else:  # SELL
            profit = currency.selling_rate * body.amount
            # check if user has enough currency
            if body.amount > uc.amount:
                abort(400, 'Not enough currency')

            # update balance
            uc.amount -= body.amount
            user.balance += profit

        # create transaction
        transaction = Transaction(
            amount=body.amount, user=user, currency=currency, type=body.type
        )
        session.add_all([uc, user, transaction])
        session.flush()

        return TransactionResponseBody.from_orm(transaction)
예제 #5
0
def list_user_transactions(
        user_id: int,
        query: TransactionListQuery) -> List[TransactionResponseBody]:
    with create_session() as session:
        user = session.query(User).get(user_id)
        if not user:
            abort(404)
        transactions = (session.query(Transaction).filter(
            Transaction.user == user).filter(
                Transaction.id > query.last_id).limit(
                    current_app.config['TRANSACTIONS_PER_PAGE']))
        return [
            TransactionResponseBody.from_orm(transaction)
            for transaction in transactions
        ]
예제 #6
0
def get_user_currency(user_id: int,
                      currency_id: int) -> UserCurrencyResponseBody:
    with create_session() as session:
        user = session.query(User).get(user_id)
        if not user:
            abort(404)
        currency = session.query(Currency).get(currency_id)
        if not currency:
            abort(404)
        uc = (session.query(UserCurrency).filter(
            UserCurrency.currency == currency,
            UserCurrency.user == user).first())
        if uc:
            return UserCurrencyResponseBody.from_orm(uc)
        return UserCurrencyResponseBody(currency_id=currency_id)
예제 #7
0
def fill_database() -> None:
    with create_session() as session:
        user1 = User(balance=Decimal('5435.29'))
        user2 = User(balance=Decimal('1000.00'))
        currency1 = Currency(
            name='Bitcoin',
            selling_rate=Decimal('504.54'),
            buying_rate=Decimal('632.53'),
        )
        currency2 = Currency(
            name='Ethereum',
            selling_rate=Decimal('243.10'),
            buying_rate=Decimal('323.84'),
        )
        currency3 = Currency(
            name='Dogecoin',
            selling_rate=Decimal('653.09'),
            buying_rate=Decimal('542.43'),
        )
        currency4 = Currency(
            name='Litecoin',
            selling_rate=Decimal('103.16'),
            buying_rate=Decimal('134.64'),
        )
        currency5 = Currency(name='Ripple',
                             selling_rate=Decimal('375.28'),
                             buying_rate=Decimal('465.19'))
        user1_currency1 = UserCurrency(user=user1,
                                       currency=currency1,
                                       amount=Decimal('10.58'))
        user2_currency2 = UserCurrency(user=user2,
                                       currency=currency2,
                                       amount=Decimal('20.36'))

        session.add_all([
            user1,
            user2,
            currency1,
            currency2,
            currency3,
            currency4,
            currency5,
            user1_currency1,
            user2_currency2,
        ])
        session.flush()
예제 #8
0
def list_currencies() -> List[CurrencyResponseBody]:
    with create_session() as session:
        currencies = session.query(Currency).all()
        return [
            CurrencyResponseBody.from_orm(currency) for currency in currencies
        ]
예제 #9
0
def create_currency(body: CurrencyRequestBody) -> CurrencyResponseBody:
    with create_session() as session:
        currency = Currency(**body.dict())
        session.add(currency)
        session.flush()
        return CurrencyResponseBody.from_orm(currency)
예제 #10
0
def get_currency(currency_id: int) -> CurrencyResponseBody:
    with create_session() as session:
        currency = session.query(Currency).get(currency_id)
        if not currency:
            abort(404)
        return CurrencyResponseBody.from_orm(currency)
예제 #11
0
def get_transaction(transaction_id: int) -> TransactionResponseBody:
    with create_session() as session:
        transaction = session.query(Transaction).get(transaction_id)
        if not transaction:
            abort(404)
        return TransactionResponseBody.from_orm(transaction)
예제 #12
0
def list_users() -> List[UserResponseBody]:
    with create_session() as session:
        users = session.query(User).all()
        return [UserResponseBody.from_orm(user) for user in users]
예제 #13
0
def create_user() -> UserResponseBody:
    with create_session() as session:
        user = User()
        session.add(user)
        session.flush()
        return UserResponseBody.from_orm(user)
예제 #14
0
def get_user(user_id: int) -> UserResponseBody:
    with create_session() as session:
        user = session.query(User).get(user_id)
        if not user:
            abort(404)
        return UserResponseBody.from_orm(user)
예제 #15
0
def session(app):
    with create_session() as session:
        yield session