-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (138 loc) · 3.96 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
const express = require('express')
const app = express()
const cors = require('cors')
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const moment = require('moment');
require('dotenv').config()
mongoose.connect(process.env.URI, { useNewUrlParser: true, useUnifiedTopology: true }).then(
() => console.log("Connection Successfull\n","Connection Status: ",mongoose.connection.readyState)).catch(
console.log("Database Error\n","Connection Status: ",mongoose.connection.readyState));
app.use(cors());
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
//user Schema
const userSchema = new mongoose.Schema(
{
username: { type : String, required: [true, "User Name is required!"] },
count: 0,
log: [{
description: String,
duration: Number,
date: String,
}],
}
);
//user model
let user = new mongoose.model('user', userSchema);
app.post('/api/users', (req, res) => {
let name = req.body.username;
user.findOne({username: name}, (err, data) => {
if(data){
res.json({username: data.username, _id: data._id});
}
else{
user.create({username: name}, (err, data) => {
if (err) return console.error(err);
res.json({username: data.username, _id: data._id});
})
}
});
});
app.post('/api/users/:_id/exercises', (req, res) => {
let id = req.params._id;
let des = req.body.description;
let duration = req.body.duration;
let date = new Date(req.body.date).toDateString();
if(!(req.body.date)){
date = new Date().toDateString();
}
user.findById(id, (err, data) => {
if (err) {
res.string('ERROR: User doesnt exist!')
}
else { data.log.push({
description: des,
duration: duration,
date: date,
});
data.save((err, updated)=> {
if (err) return console.log(err);
if(updated) {
let index = (updated.log.length - 1)
res.json({
_id: updated._id,
username: updated.username,
date: updated.log[index]['date'],
duration: updated.log[index]['duration'],
description: updated.log[index]['description'],
});
}
});
}
});
});
app.get('/api/users/:_id/logs', (req, res)=> {
const {from, to, limit} = req.query;
let id = req.params._id;
const startDate = moment(from)
const endDate = moment(to)
let d = startDate
user.findById(id, (err, data) => {
if(err) return console.log(err);
let log = data.log;
if(from === undefined || to === undefined){
let array = [];
for(let i in log){
if(array >= limit){break;}
let info = {description: log[i]['description'],
duration: log[i]['duration'],
date: new Date((log[i]['date'])).toDateString()}
array.push(info);
}
res.json({
_id: data._id,
username: data.username,
count: array.length,
log: array
});
}
else { let matches = [];
while (+d.toDate() < +endDate.toDate()) {
let next_date = (new Date(d).toDateString());
d = d.add(1, 'days')
for(let i in log){
if(log[i]['date'] === next_date){
if(matches >= limit){break;}
let info = {description: log[i]['description'],
duration: log[i]['duration'],
date: new Date((log[i]['date'])).toDateString()}
matches.push(info);
}
}
}
res.json({
_id: data._id,
username: data.username,
count: matches.length,
log: matches
});}
})
});
app.get('/api/users', (req, res) => {
user.find({}, (err, data) => {
if (err) {
res.send('NO Users in database');
}
else{
res.json(data);
}
});
});
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})