-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
157 lines (135 loc) · 4.1 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const _ = require('lodash');
require('dotenv').config()
const app = express();
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.static("public"));
app.set('view engine','ejs');
/************DB-connect*************/
mongoose.connect('"'+'mongodb+srv://'+process.env.URI_KEY+'"')
.then(function(){
console.log("Connected Successfully to DataBase");
}).catch(function(err){
console.log(err);
})
/************DB-Data-init*************/
const listschema = mongoose.Schema({
name : String
})
const todo = mongoose.model("todo",listschema);
const todo1 = new todo({name : 'To Add item Click "Add new item"'});
const todo2 = new todo({name : 'Enter to or "+" icon to Save'});
const todo3 = new todo({name : "To Delete Click Checkbox"});
const page1arr = [todo1,todo2,todo3]
const pageschema = mongoose.Schema({
name : String,
content : [listschema]
})
const page = mongoose.model('page',pageschema)
/**********************************************/
var today = new Date();
var options={weekday : 'long',day : 'numeric',month : 'long'}
var day = today.toLocaleDateString('en-us',options);
const pages = []
function listpage() {
page.find().then(function(info){
info.forEach(function(type) {
if(pages.includes(type.name)){
}else{
if(type.name == 'Favicon.ico'){
//leave It
}else{
pages.push(type.name);
}
}
});
});
return pages;
}
app.get('/',function(req,res){
todo.find().then(function(ans){
if (ans.length === 0){
todo.insertMany(page1arr).then(function(){
}).catch(function(err){
console.log(err);
})
res.redirect("/");
}else{
res.render('days.ejs',{title : day, items : ans , listslist : listpage()});
}
}).catch(function(err) {
console.log(err);
})
})
app.get('/:somepage',function(req,res){
var pagename = _.capitalize([string=req.params.somepage])
const content = page({
name : pagename,
content : page1arr
})
page.findOne({name : pagename}).then(function(ans){
if(!ans){
content.save()
pages.push(pagename);
res.redirect('/'+pagename);
}else{
res.render('days.ejs',{title : ans.name, items : ans.content, listslist : listpage()})
}
}).catch(function(err){
console.log(err);
})
});
app.post('/listdelete',function(req,res){
var check = req.body.button;
const delbody = req.body.listpage;
page.deleteOne({name : delbody}).then(function(){
if(delbody == check){
//leave it
}else{
pages.pop(delbody);
res.redirect('/');
}
}).catch(function(err){
console.log(err);
})
});
app.post('/del',function(req,res){
const delitem = req.body.del;
const pagedel = req.body.pagedel;
if(pagedel === day){
todo.deleteOne({_id : delitem}).then(function(){
res.redirect('/');
}).catch(function(err){
console.log(err);
})
}else{
page.findOneAndUpdate({name : pagedel},{$pull :{content : {_id : delitem}}}).then(function(){
res.redirect('/'+pagedel)
}).catch(function(err){
console.log(err);
})
}
});
app.post('/',function(req,res){
var check = req.body.button;
var item = req.body.todo;
const todo4 = new todo({name : item});
if(check===day){
todo4.save()
res.redirect('/');
}else{
page.findOne({name : check}).then(function(itemz){
itemz.content.push(todo4);
itemz.save()
res.redirect('/'+check)
}).catch(function(err){
console.log(err);
})
res.redirect('/'+check);
}
});
app.listen(3000, function(){
console.log('Server Started @ Port 3000')
});