From 228e01c1a290db813fa1e939200140b3e1faf89f Mon Sep 17 00:00:00 2001
From: Clemens Berteld <clemens@berteld.com>
Date: Sun, 5 Sep 2021 19:38:51 +0200
Subject: [PATCH] API works and is finished (?)

---
 dataacquisition/api.py | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/dataacquisition/api.py b/dataacquisition/api.py
index c91047f..34df101 100644
--- a/dataacquisition/api.py
+++ b/dataacquisition/api.py
@@ -13,6 +13,7 @@ app.config['TESTING'] = False
 
 @app.route('/', methods=['GET'])
 def index():
+    columns = sql.SQL(' * ')
     wheres = sql.SQL('')
     values = ''  # Gets used in cursor.execute()s' second parameter for safety reasons
 
@@ -22,6 +23,16 @@ def index():
         for n in [int(station_id)]:
             values = (*values, n)  # adding n to existing tuple
 
+    if 'years' in request.args:
+        years = request.args['years'].split(',')
+        years_clean = []
+
+        for year in years:
+            years_clean.append('"' + year + '"')
+        years_clean = str(years_clean).replace('[', '').replace(']', '').replace("'", "").replace('\n', '').replace(' ', '')
+
+        columns = sql.SQL('id, ') + sql.SQL(years_clean)
+
     # Just for development
     # if 'country' in request.args:
     #     country = request.args['country']
@@ -30,9 +41,9 @@ def index():
     #         values = (*values, n)  # adding n to existing tuple
 
     query = sql.SQL("SELECT array_to_json(array_agg(row_to_json(t))) from ("
-                    "SELECT * FROM stations WHERE lon IS NOT NULL "
+                    "SELECT {} FROM stations WHERE lon IS NOT NULL "
                     "{} "
-                    ") t;").format(wheres)
+                    ") t;").format(columns, wheres)
 
     with psycopg2.connect(database='temperatures_berteld_morstein', user='postgres', password='postgres', host='localhost', port=5432) as connection:
         with connection.cursor() as cursor:
-- 
GitLab