forked from AWSCC-PUP-DSWD/AWSCC-CodeQuest-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
60 lines (44 loc) · 1.44 KB
/
test.py
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
from flask import Flask, request, jsonify
app = Flask(__name__)
# Sample data (list of tasks)
tasks = [
{
"title": "Sheeting myself",
}
]
# Route to get all tasks
@app.route('/api/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks}), 200
# Route to create a new task
@app.route('/api/tasks', methods=['POST'])
def create_task():
data = request.get_json()
if 'title' in data:
task = {'title': data['title']}
tasks.append(task)
return jsonify({'message': 'Task created successfully'}), 201
else:
return jsonify({'message': 'Title is required'}), 400
# Route to update a task
@app.route('/api/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
if task_id < len(tasks):
data = request.get_json()
if 'title' in data:
tasks[task_id]['title'] = data['title']
return jsonify({'message': 'Task updated successfully'})
else:
return jsonify({'message': 'Title is required'}), 400
else:
return jsonify({'message': 'Task not found'}), 404
# Route to delete a task
@app.route('/api/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
if task_id < len(tasks):
del tasks[task_id]
return jsonify({'message': 'Task deleted successfully'})
else:
return jsonify({'message': 'Task not found'}), 404
if __name__ == '__main__':
app.run()