-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotes_app.py
More file actions
160 lines (148 loc) · 3.58 KB
/
Copy pathNotes_app.py
File metadata and controls
160 lines (148 loc) · 3.58 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
from flask import Flask, request, jsonify
from flask_cors import CORS
from flasgger import Swagger
app = Flask(__name__)
CORS(app)
swagger = Swagger(app)
notes = []
# Function to reassign IDs after delete
def reindex_notes():
for idx, note in enumerate(notes, start=1):
note['id'] = idx
# Create a new note
@app.route('/api/notes', methods=['POST'])
def create_note():
"""
Create a new note
---
tags:
- Notes
parameters:
- name: body
in: body
required: true
schema:
id: Note
required:
- title
- content
properties:
title:
type: string
description: Title of the note
content:
type: string
description: Content of the note
responses:
201:
description: Note created successfully
"""
data = request.get_json()
if not data or 'title' not in data or 'content' not in data:
return jsonify({'error': 'Title and content are required'}), 400
note = {
'id': len(notes) + 1,
'title': data['title'],
'content': data['content']
}
notes.append(note)
return jsonify(note), 201
# Get all notes
@app.route('/api/notes', methods=['GET'])
def get_notes():
"""
Get all notes
---
tags:
- Notes
responses:
200:
description: A list of all notes
"""
return jsonify(notes)
# Get a specific note
@app.route('/api/notes/<int:note_id>', methods=['GET'])
def get_note(note_id):
"""
Get a note by ID
---
tags:
- Notes
parameters:
- name: note_id
in: path
type: integer
required: true
description: The ID of the note
responses:
200:
description: The requested note
404:
description: Note not found
"""
for note in notes:
if note['id'] == note_id:
return jsonify(note)
return jsonify({'error': 'Note not found'}), 404
# Update a note
@app.route('/api/notes/<int:note_id>', methods=['PUT'])
def update_note(note_id):
"""
Update a note by ID
---
tags:
- Notes
parameters:
- name: note_id
in: path
type: integer
required: true
description: The ID of the note
- name: body
in: body
required: true
schema:
properties:
title:
type: string
content:
type: string
responses:
200:
description: Note updated successfully
404:
description: Note not found
"""
data = request.get_json()
for note in notes:
if note['id'] == note_id:
if 'title' in data:
note['title'] = data['title']
if 'content' in data:
note['content'] = data['content']
return jsonify({'message': 'Note updated successfully'})
return jsonify({'error': 'Note not found'}), 404
# Delete a note
@app.route('/api/notes/<int:note_id>', methods=['DELETE'])
def delete_note(note_id):
"""
Delete a note by ID
---
tags:
- Notes
parameters:
- name: note_id
in: path
type: integer
required: true
description: The ID of the note
responses:
200:
description: Note deleted and IDs updated
"""
global notes
notes = [note for note in notes if note['id'] != note_id]
reindex_notes()
return jsonify({'message': 'Note deleted and IDs updated successfully'})
if __name__ == '__main__':
app.run(debug=True)