-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.py
92 lines (72 loc) · 2.43 KB
/
handlers.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
import falcon
from models import Ticket
from config import session
from repo import Repo
class CreateTicket:
@staticmethod
def on_post(req, resp):
request_payload = json.loads(req.stream.read().decode('utf-8'))
ticket_type = request_payload.get('ticket_type')
message = request_payload.get('message')
try:
ticket = Ticket.create_new(ticket_type, message)
with session() as db:
repo = Repo(db)
ticket.ticket_id = repo.save_new_ticket(ticket)
db.commit()
resp.body = json.dumps(
{
'id': ticket.ticket_id,
'status': ticket.status
},
sort_keys=True, indent=4
)
except ValueError as error:
resp.body = json.dumps(
{
'status': 'failed',
'reason': str(error)
},
sort_keys=True, indent=4
)
class QueryTicketStatus:
@staticmethod
def on_get(req, resp, ticket_id):
with session() as db:
repo = Repo(db)
ticket = repo.list_ticket(int(ticket_id))
if ticket is None:
resp.body = json.dumps({'status': 'not found'}, sort_keys=True, indent=4)
else:
resp.body = json.dumps(
{
'id': ticket.ticket_id,
'ticket_type': ticket.ticket_type,
'status': Ticket.find_status_name(ticket.status)
},
sort_keys=True, indent=4
)
class ChangeTicketStatus:
@staticmethod
def on_put(req, resp, ticket_id):
payload = json.loads(req.stream.read().decode('utf-8'))
status_name = payload.get('status')
with session() as db:
repo = Repo(db)
ticket = repo.list_ticket(int(ticket_id))
repo.change_ticket_status(
ticket=ticket,
status=Ticket.STATUSES[status_name]
)
resp.body = json.dumps(
{
'id': ticket_id,
'status': status_name
},
sort_keys=True, indent=4
)
app = falcon.API()
app.add_route('/create-ticket', CreateTicket)
app.add_route('/view-status/{ticket_id}', QueryTicketStatus)
app.add_route('/change-ticket-status/{ticket_id}', ChangeTicketStatus)