I’m pretty sure my output looks exactly like the example and I keep testing back and forth but for some reason the camperbot just doesn’t approve it. I feel really bad having to put up a thread for this and I do appreciate anybody with his/her spare time because I value that a lot.
To make things simple:
-
Challenge link: APIs and Microservices Projects - Exercise Tracker
-
Repl link: Exercise Tracker (beware could be down)
-
Repl project: My code
-
I’ve met people who wanted code block in the past so I will put my code here anyway. Feel free to skip it if you don’t want to see:
const express = require('express');
const app = express();
const cors = require('cors');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
require('dotenv').config();
app.use(cors(), bodyParser.urlencoded({extended: false}));
app.use(express.static('public'))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html')
});
mongoose.connect(process.env.DB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
const userSchema = new mongoose.Schema({
username: String
});
const User = mongoose.model('User', userSchema);
unwrap = function({_id, username}) {
return {_id, username};
}
const exerciseSchema = new mongoose.Schema({
username: String,
userId: String,
description: String,
duration: Number,
date: Date
});
const Exercise = mongoose.model('Exercise', exerciseSchema);
exerciseUnwrap = function({username, userId, description, duration, date}) {
return {username, userId, description, duration, date};
}
app.post('/api/exercise/new-user', function(req, res) {
const newUser = new User({username: req.body.username});
newUser.save(function(err, data) {
if (err) console.log('error creating new user');
});
res.json(unwrap(newUser));
});
app.get('/api/exercise/users', function(req, res) {
User.find(function(err, data) {
if (err) console.log("cannot retrieve data");
else {
res.json(data);
}
});
});
app.post('/api/exercise/add', function(req, res) {
User.findOne({_id: req.body.userId}, function(err, userInstance) {
if (err) console.log('cannot find user');
else {
const newExercise = new Exercise({
username: userInstance.username,
userId: req.body.userId,
description: req.body.description,
duration: req.body.duration,
date: req.body.date || Date()
});
newExercise.save(function(err, data) {
if (err) {
console.log('error creating new exercise');
console.log(newExercise);
}
});
const output = {
_id: userInstance._id,
username: userInstance.username,
date: newExercise.date.toDateString(),
duration: newExercise.duration,
description: newExercise.description
}
// console.log(output);
res.json(output);
}
});
});
app.get('/api/exercise/log', function(req, res) {
User.findOne({_id: req.query.userId}, function(err, user) {
if (err) console.log('error retrieving user');
const options = {};
if (req.query.limit) {
options.limit = req.query.limit
}
const query = {userId: req.query.userId};
if (req.query.from) {
if (query.date) query.date.$gte = (new Date(req.query.from)).toDateString();
else query.date = {$gte : (new Date(req.query.from)).toDateString()};
}
if (req.query.to) {
if (query.date) query.date.$lte = (new Date(req.query.to)).toDateString();
else query.date = {$lte : (new Date(req.query.to)).toDateString()}
}
Exercise.find({userId: req.query.userId}, options).sort({'date': 'desc'}).select('-_id description duration date').exec(function(err, data) {
if (err) console.log('error retrieving activities');
else {
const output = {
_id: user._id,
username: user.username
}
if (query.date) {
if (query.date.$gte) output.from = query.date.$gte;
if (query.date.$lte) output.to = query.date.$lte;
}
output.count = data.length;
output.log = data.map((e) => {
return {
description: e.description,
duration: e.duration,
date: e.date.toDateString()
}
})
res.json(output);
}
});
});
});
const listener = app.listen(process.env.PORT || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
});
Wish you all a great week ahead. For my debugging hours never ending.