-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (78 loc) · 2.47 KB
/
app.js
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
const express = require('express');
const mysql = require('mysql2');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const port = 3000;
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.set('views', path.join(__dirname, 'views')); // Specify the views directory
app.set('view engine', 'ejs'); // Set EJS as the view engine
// MySQL Connection
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'Usama@282930',
database: 'taskdb'
});
// Connect to the database
db.connect((err) => {
if (err) throw err;
console.log('Connected to the database.');
});
// Route for the home page
app.get('/', (req, res) => {
res.render('home'); // Render the home view
});
// Routes
// Get all tasks
app.get('/tasks', (req, res) => {
db.query('SELECT * FROM tasks', (err, results) => {
if (err) throw err;
res.render('index', { tasks: results });
});
});
// Render form to create a new task
app.get('/tasks/new', (req, res) => {
res.render('new');
});
// Create a new task
app.post('/tasks', (req, res) => {
const { title, description, status } = req.body; // Use status instead of deadline
const sql = 'INSERT INTO tasks (title, description, status) VALUES (?, ?, ?)';
db.query(sql, [title, description, status], (err, result) => {
if (err) throw err;
res.redirect('/tasks');
});
});
// Render form to edit a task
app.get('/tasks/edit/:id', (req, res) => {
const taskId = req.params.id;
db.query('SELECT * FROM tasks WHERE id = ?', [taskId], (err, result) => {
if (err) throw err;
res.render('edit', { task: result[0] });
});
});
// Update a task
app.post('/tasks/update/:id', (req, res) => {
const taskId = req.params.id;
const { title, description, status } = req.body; // Use status instead of deadline
const sql = 'UPDATE tasks SET title = ?, description = ?, status = ? WHERE id = ?';
db.query(sql, [title, description, status, taskId], (err, result) => {
if (err) throw err;
res.redirect('/tasks');
});
});
// Delete a task
app.post('/tasks/delete/:id', (req, res) => {
const taskId = req.params.id;
const sql = 'DELETE FROM tasks WHERE id = ?';
db.query(sql, [taskId], (err, result) => {
if (err) throw err;
res.redirect('/tasks');
});
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});