2023-03-28 07:31:44 +02:00
|
|
|
const express = require("express");
|
|
|
|
const app = express();
|
|
|
|
const cors = require("cors");
|
2023-03-28 16:56:48 +02:00
|
|
|
const mongoose = require("mongoose");
|
2023-03-28 07:31:44 +02:00
|
|
|
require("dotenv").config();
|
2017-02-18 20:06:54 +01:00
|
|
|
|
2023-03-28 07:31:44 +02:00
|
|
|
const PORT = process.env.PORT || 16749;
|
2023-03-28 16:56:48 +02:00
|
|
|
const MONGODB_URI = process.env.MONGODB_URI || "";
|
|
|
|
|
|
|
|
mongoose.connect(MONGODB_URI, {
|
|
|
|
// @ts-ignore
|
|
|
|
useNewUrlParser: true,
|
|
|
|
useUnifiedTopology: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const exerciseLogsSchema = new mongoose.Schema({
|
|
|
|
username: { type: String, required: true },
|
2023-03-29 05:47:36 +02:00
|
|
|
logs: [
|
2023-03-28 16:56:48 +02:00
|
|
|
{
|
|
|
|
description: { type: String, required: true },
|
|
|
|
duration: { type: Number, required: true },
|
2023-03-29 05:47:36 +02:00
|
|
|
date: { type: Number, required: true },
|
2023-03-28 16:56:48 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const exerciseLogs = mongoose.model("ExerciseLogs", exerciseLogsSchema);
|
2017-02-18 20:06:54 +01:00
|
|
|
|
2023-03-28 07:31:44 +02:00
|
|
|
app.use(cors());
|
|
|
|
app.use(express.static("public"));
|
2023-03-28 16:56:48 +02:00
|
|
|
app.use(express.urlencoded({ extended: false }));
|
2023-03-28 07:31:44 +02:00
|
|
|
app.get("/", (req, res) => {
|
|
|
|
res.sendFile(__dirname + "/views/index.html");
|
|
|
|
});
|
2017-02-18 20:06:54 +01:00
|
|
|
|
2023-03-28 16:56:48 +02:00
|
|
|
app.post("/api/users", (req, res) => {
|
|
|
|
const username = req.body.username;
|
|
|
|
const exerciseLogsModel = new exerciseLogs({ username: username });
|
|
|
|
exerciseLogsModel
|
|
|
|
.save()
|
|
|
|
.then((user) => {
|
|
|
|
if (user) return res.json({ username: user.username, _id: user._id });
|
|
|
|
res.status(500);
|
|
|
|
res.json({ error: `Error creating user: ${username}` });
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
res.status(500);
|
|
|
|
return res.json({
|
|
|
|
error: `Error type 2 while creating user: ${username}`,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-28 17:34:09 +02:00
|
|
|
app.get("/api/users", (req, res) => {
|
|
|
|
exerciseLogs
|
|
|
|
.find()
|
|
|
|
.select({ username: true, _id: true })
|
|
|
|
.exec()
|
|
|
|
.then((users) => {
|
|
|
|
res.json(users);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
res.sendStatus(500);
|
|
|
|
res.json({ error: `Error while retrieving users list:\n${err}` });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-29 04:06:13 +02:00
|
|
|
app.post("/api/users/:_id/exercises", (req, res) => {
|
|
|
|
const _id = req.params._id;
|
|
|
|
const description = req.body.description;
|
|
|
|
const duration = req.body.duration;
|
2023-03-29 05:47:36 +02:00
|
|
|
|
|
|
|
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))) {
|
2023-03-29 04:16:23 +02:00
|
|
|
res.status(400);
|
2023-03-29 05:47:36 +02:00
|
|
|
return res.json({ error: `Input date: '${req.body.date}' is not valid` });
|
2023-03-29 04:16:23 +02:00
|
|
|
}
|
2023-03-29 05:47:36 +02:00
|
|
|
|
2023-03-29 04:06:13 +02:00
|
|
|
exerciseLogs
|
|
|
|
.exists({ _id: _id })
|
|
|
|
.then((userExists) => {
|
|
|
|
if (userExists) {
|
|
|
|
exerciseLogs
|
|
|
|
.updateOne(
|
|
|
|
{ _id: _id },
|
|
|
|
{
|
|
|
|
$push: {
|
2023-03-29 05:47:36 +02:00
|
|
|
logs: {
|
2023-03-29 04:06:13 +02:00
|
|
|
description: description,
|
|
|
|
duration: duration,
|
|
|
|
date: date,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ runValidators: true }
|
|
|
|
)
|
|
|
|
.then((excLogs) => {
|
|
|
|
if (excLogs.modifiedCount) {
|
|
|
|
res.json({
|
|
|
|
_id: _id,
|
|
|
|
description: description,
|
|
|
|
duration: duration,
|
2023-03-29 05:47:36 +02:00
|
|
|
date: new Date(date).toDateString(),
|
2023-03-29 04:06:13 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.json(500);
|
|
|
|
res.json({
|
|
|
|
error: `Failed to add exercise logs to database:\n${excLogs}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
res.status(500);
|
|
|
|
res.json({
|
|
|
|
error: `Failed to add exercise logs to database:\n${err}`,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.status(403);
|
|
|
|
res.json({ error: `No user with id: ${_id} exists` });
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
res.status(500);
|
|
|
|
res.json({ error: "Failed to check user in database" });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-29 04:41:49 +02:00
|
|
|
app.get("/api/users/:_id/logs", (req, res) => {
|
|
|
|
const _id = req.params._id;
|
|
|
|
exerciseLogs
|
|
|
|
.findById(_id)
|
2023-03-29 05:47:36 +02:00
|
|
|
.select({ "logs._id": false })
|
2023-03-29 04:41:49 +02:00
|
|
|
.then((excLogs) => {
|
|
|
|
if (excLogs) {
|
2023-03-29 05:47:36 +02:00
|
|
|
let excLogsJson = excLogs.toJSON();
|
|
|
|
excLogsJson.count = excLogsJson.logs.length;
|
|
|
|
excLogsJson.logs = excLogsJson.logs.map((log) => {
|
|
|
|
log.date = new Date(log.date).toDateString();
|
|
|
|
return log;
|
|
|
|
});
|
2023-03-29 04:41:49 +02:00
|
|
|
return res.json(excLogsJson);
|
|
|
|
}
|
|
|
|
res.status(400);
|
|
|
|
res.json({ error: `No user with id: '${_id}' exists` });
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
res.status(400);
|
|
|
|
res.json({ error: `Input user id: '${_id}' is malformed` });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-28 07:31:44 +02:00
|
|
|
const listener = app.listen(PORT, () => {
|
|
|
|
console.log("Your app is listening on port " + listener.address().port);
|
|
|
|
});
|