-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
228 lines (216 loc) · 5.9 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const date = require(__dirname + "/date.js");
const mongoose = require("mongoose");
const _ = require("lodash");
const Quote = require("inspirational-quotes");
const https = require("https");
const app = express();
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
// create mongodb database - on cloud Amazon server(MongodB atlas)
mongoose.connect(
"mongodb+srv://absternator:[email protected]/todolistDB",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
// create schema for items
const itemsScehma = mongoose.Schema({
name: {
type: String,
required: [true, "Needs a name"],
},
});
// creates "item(s)" collection
const Item = mongoose.model("Item", itemsScehma);
// create Default items
const item1 = new Item({
name: "Welcome to your todo List",
});
// insert items
const defaultArray = [item1];
// List schema
const listSchema = mongoose.Schema({
name: String,
items: [itemsScehma],
});
// add type of list schema
const List = mongoose.model("List", listSchema);
// Get todays date
const day = date.getDate();
// create weather schema
const weatherScehma = mongoose.Schema({
city: {
type: String,
required: [true, "NEED A CITY"],
},
description: String,
temprature: Number,
imgUrl: String,
});
const Weather = mongoose.model("Weather", weatherScehma);
app.get("/", function (req, res) {
// Check if city initialzied
Weather.find({}, (err, foundWeather) => {
if (foundWeather.length === 0) {
res.render("weather", { quote: Quote.getQuote() });
} else {
const city = foundWeather[0].city;
const temp = foundWeather[0].temprature;
const description = foundWeather[0].description;
const imgUrl = foundWeather[0].imgUrl;
Item.find({}, (err, foundItems) => {
// Add default at the beggening only
if (foundItems.length === 0) {
Item.insertMany(defaultArray, (err) => {
if (err) {
console.log(err);
} else {
console.log("Deafult items inserted");
}
});
res.redirect("/");
} else {
res.render("list", {
listTitle: day,
newListItems: foundItems,
quote: Quote.getQuote(),
temp: temp,
description: description,
imgUrl: imgUrl,
city: city,
});
}
});
}
});
});
app.post("/", function (req, res) {
const itemName = req.body.newItem;
const listName = req.body.list;
const item = new Item({
name: itemName,
});
if (listName === day) {
item.save();
res.redirect("/");
} else {
List.findOne({ name: listName }, (err, foundList) => {
foundList.items.push(item);
foundList.save();
res.redirect("/" + listName);
});
}
});
app.post("/delete", (req, res) => {
const checkedBoxID = req.body.checkbox;
const listName = req.body.listName;
if (listName == day) {
Item.findByIdAndDelete(checkedBoxID, function (err) {
if (err) {
console.log(err);
} else {
console.log("succesfully removed");
res.redirect("/");
}
});
} else {
List.findOneAndUpdate(
{ name: listName },
{ $pull: { items: { _id: checkedBoxID } } },
(err, foundList) => {
if (!err) {
res.redirect("/" + listName);
}
}
);
}
// remove the checked item
});
app.post("/weather", (req, res) => {
const city = req.body.city;
const appid = "45fe6714fe3048e1e20cd3048a90ff82";
const units = "metric";
const url =
"https://api.openweathermap.org/data/2.5/weather?q=" +
city +
"&appid=" +
appid +
"&units=" +
units;
// get weather
https.get(url, function (response) {
console.log(response.statusCode);
response.on("data", (data) => {
const weatherData = JSON.parse(data);
const cityWeather = new Weather({
city: city,
description: weatherData.weather[0].description,
temprature: weatherData.main.temp,
imgUrl:
"http://openweathermap.org/img/wn/" +
weatherData.weather[0].icon +
"@2x.png",
});
cityWeather.save();
});
setTimeout(function () {
res.redirect("/");
}, 500);
});
});
// add differnt types of lists to different pages
app.get("/:customListName", (req, res) => {
const customListName = _.capitalize(req.params.customListName);
Weather.find({}, (err, foundWeather) => {
if (foundWeather.length === 0) {
res.render("weather", { quote: Quote.getQuote() });
} else {
const city = foundWeather[0].city;
const temp = foundWeather[0].temprature;
const description = foundWeather[0].description;
const imgUrl = foundWeather[0].imgUrl;
List.findOne({ name: customListName }, (err, foundList) => {
if (!err) {
// if foundlist ===null --> (!foundlist)
if (!foundList) {
// create new List
const list = new List({
name: customListName,
items: defaultArray,
});
list.save();
res.redirect("/" + customListName);
} else {
// show exisitng list
res.render("list", {
listTitle: foundList.name,
newListItems: foundList.items,
quote: Quote.getQuote(),
temp: temp,
description: description,
imgUrl: imgUrl,
city: city,
});
}
} else {
console.log(err);
}
});
}
});
});
app.get("/about", function (req, res) {
res.render("about");
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function () {
console.log("Server started Successfully");
});