Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Sébastien Morele
interventions_api
Commits
01c9e000
Commit
01c9e000
authored
May 06, 2019
by
Sébastien Morele
Browse files
Initial commit
parents
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
85 additions
and
0 deletions
+85
-0
app.py
app.py
+85
-0
No files found.
app.py
0 → 100644
View file @
01c9e000
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment