From 1543cacc546fd94f4d475056721dd285a6874bf3 Mon Sep 17 00:00:00 2001 From: Alan Orth Date: Mon, 24 Sep 2018 14:28:00 +0300 Subject: [PATCH] app.py: Update SQL logic to use single table The indexer.py script was updated to use a single table because I learned about UPSERT. This simplifies the database schema and the Python logic, and makes it easier to page all views and downloads at once without complicated JOIN queries. --- app.py | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/app.py b/app.py index 7d6738c..fccece0 100644 --- a/app.py +++ b/app.py @@ -14,26 +14,14 @@ class ItemResource: """Handles GET requests""" cursor = db.cursor() - # get item views (and catch the TypeError if item doesn't have any views) - cursor.execute('SELECT views FROM itemviews WHERE id={0}'.format(item_id)) - try: - views = cursor.fetchone()['views'] - except: - views = 0 - - # get item downloads (and catch the TypeError if item doesn't have any downloads) - cursor.execute('SELECT downloads FROM itemdownloads WHERE id={0}'.format(item_id)) - try: - downloads = cursor.fetchone()['downloads'] - except: - downloads = 0 - + cursor.execute('SELECT views, downloads FROM items WHERE id={0}'.format(item_id)) + results = cursor.fetchone() cursor.close() statistics = { 'id': item_id, - 'views': views, - 'downloads': downloads + 'views': results['views'], + 'downloads': results['downloads'] } resp.media = statistics