-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (73 loc) · 2.27 KB
/
index.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
const express = require('express');
const app = express();
const port = 8080;
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const methodOverride = require('method-override')
app.use(express.json());
app.use(express.urlencoded({extended: true}))
app.use(methodOverride("_method"))
app.use(express.static(path.join(__dirname,"public")));
app.set("view engine", "ejs");
app.set("views", path.join(__dirname,"views"));
let tasks = [
{
id: uuidv4(),
title: "Workout Routine",
description: "Mon- Push, Tue- Pull, Wed- Legs (Repeat it again)"
},
{
id: uuidv4(),
title: "Goal for next 6 months",
description: "Practice DSA, Learn Development new concepts, Practice Deployment and CICD Pipleine"
},
];
app.get('/tasks', (req, res) => {
res.status(200).render("index.ejs", {tasks});
});
app.get('/tasks/new', (req, res) =>{
res.status(200).render("new-task.ejs")
});
app.post('/tasks', (req, res) => {
let { title, description } = req.body;
if (!title || !description) {
return res.status(400).render("missing-data.ejs");
}
let id = uuidv4();
tasks.push({id, title, description});
res.redirect("/tasks");
});
app.get('/tasks/:id', (req, res) => {
let {id} = req.params;
let task = tasks.find((t) => id === t.id);
res.status(200).render("display.ejs", {task});
});
app.put('/tasks/:id', (req, res) => {
let title = req.body.title;
let description = req.body.description;
let {id} = req.params;
if (!title || !description) {
return res.status(400).render("missing-data.ejs");
}
let task = tasks.find((t) => id === t.id)
task.title = title;
task.description = description;
res.redirect("/tasks")
});
app.get('/tasks/:id/edit', (req, res) =>{
let {id} = req.params;
let task = tasks.find((t) => id === t.id)
res.status(200).render("edit.ejs", {task});
});
app.delete('/tasks/:id', (req, res) => {
let {id} = req.params;
tasks = tasks.filter((t) => id !== t.id)
res.redirect("/tasks")
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ error: 'Something went wrong!' });
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}/tasks`);
});