-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
138 lines (101 loc) · 4.38 KB
/
app.py
File metadata and controls
138 lines (101 loc) · 4.38 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import sqlite3
from flask import Flask, jsonify, request
app = Flask(__name__)
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
@app.route('/template', methods=['GET'])
def templates_index():
conn = get_db_connection()
query = 'SELECT id, body FROM templates'
templates = conn.execute(query).fetchall()
conn.close()
template_list = [{'id': template['id'], 'body': template['body']} for template in templates]
return jsonify(template_list)
@app.route('/template/<int:id>', methods=['GET'])
def templates_show(id):
conn = get_db_connection()
query = 'SELECT * FROM templates WHERE id = ?'
template = conn.execute(query, (id,)).fetchone()
conn.close()
if template is None:
return 'Template not found', 404
return jsonify(dict(template))
@app.route('/template', methods=['POST'])
def create_template():
template_data = request.json
if 'body' not in template_data or not template_data['body'].strip():
return 'Template body cannot be blank', 400
conn = get_db_connection()
query = 'INSERT INTO templates (body) VALUES (?)'
cursor = conn.execute(query, (template_data['body'],))
template_id = cursor.lastrowid
conn.commit()
conn.close()
return jsonify({'id': template_id, 'body': template_data['body']}), 201
@app.route('/template/<int:id>', methods=['PUT'])
def update_template_put(id):
conn = get_db_connection()
existing_template = conn.execute('SELECT * FROM templates WHERE id = ?', (id,)).fetchone()
if existing_template is None:
conn.close()
return 'Template not found', 404
updated_template_data = request.json
if 'body' not in updated_template_data or not updated_template_data['body'].strip():
conn.close()
return 'Template body cannot be blank', 400
query = 'UPDATE templates SET body = ? WHERE id = ?'
conn.execute(query, (updated_template_data['body'], id))
conn.commit()
conn.close()
return jsonify({'id': id, 'body': updated_template_data['body']}), 200
@app.route('/notification', methods=['GET'])
def notifications_index():
conn = get_db_connection()
query = 'SELECT * FROM notifications'
notes = conn.execute(query).fetchall()
conn.close()
notes_list = [{'id': note['id'], 'phone_number': note['phone_number'], 'personalization': note['personalization'], 'template_id': note['template_id']} for note in notes]
return jsonify(notes_list)
@app.route('/notification/<int:id>', methods=['GET'])
def get_one_notification(id):
conn = get_db_connection()
query = '''
SELECT notifications.id, notifications.phone_number, notifications.personalization,
templates.body AS template_body
FROM notifications
JOIN templates ON notifications.template_id = templates.id
WHERE notifications.id = ?
'''
notification = conn.execute(query, (id,)).fetchone()
conn.close()
if notification is None:
return 'Notification not found', 404
content = notification['template_body'].replace('(personal)', notification['personalization'])
notification_dict = dict(notification)
notification_dict['content'] = content
return jsonify(notification_dict)
@app.route('/notification', methods=['POST'])
def create_notification():
notification_data = request.json
required_fields = ['phone_number', 'template_id']
for field in required_fields:
if field not in notification_data:
return f'Missing required field: {field}', 400
conn = get_db_connection()
template_check_query = 'SELECT id FROM templates WHERE id = ?'
template_exists = conn.execute(template_check_query, (notification_data['template_id'],)).fetchone()
if not template_exists:
return 'Invalid template_id', 400
query = 'INSERT INTO notifications (phone_number, personalization, template_id) VALUES (?, ?, ?)'
cursor = conn.execute(query, (
notification_data['phone_number'],
notification_data.get('personalization', None),
notification_data['template_id']
))
notification_id = cursor.lastrowid
conn.commit()
created_notification = conn.execute('SELECT * FROM notifications WHERE id = ?', (notification_id,)).fetchone()
conn.close()
return jsonify(dict(created_notification)), 201