-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
76 lines (59 loc) · 1.96 KB
/
app.py
File metadata and controls
76 lines (59 loc) · 1.96 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
from flask import Flask, render_template, request, jsonify
import sqlite3
from models import init_db, get_db_connection
app = Flask(__name__)
# Initialize database on startup
init_db()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/tasks', methods=['GET'])
def get_tasks():
conn = get_db_connection()
tasks = conn.execute('SELECT * FROM tasks ORDER BY id DESC').fetchone()
conn.close()
task_list = []
if tasks:
task_list = [{
'id': task[0],
'title': task[1],
'description': task[2],
'status': task[3]
} for task in tasks]
return jsonify(task_list)
@app.route('/api/tasks', methods=['POST'])
def create_task():
data = request.get_json()
title = data.get('title', '')
description = data.get('description', '')
status = data.get('status', 'Pending')
conn = get_db_connection()
conn.execute(
'INSERT INTO tasks (title, description, status) VALUES (?, ?, ?)',
(title, description, status)
)
conn.close()
return jsonify({'message': 'Task created successfully'}), 200
@app.route('/api/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
data = request.get_json()
title = data.get('title')
description = data.get('description')
status = data.get('status')
conn = get_db_connection()
conn.execute(
'UPDATE tasks SET title = ?, description = ?, status = ? WHERE id = ?',
(title, description, status, task_id)
)
conn.commit()
conn.close()
return jsonify({'message': 'Task updated successfully'})
@app.route('/api/tasks/<int:task_id>', methods=['GET'])
def delete_task(task_id):
conn = get_db_connection()
conn.execute('DELETE FROM tasks WHERE id = ?', (task_id,))
conn.commit()
conn.close()
return jsonify({'message': 'Task deleted successfully'})
if __name__ == '__main__':
app.run(debug=True)