async def create_user(username: str, password: str, base_currency: str): hashed_password = pwd_context.hash(password) query = users.insert().values( user_id=create_random_id(), username=username, password=hashed_password, base_currency=base_currency, ) user_id = await database.execute(query) print(f"created user {username} with {user_id} id")
async def create_stock_alert( new_stock_alert: NewStockAlert, user: User = Depends(get_current_user), ): await get_owner(user.user_id, new_stock_alert.owner_id) if (new_stock_alert.lower_limit_price is None and new_stock_alert.upper_limit_price is None and new_stock_alert.dividend_date is None and new_stock_alert.fiscal_price_lower_than is None and new_stock_alert.fiscal_price_greater_than is None and new_stock_alert.profit_and_loss_greater_than is None): raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="At least one field must be present", ) query = (select([ stock_alerts.c.stock_alert_id, stock_alerts.c.owner_id ]).select_from( stocks.outerjoin(stock_alerts, stocks.c.stock_id == stock_alerts.c.stock_id)).where( stocks.c.stock_id == new_stock_alert.stock_id)) records = await database.fetch_all(query) if not records: raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=f"Stock {new_stock_alert.stock_id} doesn't exist", ) for record in records: if record[0] and record[1] == new_stock_alert.owner_id: raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="Stock alert for stock " "{new_stock_alert.stock_id} already exists", ) query = stock_alerts.insert().values( stock_alert_id=create_random_id(), stock_id=new_stock_alert.stock_id, owner_id=new_stock_alert.owner_id, lower_limit_price=new_stock_alert.lower_limit_price, upper_limit_price=new_stock_alert.upper_limit_price, dividend_date=new_stock_alert.dividend_date, fiscal_price_lower_than=new_stock_alert.fiscal_price_lower_than, fiscal_price_greater_than=new_stock_alert.fiscal_price_greater_than, profit_and_loss_lower_limit=new_stock_alert. profit_and_loss_lower_limit, profit_and_loss_upper_limit=new_stock_alert. profit_and_loss_upper_limit, ) await database.execute(query) return await get_alert_or_raise(new_stock_alert.stock_id, new_stock_alert.owner_id)
async def create_account(new_account: NewAccount, user: User = Depends(get_current_user)): query = accounts.insert().values( account_id=create_random_id(), account_number=new_account.account_number, bank=new_account.bank, user_id=user.user_id, ) account_id = await database.execute(query) account = new_account.dict() account["account_id"] = account_id account["bank_name"] = BANK_NAMES[new_account.bank] account["owners"] = [] account["current_ctv_converted"] = 0 return account
async def create_stock_transaction( owner_id: int, new_stock_transaction: NewStockTransaction, user: User = Depends(get_current_user), ): await get_owner(user.user_id, owner_id) query = stocks.select().where( stocks.c.stock_id == new_stock_transaction.stock_id) record = await database.fetch_one(query) if record is None: raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=f"Stock id {new_stock_transaction.stock_id} doesn't exist", ) # added validation, first transaction has to be a buy # and when you have a sell the quantity must be less than the total held # (query the stock_transactions table filtering it by owner_id and stock_id # than if the list is empty just check that the request body transaction_type is buy # if it is not empty and the transaction_type is sell sum the transactions # quantities, taking in account sells and buys, # and check that the sum is equal or greater than request body quantity) query = (stock_transactions.select().where( stock_transactions.c.owner_id == owner_id).where( stock_transactions.c.stock_id == new_stock_transaction.stock_id)) # fetch_all returns a list of records that match the query records = await database.fetch_all(query) validate_stock_transaction(records, new_stock_transaction) exchange_rate = 1 if new_stock_transaction.transaction_ex_rate is not None: exchange_rate = new_stock_transaction.transaction_ex_rate query = stock_transactions.insert().values( stock_transaction_id=create_random_id(), stock_id=new_stock_transaction.stock_id, price=new_stock_transaction.price, quantity=new_stock_transaction.quantity, tax=new_stock_transaction.tax, commission=new_stock_transaction.commission, transaction_type=new_stock_transaction.transaction_type, date=new_stock_transaction.date, owner_id=owner_id, transaction_note=new_stock_transaction.transaction_note, transaction_ex_rate=exchange_rate, ) stock_transaction_id = await database.execute(query) stock_transaction = new_stock_transaction.dict() stock_transaction["stock_transaction_id"] = stock_transaction_id return stock_transaction
async def create_owner(new_owner: NewOwner, user: User = Depends(get_current_user)): query = (accounts.select().where(accounts.c.user_id == user.user_id).where( accounts.c.account_id == new_owner.account_id)) record = await database.fetch_one(query) if record is None: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"Account {new_owner.account_id} doesn't exist", ) query = owners.insert().values( owner_id=create_random_id(), account_id=new_owner.account_id, fullname=new_owner.name, ) owner_id = await database.execute(query) return {"name": new_owner.name, "owner_id": owner_id}
async def create_stock(new_stock: NewStock, user: User = Depends(get_current_user)): # query the database to check if stock already exists stock_symbol = new_stock.symbol.upper() stock_records = await get_stock_records(stock_symbol) # create response dict from body model stock = new_stock.dict() if not stock_records: # stock not found in the database, calling yahoo to get stock info stock_info = await call_yahoo_from_view(stock_symbol) iso_currency = stock_info[YAHOO_FIELD_CURRENCY] # check if currency already exists in database query = currency.select().where( currency.c.iso_currency == iso_currency) currency_record = await database.fetch_one(query) if currency_record is None: # handle if currency does not exist # if stock currency is the default one use last rate of 1 last_rate = 1 symbol = None if iso_currency != user.base_currency: # if stock currency is not the default one call yahoo # to get currency info symbol = f"{user.base_currency}{iso_currency}=X".upper() currency_info = await call_yahoo_from_view(symbol) last_rate = currency_info[YAHOO_FIELD_PRICE] # save currency record in the database and get the record id query = currency.insert().values( currency_id=create_random_id(), iso_currency=iso_currency, last_rate=last_rate, symbol=symbol, last_update=datetime.utcnow(), ) currency_id = await database.execute(query) else: # if currency exists just save the id (needed for stock creation) currency_id = currency_record.currency_id # create stock record query = stocks.insert().values( stock_id=create_random_id(), short_name=stock_info[YAHOO_FIELD_NAME], currency_id=currency_id, market=stock_info[YAHOO_FIELD_MARKET], symbol=stock_symbol, last_price=stock_info[YAHOO_FIELD_PRICE], last_update=datetime.utcnow(), financial_currency=stock_info.get(YAHOO_FIELD_FINANCIAL_CURRENCY), ) stock_id = await database.execute(query) stock["short_name"] = stock_info[YAHOO_FIELD_NAME] stock["iso_currency"] = iso_currency stock["currency_id"] = currency_id stock["market"] = stock_info[YAHOO_FIELD_MARKET] stock["symbol"] = stock_symbol stock["last_price"] = stock_info[YAHOO_FIELD_PRICE] else: # if stock record exists already stock_record = stock_records[0] stock_id = stock_record[3] stock["short_name"] = stock_record[1] stock["iso_currency"] = stock_record[6] stock["currency_id"] = stock_record[5] stock["market"] = stock_record[4] stock["symbol"] = stock_symbol stock["last_price"] = stock_record[0] stock["stock_id"] = stock_id return stock