diff --git a/backend.py b/backend.py index 6736654..bd04f0e 100644 --- a/backend.py +++ b/backend.py @@ -4,6 +4,7 @@ import falcon import psycopg2 import uuid +import json class StatsResource(object): def on_get(self, req, resp): @@ -21,12 +22,37 @@ class StatsResource(object): resp.set_header('Access-Control-Allow-Headers', 'X-Requested-With') resp.status = falcon.HTTP_200 +class EventResource(object): + def on_post(self, req, resp): + # get request body payload (json) + body = req.stream.read().decode('utf-8') + j=json.loads(body) + + # connect to db and insert + db = psycopg2.connect("dbname=oedb") + cur = db.cursor() + cur.execute("""INSERT INTO events ( events_type, events_what, events_when, events_tags) VALUES (%s, %s, %s, %s) RETURNING events_id;""",(j['type'],j['what'],j['when'], body)) + # get newly created event id + e = cur.fetchone() + db.commit() + cur.close() + db.close() + # send back to client + resp.body = """{"id":"%s"}""" % (e[0]) + resp.set_header('X-Powered-By', 'OpenEventDatabase') + resp.set_header('Access-Control-Allow-Origin', '*') + resp.set_header('Access-Control-Allow-Headers', 'X-Requested-With') + resp.status = falcon.HTTP_200 + + # falcon.API instances are callable WSGI apps app = falcon.API() # Resources are represented by long-lived class instances +event = EventResource() stats = StatsResource() # things will handle all requests to the matching URL path +app.add_route('/event', event) # handle single event requests app.add_route('/stats', stats) diff --git a/setup.sql b/setup.sql index b44b02b..792c147 100644 --- a/setup.sql +++ b/setup.sql @@ -41,6 +41,20 @@ CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public; COMMENT ON EXTENSION postgis IS 'PostGIS geometry, geography, and raster spatial types and functions'; +-- +-- Name: uuid-ossp; Type: EXTENSION; Schema: -; Owner: - +-- + +CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public; + + +-- +-- Name: EXTENSION "uuid-ossp"; Type: COMMENT; Schema: -; Owner: - +-- + +COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)'; + + SET search_path = public, pg_catalog; -- @@ -72,10 +86,19 @@ CREATE TABLE events ( events_where geometry, events_when timestamp with time zone, events_type text, - events_tags json + events_tags json, + events_id uuid DEFAULT uuid_generate_v4(), + createdate timestamp without time zone DEFAULT now() ); +-- +-- Name: events_idx_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX events_idx_id ON events USING btree (events_id); + + -- -- Name: events_idx_what; Type: INDEX; Schema: public; Owner: - --