Skip to content

Commit

Permalink
feat: complex queries
Browse files Browse the repository at this point in the history
  • Loading branch information
HiIAmTzeKean committed Apr 7, 2023
1 parent d101534 commit 9e1a7c8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
36 changes: 35 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,48 @@ def register():
def bookings():
email = session["user"]

user_visited_all = execute_sql(db, f'''
SELECT *
FROM users as u
WHERE NOT EXISTS (
SELECT h.id
FROM houses as h
EXCEPT
SELECT r.houseid
FROM rental as r
WHERE u.email=r.email
);
''').__len__()

unique_booking_count = execute_sql(db, f'''
SELECT count(*)
FROM rental as r, houses as h
WHERE r.email = '{email}' AND h.id=r.houseid;
''')[0]

unique_houses_not_booked = execute_sql(db, f'''
SELECT count(*)
FROM
(SELECT id
FROM houses
EXCEPT
SELECT r.houseid
FROM rental as r, houses as h
WHERE r.email = '{email}' AND h.id=r.houseid) as list
;
''')[0]

rentals = execute_sql(db, f'''
SELECT h.location, h.price, r.num_of_days, r.date
FROM rental as r, houses as h
WHERE r.email = '{email}' AND h.id=r.houseid
ORDER BY r.date desc;
''')
return render_template('bookings/index.html',
rents=rentals)
rents=rentals,
unique_booking_count=unique_booking_count,
unique_houses_not_booked=unique_houses_not_booked,
user_visited_all=user_visited_all)

@app.route('/logout')
@is_login
Expand Down
5 changes: 5 additions & 0 deletions templates/bookings/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ <h1>{% block title %}Past bookings{% endblock %}</h1>
{% endblock %}

{% block content %}
<p>There are a total of {{ user_visited_all }} users who booked all houses!!!</p>
<p>You have booked a total of {{ unique_booking_count.count }} unique houses before</p>
<p>You have a total of {{ unique_houses_not_booked.count }} unique houses to explore</p>
<hr>

<table>
<thead>
<td>Location</td>
Expand Down

0 comments on commit 9e1a7c8

Please sign in to comment.