def get_currency_by_id(currency_id: int) -> AbstractSerialize: with create_session() as session: res: Currency = session.query(Currency).filter( Currency.id == currency_id).first() if res is None: raise CurrenciesDALException('Currency not exists') return serialize(res)
def get_user_operations_history( user_id: int, operation_type: Optional[OperationType] = None, size: Optional[int] = None, page: Optional[int] = None, ) -> List[AbstractSerialize]: with create_session() as session: check_user_existence_and_get_if_exists(user_id, session) operations_query: Query = session.query(Operation).filter( Operation.user_id == user_id) if operation_type is not None: operations = operations_query.filter( Operation.operation_type == operation_type).all() else: operations = operations_query.all() if operations is None: raise UsersDALException( 'Cannot find operations with this parameters') if size is None and page is not None or size is not None and page is None: raise PaginationError('size or page not stated') if size is not None and page is not None: operations = MyPagination.get_pagination( operations, page, size) res = [] for operation in operations: res.append(serialize(operation)) return res
def add_currency(name: str, selling_price: Decimal, purchasing_price: Decimal) -> AbstractSerialize: with create_session() as session: if selling_price <= 0 or purchasing_price <= 0: raise CurrenciesDALException( 'Selling or purchasing price less or equal to zero') if (session.query(Currency).filter(Currency.name == name).first() is not None): raise CurrenciesDALException( 'Currency with this name already exists') currency = Currency( name=name, selling_price=selling_price, purchasing_price=purchasing_price, modified_at=datetime.now(), ) session.add(currency) session.flush() currency_history = CurrencyHistory( currency_id=currency.id, time=currency.modified_at, purchasing_price=purchasing_price, selling_price=selling_price, ) session.add(currency_history) return serialize(currency)
def get_currency(user_id: int, currency_id: int) -> AbstractSerialize: with create_session() as session: res = session.query(UserCurrency).filter( UserCurrency.user_id == user_id, UserCurrency.currency_id == currency_id).first() if not res: raise UsersDALException() return serialize(res)
def get_user_by_name(user_name: str) -> AbstractSerialize: with create_session() as session: try: user = session.query(User).filter( User.login == user_name).one() return serialize(user) except NoResultFound: raise UsersDALException()
def add_user(login: str) -> AbstractSerialize: with create_session() as session: if session.query(User).filter(User.login == login).first() is None: u = User(login=login, money=settings.start_money) session.add(u) session.flush() return serialize(u) raise UsersDALException(Message.USER_ALREADY_CREATED.value)
def get_user_currencies(user_id: int) -> List[AbstractSerialize]: res: List[AbstractSerialize] = [] with create_session() as session: check_user_existence_and_get_if_exists(user_id, session) user_currencies = (session.query(UserCurrency).filter( UserCurrency.user_id == user_id).all()) for user_currency in user_currencies: res.append(serialize(user_currency)) return res
def get_currency_history(currency_id: int) -> List[AbstractSerialize]: with create_session() as session: currency = (session.query(Currency).filter( Currency.id == currency_id).first()) if currency is None: raise CurrenciesDALException('Currency not exists') res: List[AbstractSerialize] = [] for currency_history in (session.query(CurrencyHistory).filter( CurrencyHistory.currency_id == currency_id).all()): res.append(serialize(currency_history)) return res
def make_operation_with_currency( user_id: int, currency_id: int, operation: OperationType, amount: Decimal, time: datetime, ) -> AbstractSerialize: with create_session() as session: currency: Currency = session.query(Currency).filter( Currency.id == currency_id).first() if currency is None: raise UsersDALException('currency does not exist') user = check_user_existence_and_get_if_exists(user_id, session) check_course_changes(currency.modified_at, time) user_currency: UserCurrency = session.query(UserCurrency).filter( UserCurrency.user_id == user_id, UserCurrency.currency_id == currency_id).first() if amount <= 0: raise UsersDALException('Invalid amount') os = OperationStrategy() if operation == OperationType.BUY: os.set_strategy(Buy()) else: os.set_strategy(Sell()) user_currency = os.invoke(currency, amount, user, user_currency) session.add(user_currency) session.flush() copy = deepcopy(user_currency) if user_currency.amount == 0: session.delete(user_currency) operation = Operation( user_id=user_id, currency_id=currency_id, operation_type=operation, amount=amount, time=datetime.now(), ) session.add(operation) return serialize(copy)
def get_user_by_id(user_id: int) -> AbstractSerialize: with create_session() as session: user = check_user_existence_and_get_if_exists(user_id, session) return serialize(user)
def test_serializer_with_invalid_target_class(): with pytest.raises(TypeError): serialize(object())
def get_currencies() -> List[AbstractSerialize]: currencies: List[AbstractSerialize] = [] with create_session() as session: for currency in session.query(Currency): currencies.append(serialize(currency)) return currencies