-
Notifications
You must be signed in to change notification settings - Fork 564
Expand file tree
/
Copy pathapi.py
More file actions
310 lines (255 loc) · 8.14 KB
/
api.py
File metadata and controls
310 lines (255 loc) · 8.14 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#
from flask import Blueprint, jsonify, request
from models import Note, db_session
from datetime import datetime
#----------------------------------------------------------------------------#
# API Blueprint
#----------------------------------------------------------------------------#
api_bp = Blueprint('api', __name__, url_prefix='/api')
#----------------------------------------------------------------------------#
# Helper Functions
#----------------------------------------------------------------------------#
def validate_note_data(data):
"""Validate note data from request"""
errors = []
if not data.get('title') or not data.get('title').strip():
errors.append('Title is required')
if not data.get('content') or not data.get('content').strip():
errors.append('Content is required')
return errors
#----------------------------------------------------------------------------#
# REST API Endpoints
#----------------------------------------------------------------------------#
# GET /api/notes - Get all notes
@api_bp.route('/notes', methods=['GET'])
def get_notes():
"""
Get all notes
Query Parameters:
search: Search term to filter notes
tag: Filter notes by tag
Returns:
JSON array of all notes
"""
try:
search_term = request.args.get('search')
tag_filter = request.args.get('tag')
if search_term:
notes = Note.search_notes(search_term)
elif tag_filter:
notes = Note.get_notes_by_tag(tag_filter)
else:
notes = Note.get_all_notes()
return jsonify({
'success': True,
'data': [note.to_dict() for note in notes],
'count': len(notes)
}), 200
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
# GET /api/notes/<id> - Get a single note
@api_bp.route('/notes/<int:note_id>', methods=['GET'])
def get_note(note_id):
"""
Get a single note by ID
Args:
note_id: The ID of the note to retrieve
Returns:
JSON object of the note
"""
try:
note = Note.get_note_by_id(note_id)
if not note:
return jsonify({
'success': False,
'error': 'Note not found'
}), 404
return jsonify({
'success': True,
'data': note.to_dict()
}), 200
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
# POST /api/notes - Create a new note
@api_bp.route('/notes', methods=['POST'])
def create_note():
"""
Create a new note
Request Body:
title: Note title (required)
content: Note content (required)
tags: Comma-separated tags (optional)
Returns:
JSON object of the created note
"""
try:
data = request.get_json()
if not data:
return jsonify({
'success': False,
'error': 'No data provided'
}), 400
# Validate data
errors = validate_note_data(data)
if errors:
return jsonify({
'success': False,
'errors': errors
}), 400
# Create note
note = Note.create_note(
title=data['title'].strip(),
content=data['content'].strip(),
tags=data.get('tags', '').strip() if data.get('tags') else None
)
return jsonify({
'success': True,
'data': note.to_dict(),
'message': 'Note created successfully'
}), 201
except Exception as e:
db_session.rollback()
return jsonify({
'success': False,
'error': str(e)
}), 500
# PUT /api/notes/<id> - Update a note
@api_bp.route('/notes/<int:note_id>', methods=['PUT'])
def update_note(note_id):
"""
Update an existing note
Args:
note_id: The ID of the note to update
Request Body:
title: Note title (optional)
content: Note content (optional)
tags: Comma-separated tags (optional)
Returns:
JSON object of the updated note
"""
try:
note = Note.get_note_by_id(note_id)
if not note:
return jsonify({
'success': False,
'error': 'Note not found'
}), 404
data = request.get_json()
if not data:
return jsonify({
'success': False,
'error': 'No data provided'
}), 400
# Update fields if provided
title = data.get('title')
content = data.get('content')
tags = data.get('tags')
# Validate if title or content are provided
if title is not None and not title.strip():
return jsonify({
'success': False,
'error': 'Title cannot be empty'
}), 400
if content is not None and not content.strip():
return jsonify({
'success': False,
'error': 'Content cannot be empty'
}), 400
updated_note = Note.update_note(
note_id,
title=title.strip() if title else None,
content=content.strip() if content else None,
tags=tags.strip() if tags else None
)
return jsonify({
'success': True,
'data': updated_note.to_dict(),
'message': 'Note updated successfully'
}), 200
except Exception as e:
db_session.rollback()
return jsonify({
'success': False,
'error': str(e)
}), 500
# DELETE /api/notes/<id> - Delete a note
@api_bp.route('/notes/<int:note_id>', methods=['DELETE'])
def delete_note(note_id):
"""
Delete a note
Args:
note_id: The ID of the note to delete
Returns:
Success message
"""
try:
note = Note.get_note_by_id(note_id)
if not note:
return jsonify({
'success': False,
'error': 'Note not found'
}), 404
success = Note.delete_note(note_id)
if success:
return jsonify({
'success': True,
'message': 'Note deleted successfully'
}), 200
else:
return jsonify({
'success': False,
'error': 'Failed to delete note'
}), 500
except Exception as e:
db_session.rollback()
return jsonify({
'success': False,
'error': str(e)
}), 500
# GET /api/tags - Get all unique tags
@api_bp.route('/tags', methods=['GET'])
def get_tags():
"""
Get all unique tags from all notes
Returns:
JSON array of unique tags
"""
try:
notes = Note.get_all_notes()
all_tags = set()
for note in notes:
if note.tags:
tags = [tag.strip() for tag in note.tags.split(',')]
all_tags.update(tags)
return jsonify({
'success': True,
'data': sorted(list(all_tags))
}), 200
except Exception as e:
return jsonify({
'success': False,
'error': str(e)
}), 500
#----------------------------------------------------------------------------#
# Error Handlers
#----------------------------------------------------------------------------#
@api_bp.errorhandler(404)
def not_found(error):
return jsonify({
'success': False,
'error': 'Endpoint not found'
}), 404
@api_bp.errorhandler(500)
def internal_error(error):
return jsonify({
'success': False,
'error': 'Internal server error'
}), 500