-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrud.py
More file actions
71 lines (63 loc) · 1.81 KB
/
crud.py
File metadata and controls
71 lines (63 loc) · 1.81 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
from flask import Flask, jsonify, request
app = Flask(__name__)
# Sample tasks
tasks = [
{
'id': 1,
'title': 'Task 1',
'description': 'Description 1',
'completed': False
},
{
'id': 2,
'title': 'Task 2',
'description': 'Description 2',
'completed': False
}
]
# Get all tasks
@app.route('/tasks', methods=['GET'])
def get_tasks():
return jsonify(tasks)
# Get a specific task
@app.route('/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
task = next((task for task in tasks if task['id'] == task_id), None)
if task:
return jsonify(task)
else:
return jsonify({'message': 'Task not found'})
# Create a new task
@app.route('/tasks', methods=['POST'])
def create_task():
data = request.get_json()
task = {
'id': len(tasks) + 1,
'title': data['title'],
'description': data['description'],
'completed': False,
'name' : data['name']
}
tasks.append(task)
return jsonify({'message': 'Task created successfully'})
# Update a task
@app.route('/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
task = next((task for task in tasks if task['id'] == task_id), None)
if task:
data = request.get_json()
task.update(data)
return jsonify({'message': 'Task updated successfully'})
else:
return jsonify({'message': 'Task not found'})
# Delete a task
@app.route('/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
task = next((task for task in tasks if task['id'] == task_id), None)
if task:
tasks.remove(task)
return jsonify({'message': 'Task deleted successfully'})
else:
return jsonify({'message': 'Task not found'})
if __name__ == '__main__':
app.run(debug=True)