-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
204 lines (192 loc) · 5.91 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
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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const Logger = require("./controller/logger");
const cors = require("cors");
require("dotenv").config();
const { USER, LOG } = require("./models/tracker");
// --- Setting Up PORT && MONGOOSE CONFIG --- //
const MONGO_URI = process.env.MONGO_URI;
const PORT = process.env.PORT;
const app = express();
app.use(Logger);
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.static(`${process.cwd()}/public`));
// --- Middlewares --- //
// --- database connection --- //
// --- MONGO_URI came from process.env.MONGOURI --- //
async function ConnectDB() {
return await mongoose.connect(MONGO_URI, {
dbName: "db",
useNewUrlParser: true,
serverSelectionTimeoutMS: 5000,
});
}
// --- Mongoose Connection Events Listning --- //
const connection = mongoose.connection;
connection.on("error", (error) => {
// On Connecting Error
console.log(error);
});
connection.on("connecting", (data) => {
// On Connection Attempt
console.log("Connecting to mongoose database......");
});
connection.on("open", (data) => {
// On Database connected and open to use
console.log("Connection to mongoose database established successfully.");
});
// --- Home Route Api * Index.html File --- //
app.get("/", (req, res) => {
res.sendFile(process.cwd() + "/views/index.html");
});
// --- Get All Users * Array Result --- //
app.get("/api/users", async (req, res) => {
const users = await USER.find().select("-__v");
res.json(users);
});
// --- Create New User Route Api --- //
app.post("/api/users", async (req, res) => {
const data = req.body;
const username = data?.username;
// --- Username Nan? --- //
if (!username) {
console.log("Username not provided");
return res.json({
error: "Username must be provided!",
});
}
// --- New User Creation --- //
console.log("user: " + username + " requested admission.");
// --- New User Options --- //
const _user = new USER({
username: username,
});
// --- New User Created --- //
await _user.save();
console.log(_user);
res.json({
username: _user.username,
_id: _user._id,
});
});
// --- User Excercise Create | Post Data | --- //
app.post("/api/users/:_id/exercises", async (req, res) => {
console.log(req.body);
// --- User id To Insert Data--- //
const data = req.body;
// --- _id data comes from req.params --- //
const { _id } = req.params;
const description = data?.description;
const duration = data?.duration;
const date = data?.date;
if (!_id) {
return res.send("please provide user id");
}
if (!description || !duration) {
return res.send("please provide description and duration");
}
// --- Data Validation Passed --- //
// --- Creating a Log Object --- //
let _userLog = new LOG({
description: description,
duration: Number(duration),
});
// --- If user didnt provided date! seting up new date --- //
if (!date) {
_userLog.date = new Date().toISOString().substring(0, 10);
} else {
_userLog.date = date;
}
// --- User Validation & Updating Log Array --- //
let userfound = await USER.findByIdAndUpdate(
_id,
{
$push: {
log: _userLog,
},
},
{ new: true }
);
if (!userfound) {
// --- No User Found --- //
console.log(`No User Found! with id:${_id}`);
return res.send("No user found!");
}
// --- Saving user after Inserting new Log Array --- //
await userfound.save();
// --- Generating manual response --- //
res.json({
_id: userfound._id,
username: userfound.username,
date: new Date(_userLog.date).toDateString(),
description: _userLog.description,
duration: _userLog.duration,
});
});
// --- Get User All Excercise Logs || Route Api --- //
app.get("/api/users/:_id/logs", async (req, res) => {
// --- Get User ID From req.params (:_id) --- //
const { _id } = req.params;
console.log(_id);
// --- Finding User by Id --- //
let _user = await USER.findById(_id);
// --- If No User Found --- //
if (!_user) {
console.log("No User Found!");
return res.send("No User Found");
}
// --- converting each date value of log array into DateString --- //
_user.log.forEach((record) => {
record.date = new Date(record.date).toDateString();
});
// --- If user provided query params --- //
if (req.query.from || req.query.to) {
let fromDate = new Date(0); // max from date
let toDate = new Date(); // max to date
if (req.query.from) {
fromDate = new Date(req.query.from); // if (from) provided then setting up new date object with it
}
if (req.query.to) {
toDate = new Date(req.query.to); // if (to) provided then setting up new date object with it
}
// --- converting dates to unix-timelapse --- //
fromDate = fromDate.getTime();
toDate = toDate.getTime();
// --- Comparing Dates with Filters & returning||saving --- //
_user.log = _user.log.filter((record) => {
let unixLogDate = new Date(record.date).getTime();
return unixLogDate >= fromDate && unixLogDate <= toDate;
});
}
// --- If Limit is provided --- //
if (req.query.limit) {
let { limit } = req.query;
_user.log = _user.log.slice(0, limit);
}
// --- Sending Back The Response --- //
res.json({
username: _user.username,
count: _user.log.length,
_id: _user._id,
log: _user.log,
});
});
// --- Server Starts Here --- //
async function START_SERVER() {
// await ConnectDB();
// --- Server Listening --- //
const listener = await app.listen(process.env.PORT || 3000, () => {
console.log("Your app is listening on port " + listener.address().port);
});
}
//--- Starting Server && Mongoo Connection --- Deployment(heroku) has Passed. //
START_SERVER();
//for local testing start this server instead.
/*
app.listen(4500,()=>{
console.log("Your app is listening on port " + 4500);
});
*/