コード例 #1
0
def create_stock(input: StockRequest, db: Session = Depends(get_db)):
    """
    Create a new stock and save in the database.
    """
    stock = StockItem()
    stock.ticker = input.ticker

    db.add(stock)
    db.commit()

    fetch_stock_data(stock.id)

    return {
        "code": "success",
        "message": "stock created"
    }
コード例 #2
0
def update_table(input: StockRequest, db: Session = Depends(get_db)):
    """
    Replace existing stocks data with the latest data from yfinance
    """

    engine.execute('DELETE FROM Stocks')  # delete all data from table
    for tick in eval(input.ticker):
        stock = StockItem()
        stock.ticker = tick

        db.add(stock)
        db.commit()

        fetch_stock_data(stock.id)

    return {
        "code": "success",
        "message": "stock table updated"
    }