-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
103 lines (79 loc) · 3.4 KB
/
server.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
91
92
93
94
95
96
97
98
99
100
101
102
103
const express = require("express");
const bodyparser = require("body-parser");
const mongoose = require("mongoose");
const ejs = require("ejs");
const date = require(__dirname + "/date.js");
const day = date.getDate();
const path = require('path');
const { log } = require("console");
const _ = require("lodash");
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyparser.urlencoded({extended: true}));
app.use(express.static("public"));
const itemschema = new mongoose.Schema({
name: String
});
const Item = mongoose.model("Item", itemschema);
const donetask = mongoose.model("Donetask", itemschema);
const defaultItems = [];
mongoose.connect('mongodb+srv://21bcs082:Test123@cluster0.rlziopl.mongodb.net/To-Do-ListDB', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(function(){
console.log("Succefully connected to mongoose");
app.get("/", function(req,res){
Item.find()
.then(function(founditems){
donetask.find()
.then(function(complete){
if(founditems.length === 0){
res.render("list" ,{listTitle: day, newListItem: defaultItems, completeditems: complete, completeitemlength: complete.length, Todoitemlength: founditems.length + complete.length});
}
else{
res.render("list" ,{listTitle: day, newListItem: founditems , completeditems: complete, completeitemlength: complete.length, Todoitemlength: founditems.length + complete.length});
}
});
});
});
app.post("/", function(req,res){
const Newinput = req.body.newItem;
const listName = req.body.list;
const newitem = new Item({
name: Newinput
});
newitem.save()
res.redirect("/");
});
app.post("/delete", function(req,res){
const checkedItemId = req.body.checkbox;
const completetaskname = req.body.doneName;
const completetask = new donetask({
name: completetaskname
});
completetask.save();
Item.findByIdAndRemove(checkedItemId)
.then( function(){
res.redirect("/");
})
.catch(function(err){
console.error("Error while ticking checkbox : ",err);
});
});
app.post("/delete_completetask",function(req,res){
const deletebtn = req.body.deletebtn;
donetask.findByIdAndRemove(deletebtn)
.then(function(){
res.redirect("/");
})
.catch(function(err){
console.error("Error while deleting completed task : ",err);
});
})
})
.catch(err => console.error('Error connecting to MongoDB:', err));
app.listen(3006, function() {
console.log("Start Hogya Bc");
});