mirror of
https://codeberg.org/andyscott/ExerciseTracker.git
synced 2024-12-21 11:43:10 -05:00
REST API configured for GET, POST, and PUT
This commit is contained in:
parent
0ade677756
commit
0484852895
4 changed files with 3742 additions and 0 deletions
119
exercises/exercises_controller.mjs
Normal file
119
exercises/exercises_controller.mjs
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
import "dotenv/config";
|
||||||
|
import express from "express";
|
||||||
|
import asyncHandler from "express-async-handler";
|
||||||
|
import * as exercises from "./exercises_model.mjs";
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
const PORT = process.env.PORT;
|
||||||
|
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validates date format
|
||||||
|
* @param {string} date
|
||||||
|
* Return true if the date format is MM-DD-YY
|
||||||
|
*/
|
||||||
|
function isDateValid(date) {
|
||||||
|
const format = /^\d\d-\d\d-\d\d$/;
|
||||||
|
return format.test(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
app.get(
|
||||||
|
"/exercises",
|
||||||
|
asyncHandler(async (req, res) => {
|
||||||
|
let filter = {};
|
||||||
|
const exercise = await exercises.findExercises(filter);
|
||||||
|
res.status(200).json(exercise);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
app.get(
|
||||||
|
"/exercises/:_id",
|
||||||
|
asyncHandler(async (req, res) => {
|
||||||
|
const exerciseId = req.params._id;
|
||||||
|
const exercise = await exercises.findExerciseById(exerciseId);
|
||||||
|
if (exercise !== null) {
|
||||||
|
res.status(200).json(exercise);
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ Error: "Not found" });
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
app.post(
|
||||||
|
"/exercises",
|
||||||
|
asyncHandler(async (req, res) => {
|
||||||
|
let isValid;
|
||||||
|
if (req.body.name === "" || typeof req.body.name !== "string") {
|
||||||
|
isValid = false;
|
||||||
|
} else if (typeof req.body.reps !== "number" || req.body.reps < 1) {
|
||||||
|
isValid = false;
|
||||||
|
} else if (typeof req.body.weight !== "number" || req.body.weight < 1) {
|
||||||
|
isValid = false;
|
||||||
|
} else if (req.body.unit !== "lbs" && req.body.unit !== "kgs") {
|
||||||
|
isValid = false;
|
||||||
|
} else if (!isDateValid(req.body.date)) {
|
||||||
|
isValid = false;
|
||||||
|
} else {
|
||||||
|
isValid = true;
|
||||||
|
}
|
||||||
|
if (isValid) {
|
||||||
|
const exercise = await exercises.createExercise(
|
||||||
|
req.body.name,
|
||||||
|
req.body.reps,
|
||||||
|
req.body.weight,
|
||||||
|
req.body.unit,
|
||||||
|
req.body.date
|
||||||
|
);
|
||||||
|
res.status(201).json(exercise);
|
||||||
|
} else {
|
||||||
|
res.status(400).json({ Error: "Invalid request" });
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
app.put(
|
||||||
|
"/exercises/:_id",
|
||||||
|
asyncHandler(async (req, res) => {
|
||||||
|
let isValid;
|
||||||
|
let resultVal;
|
||||||
|
if (req.body.name === "" || typeof req.body.name !== "string") {
|
||||||
|
isValid = false;
|
||||||
|
} else if (typeof req.body.reps !== "number" || req.body.reps < 1) {
|
||||||
|
isValid = false;
|
||||||
|
} else if (typeof req.body.weight !== "number" || req.body.weight < 1) {
|
||||||
|
isValid = false;
|
||||||
|
} else if (req.body.unit !== "lbs" && req.body.unit !== "kgs") {
|
||||||
|
isValid = false;
|
||||||
|
} else if (!isDateValid(req.body.date)) {
|
||||||
|
isValid = false;
|
||||||
|
} else {
|
||||||
|
isValid = true;
|
||||||
|
}
|
||||||
|
if (isValid) {
|
||||||
|
const update = {};
|
||||||
|
update.name = req.body.name;
|
||||||
|
update.reps = req.body.reps;
|
||||||
|
update.weight = req.body.weight;
|
||||||
|
update.unit = req.body.unit;
|
||||||
|
update.date = req.body.date;
|
||||||
|
resultVal = await exercises.updateExercise(
|
||||||
|
{ _id: req.params._id },
|
||||||
|
update
|
||||||
|
);
|
||||||
|
if (resultVal > 0) {
|
||||||
|
const exercise = await exercises.findExerciseById(req.params._id);
|
||||||
|
res.status(200).json(exercise);
|
||||||
|
} else {
|
||||||
|
res.status(404).json({ Error: "Not Found" });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
res.status(400).json({ Error: "Invalid request" });
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
console.log(`Server listening on port ${PORT}...`);
|
||||||
|
});
|
58
exercises/exercises_model.mjs
Normal file
58
exercises/exercises_model.mjs
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
import mongoose from "mongoose";
|
||||||
|
import "dotenv/config";
|
||||||
|
|
||||||
|
mongoose.connect(process.env.MONGODB_CONNECT_STRING, { useNewUrlParser: true });
|
||||||
|
|
||||||
|
// Connect to to the database
|
||||||
|
const db = mongoose.connection;
|
||||||
|
// Log successful database connection
|
||||||
|
db.once("open", () => {
|
||||||
|
console.log("Successfully connected to MongoDB using Mongoose!");
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define the exercises schema
|
||||||
|
*/
|
||||||
|
const exerciseSchema = mongoose.Schema({
|
||||||
|
name: { type: String, required: true },
|
||||||
|
reps: { type: Number, required: true },
|
||||||
|
weight: { type: Number, required: true },
|
||||||
|
unit: { type: String, required: true },
|
||||||
|
date: { type: String, required: true },
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile the model from the schema
|
||||||
|
*/
|
||||||
|
const Exercise = mongoose.model("Exercise", exerciseSchema);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new exercise
|
||||||
|
*/
|
||||||
|
const createExercise = async (name, reps, weight, unit, date) => {
|
||||||
|
const exercise = new Exercise({
|
||||||
|
name: name,
|
||||||
|
reps: reps,
|
||||||
|
weight: weight,
|
||||||
|
unit: unit,
|
||||||
|
date: date,
|
||||||
|
});
|
||||||
|
return exercise.save();
|
||||||
|
};
|
||||||
|
|
||||||
|
const findExercises = async (filter) => {
|
||||||
|
const query = Exercise.find(filter);
|
||||||
|
return query.exec();
|
||||||
|
};
|
||||||
|
|
||||||
|
const findExerciseById = async (_id) => {
|
||||||
|
const query = Exercise.findById(_id);
|
||||||
|
return query.exec();
|
||||||
|
};
|
||||||
|
|
||||||
|
const updateExercise = async (_id, update) => {
|
||||||
|
const result = await Exercise.updateOne(_id, update);
|
||||||
|
return result.modifiedCount;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { createExercise, findExerciseById, findExercises, updateExercise };
|
3546
exercises/package-lock.json
generated
Normal file
3546
exercises/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
19
exercises/package.json
Normal file
19
exercises/package.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"name": "exercises",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "exercises_controller.mjs",
|
||||||
|
"scripts": {
|
||||||
|
"start": "nodemon exercises_controller.mjs",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "Andrew Scott",
|
||||||
|
"license": "ISC",
|
||||||
|
"dependencies": {
|
||||||
|
"dotenv": "^16.0.1",
|
||||||
|
"express": "^4.18.1",
|
||||||
|
"express-async-handler": "^1.2.0",
|
||||||
|
"mongoose": "^6.3.5",
|
||||||
|
"nodemon": "^2.0.16"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue