|
const express = require('express') |
|
const app = express() |
|
const cors = require('cors') |
|
var mongoose = require('mongoose'); |
|
const bodyParser = require('body-parser'); |
|
require('dotenv').config() |
|
|
|
mongoose.connect("mongodb+srv://blangue:roueca69@test.i0gbwvk.mongodb.net/test?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true }); |
|
|
|
app.use(bodyParser.urlencoded({extended : false})); |
|
|
|
/* Exercise class */ |
|
let exerciseSchema = new mongoose.Schema({ |
|
username: String, |
|
description: String, |
|
duration: Number, |
|
date: String |
|
}); |
|
const Exercise = mongoose.model("Exercise", exerciseSchema); |
|
|
|
/* User class */ |
|
let userSchema = new mongoose.Schema({ |
|
username: String |
|
}); |
|
const User = mongoose.model("User", userSchema); |
|
|
|
/* Log class */ |
|
let logSchema = new mongoose.Schema({ |
|
username: String, |
|
count: Number, |
|
log: [{description: String, |
|
duration: Number, |
|
date: String}] |
|
}); |
|
const Log = mongoose.model("Log", logSchema); |
|
|
|
|
|
app.use(cors()) |
|
app.use(express.static('public')) |
|
app.get('/', (req, res) => { |
|
res.sendFile(__dirname + '/views/index.html') |
|
}); |
|
|
|
//POST /api/users |
|
app.post('/api/users', (req,res)=>{ |
|
console.log("New User: "+req.body.username); |
|
var user = new User({username: req.body.username}); |
|
User.create(user); |
|
res.json({username: user.username, _id: user._id}); |
|
}); |
|
|
|
//GET /api/users |
|
app.get('/api/users', (req,res,done)=>{ |
|
var query = User.find(); |
|
query.exec((err, data)=>{ |
|
if(err) return console.log(err); |
|
res.send(data); |
|
done(null, data); |
|
}); |
|
}); |
|
|
|
//POST /api/users/:_id/exercises |
|
app.post('/api/users/:id/exercises', (req,res)=>{ |
|
//init |
|
var id = req.params.id; |
|
var description = req.body.description; |
|
var duration = req.body.duration; |
|
var date = new Date(req.body.date); |
|
if (date.toString() === "Invalid Date") date = Date.now(); |
|
//confirm |
|
console.log("Received new exercise: id= "+id +" decri= "+ description +" duration= "+ duration + " date= "+ date.toString()); |
|
// new exercise entry |
|
User.findById(id, (err,data)=>{ |
|
if(err) return console.error(err); |
|
console.log(data); |
|
res.json({_id: id, username: data.username, date: new Date(date).toDateString(), duration: Number(duration), description: description}); |
|
var exe = new Exercise({username: data.username, description: description, duration: duration, date: new Date(date).toDateString()}); |
|
Exercise.create(exe); |
|
}); |
|
}); |
|
|
|
//GET /api/users/:_id/logs |
|
app.get('/api/users/:_id/logs', ( req,res)=>{ |
|
console.log("Request received: from: "+ req.originalUrl); |
|
var temoin = 0; |
|
User.findById(req.params._id, (err, user)=>{ |
|
if(err) return console.error(err); |
|
Exercise.find({username: user.username}, (err, list)=>{ |
|
if(err) return console.error(err); |
|
//Execute from/to queries |
|
var fromDate = new Date(req.query.from); |
|
var toDate = new Date(req.query.to); |
|
var limit = Number(req.query.limit); |
|
var realLimit = list.length; |
|
if(limit) realLimit = Math.min(list.length, limit); |
|
temoin = fromDate? 1 : 0; |
|
temoin = toDate? (temoin == 1 ? 3 : 2) : temoin; |
|
if(fromDate.toString() != "Invalid Date" || toDate.toString() != "Invalid Date"){ |
|
for(let i=0; i<realLimit; i++){ |
|
var date = new Date(list[i].date); |
|
if(new Date(date).getTime() < new Date(fromDate).getTime() || new Date(date).getTime() >= new Date(toDate).getTime()){ |
|
list.splice(i, 1); |
|
i--; |
|
realLimit--; |
|
}; |
|
} |
|
if(limit) list.length = limit; |
|
if(temoin == 3) res.json({_id: user._id, username: user.username, from: new Date(fromDate).toDateString(), to: new Date(toDate).toDateString(), count: list.length, log: JSON.parse(JSON.stringify(list)) |
|
}) |
|
else if(temoin == 1) res.json({_id: user._id, username: user.username, from: new Date(fromDate).toDateString(), count: list.length, log: JSON.parse(JSON.stringify(list)) |
|
}) |
|
else if(temoin == 2) res.json({_id: user._id, username: user.username, to: new Date(toDate).toDateString(), count: list.length, log: JSON.parse(JSON.stringify(list)) |
|
}) |
|
} |
|
else { |
|
list.length = limit? limit : list.length; |
|
res.json({_id: user._id, username: user.username, count: list.length, log: JSON.parse(JSON.stringify(list))}) |
|
} |
|
}).select({__v:0,_id:0, username:0}) |
|
}); |
|
}); |
|
|
|
const listener = app.listen(process.env.PORT || 3000, () => { |
|
console.log('Your app is listening on port ' + listener.address().port) |
|
}) |