Compare commits
No commits in common. "e4bf128b29f5dbaeaa779a60cb80d5b6209205d2" and "cfa55c020f32dbde7af5701dd21b49ad790b0ad5" have entirely different histories.
e4bf128b29
...
cfa55c020f
56
index.js
56
index.js
@ -15,7 +15,7 @@ mongoose.connect(MONGODB_URI, {
|
|||||||
|
|
||||||
const exerciseLogsSchema = new mongoose.Schema({
|
const exerciseLogsSchema = new mongoose.Schema({
|
||||||
username: { type: String, required: true },
|
username: { type: String, required: true },
|
||||||
log: [
|
logs: [
|
||||||
{
|
{
|
||||||
description: { type: String, required: true },
|
description: { type: String, required: true },
|
||||||
duration: { type: Number, required: true },
|
duration: { type: Number, required: true },
|
||||||
@ -65,7 +65,12 @@ app.get("/api/users", (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const getDateInTicks = (date) => {
|
app.post("/api/users/:_id/exercises", (req, res) => {
|
||||||
|
const _id = req.params._id;
|
||||||
|
const description = req.body.description;
|
||||||
|
const duration = req.body.duration;
|
||||||
|
|
||||||
|
let date = req.body.date;
|
||||||
// current time in tick if no date specified
|
// current time in tick if no date specified
|
||||||
if (!date) date = new Date().getTime();
|
if (!date) date = new Date().getTime();
|
||||||
// Convert String to Number if date specified in ticks
|
// Convert String to Number if date specified in ticks
|
||||||
@ -73,15 +78,6 @@ const getDateInTicks = (date) => {
|
|||||||
// get date ticks from ticks Number or date String
|
// get date ticks from ticks Number or date String
|
||||||
date = new Date(date).getTime();
|
date = new Date(date).getTime();
|
||||||
// date will be NaN if invalid date String is provided
|
// date will be NaN if invalid date String is provided
|
||||||
return date;
|
|
||||||
};
|
|
||||||
|
|
||||||
app.post("/api/users/:_id/exercises", (req, res) => {
|
|
||||||
const _id = req.params._id;
|
|
||||||
const description = req.body.description;
|
|
||||||
const duration = req.body.duration;
|
|
||||||
|
|
||||||
let date = getDateInTicks(req.body.date);
|
|
||||||
if (Number.isNaN(Number(date))) {
|
if (Number.isNaN(Number(date))) {
|
||||||
res.status(400);
|
res.status(400);
|
||||||
return res.json({ error: `Input date: '${req.body.date}' is not valid` });
|
return res.json({ error: `Input date: '${req.body.date}' is not valid` });
|
||||||
@ -96,7 +92,7 @@ app.post("/api/users/:_id/exercises", (req, res) => {
|
|||||||
{ _id: _id },
|
{ _id: _id },
|
||||||
{
|
{
|
||||||
$push: {
|
$push: {
|
||||||
log: {
|
logs: {
|
||||||
description: description,
|
description: description,
|
||||||
duration: duration,
|
duration: duration,
|
||||||
date: date,
|
date: date,
|
||||||
@ -139,44 +135,14 @@ app.post("/api/users/:_id/exercises", (req, res) => {
|
|||||||
|
|
||||||
app.get("/api/users/:_id/logs", (req, res) => {
|
app.get("/api/users/:_id/logs", (req, res) => {
|
||||||
const _id = req.params._id;
|
const _id = req.params._id;
|
||||||
const limit = req.query.limit
|
|
||||||
? Math.trunc(Number(req.query.limit))
|
|
||||||
: Number.MAX_SAFE_INTEGER;
|
|
||||||
if (Number.isNaN(limit)) {
|
|
||||||
res.status(400);
|
|
||||||
return res.json({
|
|
||||||
error: `Query key 'limit''s value '${req.query.limit}' is not a number`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const from = req.query.from ? getDateInTicks(req.query.from) : 0;
|
|
||||||
if (Number.isNaN(from)) {
|
|
||||||
res.status(400);
|
|
||||||
return res.json({
|
|
||||||
error: `Query key 'from''s value '${req.query.from}' is invalid for date`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const to = req.query.to
|
|
||||||
? getDateInTicks(req.query.to)
|
|
||||||
: Number.MAX_SAFE_INTEGER;
|
|
||||||
if (Number.isNaN(to)) {
|
|
||||||
res.status(400);
|
|
||||||
return res.json({
|
|
||||||
error: `Query key 'to''s value '${req.query.to}' is invalid for date`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
exerciseLogs
|
exerciseLogs
|
||||||
.findById(_id)
|
.findById(_id)
|
||||||
.select({ "log._id": false, __v: false })
|
.select({ "logs._id": false })
|
||||||
.then((excLogs) => {
|
.then((excLogs) => {
|
||||||
if (excLogs) {
|
if (excLogs) {
|
||||||
let excLogsJson = excLogs.toJSON();
|
let excLogsJson = excLogs.toJSON();
|
||||||
excLogsJson.log = excLogsJson.log.filter(
|
excLogsJson.count = excLogsJson.logs.length;
|
||||||
(log) => log.date >= from && log.date <= to
|
excLogsJson.logs = excLogsJson.logs.map((log) => {
|
||||||
);
|
|
||||||
excLogsJson.log.sort((a, b) => a.date - b.date);
|
|
||||||
excLogsJson.log = excLogsJson.log.slice(0, limit);
|
|
||||||
excLogsJson.count = excLogsJson.log.length;
|
|
||||||
excLogsJson.log = excLogsJson.log.map((log) => {
|
|
||||||
log.date = new Date(log.date).toDateString();
|
log.date = new Date(log.date).toDateString();
|
||||||
return log;
|
return log;
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user