Commit 01c9e000 authored by Sébastien Morele's avatar Sébastien Morele
Browse files

Initial commit

parents
import falcon
import json
import sqlite3 as lite
import sys
class InterventionsRessource(object):
def __init__(self):
try:
self.con = lite.connect("interventions.db")
except lite.Error as e:
print(e)
sys.exit()
def on_get(self, req, resp):
interventions = []
with self.con:
self.cur = self.con.cursor()
self.cur.execute('SELECT * FROM Interventions;')
data = self.cur.fetchall()
for intervention in data:
interventions.append(intervention)
resp.status = falcon.HTTP_200
resp.body = json.dumps(interventions)
def on_post(self, req, resp):
data = json.load(req.stream)
title = data['title']
description = data['description']
with self.con:
self.cur = self.con.cursor()
self.con.execute('INSERT INTO Interventions (title, description) VALUES ("{}", "{}")'.format(title, description))
resp.body = data['title']
resp.status = falcon.HTTP_200
class InterventionRessource(object):
def __init__(self):
try:
self.con = lite.connect("interventions.db")
except lite.Error as e:
print(e)
sys.exit()
def on_get(self, req, resp, intervention_id):
with self.con:
self.cur = self.con.cursor()
self.cur.execute("SELECT * FROM Interventions where id=%s;" % intervention_id)
data = self.cur.fetchone()
if data:
resp.status = falcon.HTTP_200
resp.body = json.dumps(data)
else:
resp.status = falcon.HTTP_404
def on_delete(self, req, resp, intervention_id):
with self.con:
self.cur = self.con.cursor()
self.cur.execute("DELETE FROM Interventions WHERE id=%s;" % intervention_id)
resp.status = falcon.HTTP_200
def on_put(self, req, resp, intervention_id):
data = json.load(req.stream)
title = data['title']
description = data['description']
with self.con:
self.cur = self.con.cursor()
self.con.execute('UPDATE Interventions SET title = "{}", description = "{}" WHERE id={}'.format(title, description, intervention_id))
resp.body = data['title']
resp.status = falcon.HTTP_200
api = falcon.API()
interventions = InterventionsRessource()
api.add_route('/interventions', interventions)
intervention = InterventionRessource()
api.add_route('/interventions/{intervention_id}', intervention)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment