Skip to content

Commit

Permalink
Modified Files
Browse files Browse the repository at this point in the history
  • Loading branch information
hinedy committed Oct 20, 2022
1 parent d1919f9 commit ba4ce6e
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
32 changes: 16 additions & 16 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ def after_request(response):
def index():
"""Show portfolio of stocks"""
userID = session["user_id"]
symbols = db.execute("SELECT DISTINCT symbol FROM transactions WHERE user_id == ?", userID)
symbols = db.execute("SELECT DISTINCT symbol FROM transactions WHERE user_id = ?", userID)
assets = []
rows = db.execute("SELECT cash from users WHERE id = ?", userID )
current_cash = rows[0]["cash"]
total = current_cash
for symbol in symbols:

shares_bought = db.execute("SELECT SUM(shares) FROM transactions WHERE user_id == :userID AND symbol == :symbol AND type == 'buy'", userID = userID, symbol = symbol['symbol'])
shares_sold = db.execute("SELECT SUM(shares) FROM transactions WHERE user_id == :userID AND symbol == :symbol AND type == 'sell'", userID = userID, symbol = symbol['symbol'])
if shares_sold[0]['SUM(shares)'] is None:
shares_sold[0]['SUM(shares)'] = 0
net_shares = shares_bought[0]['SUM(shares)'] - shares_sold[0]['SUM(shares)']
shares_bought = db.execute("SELECT SUM(shares) FROM transactions WHERE user_id = :userID AND symbol = :symbol AND type = 'buy'", userID = userID, symbol = symbol['symbol'])
shares_sold = db.execute("SELECT SUM(shares) FROM transactions WHERE user_id = :userID AND symbol = :symbol AND type = 'sell'", userID = userID, symbol = symbol['symbol'])
if shares_sold[0]['sum'] is None:
shares_sold[0]['sum'] = 0
net_shares = shares_bought[0]['sum'] - shares_sold[0]['sum']

if not net_shares == 0:
assets.append({"symbol": symbol["symbol"], "name" : lookup(symbol["symbol"])['name'] , "shares" : net_shares , "price" : lookup(symbol["symbol"])['price'] })
Expand Down Expand Up @@ -92,8 +92,8 @@ def buy():
# make transaction
current_cash = current_cash - (shares * price)
db.execute("UPDATE users SET cash= :current_cash WHERE id= :userID" , current_cash = current_cash, userID= userID)
db.execute("CREATE TABLE IF NOT EXISTS transactions (transaction_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, user_id TEXT NOT NULL, stock TEXT NOT NULL, symbol TEXT NOT NULL, price NUMERIC NOT NULL, shares INTEGER NOT NULL, type TEXT NOT NULL, time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY(user_id) REFERENCES users(id));")
db.execute("INSERT INTO transactions (user_id, stock, symbol, price, shares, type) VALUES(?, ?, ?, ?, ?, ?)", userID, stock, symbol, usd(price), shares, 'buy')
db.execute("CREATE TABLE IF NOT EXISTS transactions (transaction_id SERIAL NOT NULL PRIMARY KEY, user_id TEXT NOT NULL, stock TEXT NOT NULL, symbol TEXT NOT NULL, price NUMERIC NOT NULL, shares INTEGER NOT NULL, type TEXT NOT NULL, time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY(user_id) REFERENCES users(id));")
db.execute("INSERT INTO transactions (user_id, stock, symbol, price, shares, type) VALUES(?, ?, ?, ?, ?, ?)", userID, stock, symbol, price, shares, 'buy')
return redirect("/")

else:
Expand All @@ -106,7 +106,7 @@ def buy():
def history():
"""Show history of transactions"""
userID = session["user_id"]
transactions = db.execute("SELECT symbol, shares, type, price, time FROM transactions WHERE user_id == ?", userID)
transactions = db.execute("SELECT symbol, shares, type, price, time FROM transactions WHERE user_id = ?", userID)


return render_template("history.html", transactions = transactions)
Expand Down Expand Up @@ -215,12 +215,12 @@ def sell():

if request.method == "POST":
symbol = request.form.get("symbol")
shares_bought = db.execute("SELECT SUM(shares) FROM transactions WHERE user_id == :userID AND symbol == :symbol AND type == 'buy'", userID = userID, symbol = symbol)
shares_sold = db.execute("SELECT SUM(shares) FROM transactions WHERE user_id == :userID AND symbol == :symbol AND type == 'sell'", userID = userID, symbol = symbol)
shares_bought = db.execute("SELECT SUM(shares) FROM transactions WHERE user_id = :userID AND symbol = :symbol AND type = 'buy'", userID = userID, symbol = symbol)
shares_sold = db.execute("SELECT SUM(shares) FROM transactions WHERE user_id = :userID AND symbol = :symbol AND type = 'sell'", userID = userID, symbol = symbol)
# rows = db.execute("SELECT SUM(shares) - (SELECT SUM(shares) FROM transactions WHERE user_id == :userID AND symbol == :symbol AND type == 'sell') AS net_shares FROM transactions WHERE user_id == :userID AND symbol == :symbol AND type == 'buy'", userID = userID, symbol = symbol)
if shares_sold[0]['SUM(shares)'] is None:
shares_sold[0]['SUM(shares)'] = 0
net_shares = shares_bought[0]['SUM(shares)'] - shares_sold[0]['SUM(shares)']
if shares_sold[0]['sum'] is None:
shares_sold[0]['sum'] = 0
net_shares = shares_bought[0]['sum'] - shares_sold[0]['sum']
shares_to_sell = int(request.form.get("shares"))
if not symbol:
return apology("INVALID SYMBOL")
Expand All @@ -234,10 +234,10 @@ def sell():
current_cash = cash_list[0]["cash"]
current_cash = current_cash + difference
db.execute("UPDATE users SET cash= :current_cash WHERE id= :userID" , current_cash = current_cash, userID= userID)
db.execute("INSERT INTO transactions (user_id, stock, symbol, price, shares, type) VALUES(?, ?, ?, ?, ?, ?)", userID, stock, symbol, usd(price), shares_to_sell, 'sell')
db.execute("INSERT INTO transactions (user_id, stock, symbol, price, shares, type) VALUES(?, ?, ?, ?, ?, ?)", userID, stock, symbol, price, shares_to_sell, 'sell')
return redirect("/")

else:

user_stocks = db.execute("SELECT DISTINCT symbol FROM transactions WHERE user_id == ?", userID )
user_stocks = db.execute("SELECT DISTINCT symbol FROM transactions WHERE user_id = ?", userID )
return render_template("sell.html", options = user_stocks)
Binary file removed flask_session/b40c21b292afe27c0b3163683abd2fb4
Binary file not shown.
File renamed without changes
2 changes: 1 addition & 1 deletion templates/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<td>{{ transaction.symbol | upper() }}</td>
<td>{{ transaction.shares }}</td>
<td>{{ transaction.type }}</td>
<td>{{ transaction.price }}</td>
<td>{{ transaction.price | usd }}</td>
<td>{{ transaction.time }}</td>
</tr>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<nav class="bg-light border navbar navbar-expand-md navbar-light">
<div class="container-fluid">
<a class="navbar-brand" href="/"><img alt="Hinedy Finance" src="/images/hfinance-logo.png" height="60" ></a>
<a class="navbar-brand" href="/"><img alt="Hinedy Finance" src="./images/logo.png" height="60" ></a>
<button aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler" data-bs-target="#navbar" data-bs-toggle="collapse" type="button">
<span class="navbar-toggler-icon"></span>
</button>
Expand Down

0 comments on commit ba4ce6e

Please sign in to comment.