-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathapi.py
More file actions
109 lines (82 loc) · 2.14 KB
/
Copy pathapi.py
File metadata and controls
109 lines (82 loc) · 2.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
from flask import Blueprint, jsonify, request
from .models import Note
from . import db
api = Blueprint('api', __name__)
# CREATE NOTE
@api.route('/api/notes', methods=['POST'])
def create_note():
data = request.get_json()
if not data or 'data' not in data:
return jsonify({
"error": "Note content is required"
}), 400
new_note = Note(
data=data['data'],
user_id=1
)
db.session.add(new_note)
db.session.commit()
return jsonify({
"message": "Note created successfully",
"id": new_note.id,
"data": new_note.data
}), 201
# READ ALL NOTES
@api.route('/api/notes', methods=['GET'])
def get_notes():
notes = Note.query.all()
output = []
for note in notes:
output.append({
"id": note.id,
"data": note.data,
"date": str(note.date)
})
return jsonify(output), 200
# READ SINGLENOTE
@api.route('/api/notes/<int:id>', methods=['GET'])
def get_note(id):
note = Note.query.get(id)
if not note:
return jsonify({
"error": "Note not found"
}), 404
return jsonify({
"id": note.id,
"data": note.data,
"date": str(note.date)
}), 200
# UPDATE NOTE
@api.route('/api/notes/<int:id>', methods=['PUT'])
def update_note(id):
note = Note.query.get(id)
if not note:
return jsonify({
"error": "Note not found"
}), 404
data = request.get_json()
if not data or 'data' not in data:
return jsonify({
"error": "Missing note content"
}), 400
note.data = data['data']
db.session.commit()
return jsonify({
"message": "Note updated successfully",
"id": note.id,
"data": note.data
}), 200
# DELETE NOTE
@api.route('/api/notes/<int:id>', methods=['DELETE'])
def delete_note(id):
note = Note.query.get(id)
if not note:
return jsonify({
"error": "Note not found"
}), 404
db.session.delete(note)
db.session.commit()
return jsonify({
"message": "Note deleted successfully",
"id": id
}), 200