-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
56 lines (45 loc) · 1.46 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from tornado.ioloop import IOLoop
import tornado.web
from repository import repo
from domain import service
import sqlite3 as sqlite
class DishHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self, restaurant, id):
if id is None:
#Raise exception
self.write('id is missing')
self.finish()
repository = repo.Repo()
svc = service.Service(repository)
dish = svc.getDish(restaurant, id)
self.write({'dish': dish})
self.finish()
@tornado.web.asynchronous
def post(self, restaurant, id):
if id is not None:
#Raise exception
self.write('ID should not be in the URL')
self.finish()
repository = repo.Repo()
svc = service.Service(repository)
bodyData = tornado.escape.json_decode(self.request.body)
try:
dish = svc.insertDish(restaurant, bodyData['name'], bodyData['description'], bodyData['price'])
self.write({'dish': dish})
except:
self.write('Incorrect post parameters')
finally:
self.finish()
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/([a-z]+)/dish/([0-9]+)?", DishHandler)
]
tornado.web.Application.__init__(self, handlers)
def main():
app = Application()
app.listen(8888)
IOLoop.instance().start()
if __name__ == '__main__':
main()