Imp: store date in ticks and return as string and more
Rename log sub-collection to logs
This commit is contained in:
parent
ed0c3ac118
commit
cfa55c020f
39
index.js
39
index.js
@ -15,11 +15,11 @@ 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 },
|
||||||
date: { type: String, required: true },
|
date: { type: Number, required: true },
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
@ -65,21 +65,24 @@ app.get("/api/users", (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const isValidDate = (dateString) => {
|
|
||||||
if (!Number.isNaN(Number(dateString))) return true;
|
|
||||||
const date = new Date(dateString);
|
|
||||||
return !Number.isNaN(date.getTime());
|
|
||||||
};
|
|
||||||
|
|
||||||
app.post("/api/users/:_id/exercises", (req, res) => {
|
app.post("/api/users/:_id/exercises", (req, res) => {
|
||||||
const _id = req.params._id;
|
const _id = req.params._id;
|
||||||
const description = req.body.description;
|
const description = req.body.description;
|
||||||
const duration = req.body.duration;
|
const duration = req.body.duration;
|
||||||
const date = req.body.date ? req.body.date : new Date().toDateString();
|
|
||||||
if (!isValidDate(date)) {
|
let date = req.body.date;
|
||||||
|
// current time in tick if no date specified
|
||||||
|
if (!date) date = new Date().getTime();
|
||||||
|
// Convert String to Number if date specified in ticks
|
||||||
|
if (!Number.isNaN(Number(date))) date = Number(date);
|
||||||
|
// get date ticks from ticks Number or date String
|
||||||
|
date = new Date(date).getTime();
|
||||||
|
// date will be NaN if invalid date String is provided
|
||||||
|
if (Number.isNaN(Number(date))) {
|
||||||
res.status(400);
|
res.status(400);
|
||||||
return res.json({ error: `Input date: '${date}' is not valid` });
|
return res.json({ error: `Input date: '${req.body.date}' is not valid` });
|
||||||
}
|
}
|
||||||
|
|
||||||
exerciseLogs
|
exerciseLogs
|
||||||
.exists({ _id: _id })
|
.exists({ _id: _id })
|
||||||
.then((userExists) => {
|
.then((userExists) => {
|
||||||
@ -89,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,
|
||||||
@ -104,7 +107,7 @@ app.post("/api/users/:_id/exercises", (req, res) => {
|
|||||||
_id: _id,
|
_id: _id,
|
||||||
description: description,
|
description: description,
|
||||||
duration: duration,
|
duration: duration,
|
||||||
date: date,
|
date: new Date(date).toDateString(),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
res.json(500);
|
res.json(500);
|
||||||
@ -134,11 +137,15 @@ app.get("/api/users/:_id/logs", (req, res) => {
|
|||||||
const _id = req.params._id;
|
const _id = req.params._id;
|
||||||
exerciseLogs
|
exerciseLogs
|
||||||
.findById(_id)
|
.findById(_id)
|
||||||
.select({ "log._id": false })
|
.select({ "logs._id": false })
|
||||||
.then((excLogs) => {
|
.then((excLogs) => {
|
||||||
if (excLogs) {
|
if (excLogs) {
|
||||||
const excLogsJson = excLogs.toJSON();
|
let excLogsJson = excLogs.toJSON();
|
||||||
excLogsJson.count = excLogsJson.log.length;
|
excLogsJson.count = excLogsJson.logs.length;
|
||||||
|
excLogsJson.logs = excLogsJson.logs.map((log) => {
|
||||||
|
log.date = new Date(log.date).toDateString();
|
||||||
|
return log;
|
||||||
|
});
|
||||||
return res.json(excLogsJson);
|
return res.json(excLogsJson);
|
||||||
}
|
}
|
||||||
res.status(400);
|
res.status(400);
|
||||||
|
Loading…
Reference in New Issue
Block a user