Skip to content

Commit 13fca1b

Browse files
updated exercise router
1 parent 82fdb6c commit 13fca1b

File tree

13 files changed

+280
-42
lines changed

13 files changed

+280
-42
lines changed

app/exercise/exercise.model.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,60 @@
11
// Exercises should have the following:
22
// 1. Name
33
// 2. Bodypart
4-
//3. Resistence or cardiovascular activity
4+
//3. Resistence or cardiovascular activity
5+
6+
7+
const mongoose = require("mongoose");
8+
const Joi = require("joi");
9+
10+
const exerciseSchema = new mongoose.Schema({
11+
// user: {
12+
// type: mongoose.Schema.Types.ObjectId,
13+
// ref: "user"
14+
// },
15+
exercise: {
16+
type: String,
17+
required: true
18+
},
19+
bodypart: {
20+
type: String,
21+
required: true
22+
},
23+
ex_type: {
24+
type: String,
25+
enum: ["cardiovascular", "strength training"]
26+
}
27+
});
28+
29+
exerciseSchema.methods.serialize = function() {
30+
let user;
31+
if (typeof this.user.serialize === "function") {
32+
user = this.user.serialize();
33+
} else {
34+
user = this.user;
35+
}
36+
return {
37+
id: this._id,
38+
// user: user,
39+
exercise: this.exercise,
40+
bodypart: this.bodypart,
41+
ex_type: this.ex_type
42+
};
43+
};
44+
45+
const Exercise = mongoose.model("exercise", exerciseSchema);
46+
47+
const ExerciseJoiSchema = Joi.object().keys({
48+
// user: Joi.string().optional(),
49+
exercise: Joi.string()
50+
.min(1)
51+
.required(),
52+
bodypart: Joi.string()
53+
.min(1)
54+
.required(),
55+
ex_type: Joi.string().min(1)
56+
});
57+
58+
module.exports = {
59+
Exercise
60+
};

app/exercise/exercise.router.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const express = require('express');
2+
const Joi = require('joi');
3+
const exerciseRouter = express.Router();
4+
const path = require('path');
5+
6+
const {
7+
HTTP_STATUS_CODES
8+
} = require('../config');
9+
const {
10+
jwtPassportMiddleware
11+
} = require('../auth/auth.strategy');
12+
const {
13+
Exercise,
14+
ExerciseJoiSchema
15+
} = require('./exercise.model');
16+
17+
//CREATE NEW Exercise
18+
exerciseRouter.post('/', (request, response) => {
19+
20+
const newExercise = {
21+
// user: request.user.id,
22+
exercise: request.body.exercise,
23+
ex_type: request.body.ex_type,
24+
bodypart: request.body.bodypart
25+
26+
};
27+
28+
const validation = Joi.validate(newExercise, ExerciseJoiSchema);
29+
if (validation.error) {
30+
console.log("Validation Error")
31+
return response.status(HTTP_STATUS_CODES.BAD_REQUEST).json({
32+
error: validation.error
33+
});
34+
}
35+
36+
Exercise.create(newExercise)
37+
.then(createdExercise => {
38+
return response.status(HTTP_STATUS_CODES.CREATED).json(createdExercise.serialize());
39+
})
40+
.catch(error => {
41+
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).json(error);
42+
})
43+
})
44+
45+
46+
// RETRIEVE Exercises
47+
exerciseRouter.get('/', (request, response) => {
48+
console.log("Retrieving All Exercises");
49+
Exercise.find()
50+
.then(exercises => {
51+
// Step 2A: Return the correct HTTP status code, and the users correctly formatted via serialization.
52+
53+
return response.status(HTTP_STATUS_CODES.OK).json(
54+
exercises.map(exercise => exercise.serialize())
55+
);
56+
})
57+
.catch(error => {
58+
// Step 2B: If an error ocurred, return an error HTTP status code and the error in JSON format.
59+
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).json(error);
60+
});
61+
});
62+
// RETRIEVE ONE EXERCISE
63+
exerciseRouter.get('/:exerciseid', (request, response) => {
64+
// Step 1: Attempt to retrieve a specific user using Mongoose.Model.findById()
65+
// https://mongoosejs.com/docs/api.html#model_Model.findById
66+
Exercise.findById(request.params.exerciserid)
67+
.then(exercise => {
68+
// Step 2A: Return the correct HTTP status code, and the user correctly formatted via serialization.
69+
// return response.status(HTTP_STATUS_CODES.OK).json(user.serialize());
70+
return response.resolve('/api/exercises');
71+
72+
})
73+
.catch(error => {
74+
// Step 2B: If an error ocurred, return an error HTTP status code and the error in JSON format.
75+
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).json(error);
76+
});
77+
});
78+
79+
module.exports = {
80+
exerciseRouter
81+
};

app/server.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ const {
2323
const {
2424
workoutRouter
2525
} = require('./workout/workout.router');
26-
26+
const {
27+
exerciseRouter
28+
} = require('./exercise/exercise.router')
2729

2830

2931
let server;
@@ -41,6 +43,8 @@ app.use(express.urlencoded());
4143
app.use('/api/auth', authRouter);
4244
app.use('/api/user', userRouter); //Redirects all calls to /api/user to userRouter
4345
app.use('/api/home', workoutRouter);
46+
app.use('/api/exercises', exerciseRouter);
47+
4448

4549
app.use('*', function(req, res) {
4650
res.status(HTTP_STATUS_CODES.NOT_FOUND).json({

app/user/user.model.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const userSchema = new mongoose.Schema({
2525
}
2626
});
2727

28-
//Is this globablly accessible?
28+
2929
userSchema.methods.serialize = function() {
3030
return {
3131
id: this._id,

app/user/user.router.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const express = require('express');
2-
32
const Joi = require('joi');
43

54
const {
@@ -70,9 +69,12 @@ userRouter.get('/', (request, response) => {
7069
User.find()
7170
.then(users => {
7271
// Step 2A: Return the correct HTTP status code, and the users correctly formatted via serialization.
72+
7373
return response.status(HTTP_STATUS_CODES.OK).json(
7474
users.map(user => user.serialize())
7575
);
76+
77+
7678
})
7779
.catch(error => {
7880
// Step 2B: If an error ocurred, return an error HTTP status code and the error in JSON format.

app/workout/workout.model.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ const WorkoutJoiSchema = Joi.object().keys({
6161
reps: Joi.number()
6262
.min(1)
6363
.required(),
64-
weight: Joi.number().min(1)
64+
weight: Joi.number().min(1),
65+
date: Joi.date()
6566
});
6667

6768
module.exports = {

app/workout/workout.router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ const {
1515
} = require('./workout.model');
1616

1717
//CREATE NEW WORKOUT
18-
workoutRouter.post('/', jwtPassportMiddleware, (request, response) => {
18+
workoutRouter.post('/', (request, response) => {
1919

2020
const newWorkout = {
21-
user: request.user.id,
21+
// user: request.user.id,
2222
exercise: request.body.exercise,
2323
reps: request.body.reps,
2424
weight: request.body.weight,

exercises.json

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{"_id":{"$oid":"5c0a0b3ce046767d25548e5c"},"exercise":"Pull-Ups","bodypart":"back"}
2+
{"_id":{"$oid":"5c0a0b3ce046767d25548e5d"},"exercise":"Chin-Ups","bodypart":"back"}
3+
{"_id":{"$oid":"5c0a0b3ce046767d25548e5e"},"exercise":"Lat Pull-Downs","bodypart":"back"}
4+
{"_id":{"$oid":"5c0a0b3ce046767d25548e5f"},"exercise":"Bent Over Barbell ","bodypart":"back"}
5+
{"_id":{"$oid":"5c0a0b3ce046767d25548e60"},"exercise":"Dumbbell Rows","bodypart":"back"}
6+
{"_id":{"$oid":"5c0a0b3ce046767d25548e61"},"exercise":"T-Bar Rows","bodypart":"back"}
7+
{"_id":{"$oid":"5c0a0b3ce046767d25548e62"},"exercise":"Seated Cable Rows","bodypart":"back"}
8+
{"_id":{"$oid":"5c0a0b3ce046767d25548e63"},"exercise":"Barbell Rows","bodypart":"back"}
9+
{"_id":{"$oid":"5c0a0b3ce046767d25548e64"},"exercise":"Dumbbell Rows","bodypart":"back"}
10+
{"_id":{"$oid":"5c0a0b3ce046767d25548e65"},"exercise":"Machine Rows","bodypart":"back"}
11+
{"_id":{"$oid":"5c0a0b3ce046767d25548e66"},"exercise":"Inverted Rows","bodypart":"back"}
12+
{"_id":{"$oid":"5c0a0b3ce046767d25548e67"},"exercise":"Barbell Shrugs","bodypart":"back"}
13+
{"_id":{"$oid":"5c0a0b3ce046767d25548e68"},"exercise":"Dumbbell Shrugs","bodypart":"back"}
14+
{"_id":{"$oid":"5c0a0b3ce046767d25548e69"},"exercise":"Machine Shrugs","bodypart":"back"}
15+
{"_id":{"$oid":"5c0a0c96e046767d25548e6a"},"exercise":"Flat Barbell","bodypart":"chest"}
16+
{"_id":{"$oid":"5c0a0c96e046767d25548e6b"},"exercise":"Dumbbell Bench Press","bodypart":"chest"}
17+
{"_id":{"$oid":"5c0a0c96e046767d25548e6c"},"exercise":"Incline Barbell ","bodypart":"chest"}
18+
{"_id":{"$oid":"5c0a0c96e046767d25548e6d"},"exercise":"Dumbbell Bench Press","bodypart":"chest"}
19+
{"_id":{"$oid":"5c0a0c96e046767d25548e6e"},"exercise":"Decline Barbell ","bodypart":"chest"}
20+
{"_id":{"$oid":"5c0a0c96e046767d25548e6f"},"exercise":"Dumbbell Bench Press","bodypart":"chest"}
21+
{"_id":{"$oid":"5c0a0c96e046767d25548e70"},"exercise":"Flat Chest Press Machine","bodypart":"chest"}
22+
{"_id":{"$oid":"5c0a0c96e046767d25548e71"},"exercise":"Incline Chest Press Machine","bodypart":"chest"}
23+
{"_id":{"$oid":"5c0a0c96e046767d25548e72"},"exercise":"Decline Chest Press Machine","bodypart":"chest"}
24+
{"_id":{"$oid":"5c0a0c96e046767d25548e73"},"exercise":"Dips","bodypart":"chest"}
25+
{"_id":{"$oid":"5c0a0c96e046767d25548e74"},"exercise":"Dips (Machine)","bodypart":"chest"}
26+
{"_id":{"$oid":"5c0a0c96e046767d25548e75"},"exercise":"Push-Ups","bodypart":"chest"}
27+
{"_id":{"$oid":"5c0a0c96e046767d25548e76"},"exercise":"Flat Dumbbell Flyes","bodypart":"chest"}
28+
{"_id":{"$oid":"5c0a0c96e046767d25548e77"},"exercise":"Incline Dumbbell Flyes","bodypart":"chest"}
29+
{"_id":{"$oid":"5c0a0c96e046767d25548e78"},"exercise":"Decline Dumbbell Flyes","bodypart":"chest"}
30+
{"_id":{"$oid":"5c0a0c96e046767d25548e79"},"exercise":"Pec Deck Machine","bodypart":"chest"}
31+
{"_id":{"$oid":"5c0a0c96e046767d25548e7a"},"exercise":"Cable Crossovers","bodypart":"chest"}
32+
{"_id":{"$oid":"5c0a0c96e046767d25548e7b"},"exercise":"Cable Flyes","bodypart":"chest"}
33+
{"_id":{"$oid":"5c0a1146e166ad1d582509c3"},"exercise":"Dips (Machine)","bodypart":["chest","triceps"]}
34+
{"_id":{"$oid":"5c0a15c255eed58e66e75048"},"exercise":"Seated Overhead Barbell Press","bodypart":"shoulders"}
35+
{"_id":{"$oid":"5c0a15c255eed58e66e75049"},"exercise":"Seated Overhead Dumbbell Press","bodypart":"shoulders"}
36+
{"_id":{"$oid":"5c0a15c255eed58e66e7504a"},"exercise":"Standing Overhead Barbell ","bodypart":"shoulders"}
37+
{"_id":{"$oid":"5c0a15c255eed58e66e7504b"},"exercise":"Standing Overhead Dumbbell Press","bodypart":"shoulders"}
38+
{"_id":{"$oid":"5c0a15c255eed58e66e7504c"},"exercise":"Overhead Machine Press","bodypart":"shoulders"}
39+
{"_id":{"$oid":"5c0a15c255eed58e66e7504d"},"exercise":"Arnold Press","bodypart":"shoulders"}
40+
{"_id":{"$oid":"5c0a15c255eed58e66e7504e"},"exercise":"Barbell Upright Rows","bodypart":"shoulders"}
41+
{"_id":{"$oid":"5c0a15c255eed58e66e7504f"},"exercise":"Dumbbell Upright Rows","bodypart":"shoulders"}
42+
{"_id":{"$oid":"5c0a15c255eed58e66e75050"},"exercise":"Machine Upright Rows","bodypart":"shoulders"}
43+
{"_id":{"$oid":"5c0a15c255eed58e66e75051"},"exercise":"Dumbbell Lateral Raises ","bodypart":"shoulders"}
44+
{"_id":{"$oid":"5c0a15c255eed58e66e75052"},"exercise":"Cable Lateral Raises","bodypart":"shoulders"}
45+
{"_id":{"$oid":"5c0a15c255eed58e66e75053"},"exercise":"Machine Lateral Raises","bodypart":"shoulders"}
46+
{"_id":{"$oid":"5c0a15c255eed58e66e75054"},"exercise":"Dumbbell Front Raises","bodypart":"shoulders"}
47+
{"_id":{"$oid":"5c0a15c255eed58e66e75055"},"exercise":"Cable Front Raises","bodypart":"shoulders"}
48+
{"_id":{"$oid":"5c0a15c255eed58e66e75056"},"exercise":"Machine Front Raises","bodypart":"shoulders"}
49+
{"_id":{"$oid":"5c0a15c255eed58e66e75057"},"exercise":"Barbell Rear Delt Flyes","bodypart":"shoulders"}
50+
{"_id":{"$oid":"5c0a15c255eed58e66e75058"},"exercise":"Dumbbell Rear Delt Flyes","bodypart":"shoulders"}
51+
{"_id":{"$oid":"5c0a15c255eed58e66e75059"},"exercise":"Reverse Pec Deck","bodypart":"shoulders"}
52+
{"_id":{"$oid":"5c0a15c255eed58e66e7505a"},"exercise":"Barbell Squats","bodypart":"shoulders"}
53+
{"_id":{"$oid":"5c0a15c255eed58e66e7505b"},"exercise":"Dumbbell Squats","bodypart":"shoulders"}
54+
{"_id":{"$oid":"5c0a15c255eed58e66e7505c"},"exercise":"Front Squats","bodypart":"shoulders"}
55+
{"_id":{"$oid":"5c0a15c255eed58e66e7505d"},"exercise":"Barbell Split Squats","bodypart":"legs"}
56+
{"_id":{"$oid":"5c0a15c255eed58e66e7505e"},"exercise":"Dumbbell Split Squats","bodypart":"legs"}
57+
{"_id":{"$oid":"5c0a15c255eed58e66e7505f"},"exercise":"Barbell Lunges","bodypart":"legs"}
58+
{"_id":{"$oid":"5c0a15c255eed58e66e75060"},"exercise":"Dumbbell Lunges","bodypart":"legs"}
59+
{"_id":{"$oid":"5c0a15c255eed58e66e75061"},"exercise":"Barbell Step Ups","bodypart":"legs"}
60+
{"_id":{"$oid":"5c0a15c255eed58e66e75062"},"exercise":"Dumbbell Step-Ups","bodypart":"legs"}
61+
{"_id":{"$oid":"5c0a15c255eed58e66e75063"},"exercise":"Leg Press","bodypart":"legs"}
62+
{"_id":{"$oid":"5c0a15c255eed58e66e75064"},"exercise":"Single Leg Press","bodypart":"legs"}
63+
{"_id":{"$oid":"5c0a15c255eed58e66e75065"},"exercise":"Machine Squat","bodypart":"legs"}
64+
{"_id":{"$oid":"5c0a15c255eed58e66e75066"},"exercise":"Hack Squat","bodypart":"legs"}
65+
{"_id":{"$oid":"5c0a15c255eed58e66e75067"},"exercise":"Leg Extensions","bodypart":"legs"}
66+
{"_id":{"$oid":"5c0a15c255eed58e66e75068"},"exercise":"Romanian Deadlifts","bodypart":"legs"}
67+
{"_id":{"$oid":"5c0a15c255eed58e66e75069"},"exercise":"Straight Leg Deadlifts","bodypart":"legs"}
68+
{"_id":{"$oid":"5c0a15c255eed58e66e7506a"},"exercise":"Sumo Deadlifts","bodypart":"legs"}
69+
{"_id":{"$oid":"5c0a15c255eed58e66e7506b"},"exercise":"Glute-Ham Raises","bodypart":"legs"}
70+
{"_id":{"$oid":"5c0a15c255eed58e66e7506c"},"exercise":"Hyperextensions","bodypart":"legs"}
71+
{"_id":{"$oid":"5c0a15c255eed58e66e7506d"},"exercise":"Cable Pull-Throughs","bodypart":"legs"}
72+
{"_id":{"$oid":"5c0a15c255eed58e66e7506e"},"exercise":"Good-Mornings","bodypart":"legs"}
73+
{"_id":{"$oid":"5c0a15c255eed58e66e7506f"},"exercise":"Leg Curls","bodypart":"legs"}
74+
{"_id":{"$oid":"5c0a15c255eed58e66e75070"},"exercise":"Standing Barbell Curls","bodypart":"biceps"}
75+
{"_id":{"$oid":"5c0a15c255eed58e66e75071"},"exercise":"Standing Dumbbell Curls","bodypart":"biceps"}
76+
{"_id":{"$oid":"5c0a15c255eed58e66e75072"},"exercise":"Barbell Preacher Curls","bodypart":"biceps"}
77+
{"_id":{"$oid":"5c0a15c255eed58e66e75073"},"exercise":"Dumbbell Preacher Curls","bodypart":"biceps"}
78+
{"_id":{"$oid":"5c0a15c255eed58e66e75074"},"exercise":"Seated Dumbbell Curls","bodypart":"biceps"}
79+
{"_id":{"$oid":"5c0a15c255eed58e66e75075"},"exercise":"Incline Dumbbell Curls","bodypart":"biceps"}
80+
{"_id":{"$oid":"5c0a15c255eed58e66e75076"},"exercise":"Hammer Curls","bodypart":"biceps"}
81+
{"_id":{"$oid":"5c0a15c255eed58e66e75077"},"exercise":"Concentration Curls","bodypart":"biceps"}
82+
{"_id":{"$oid":"5c0a15c255eed58e66e75078"},"exercise":"Cable Curls","bodypart":"biceps"}
83+
{"_id":{"$oid":"5c0a15c255eed58e66e75079"},"exercise":"Biceps Curl Machine","bodypart":"biceps"}
84+
{"_id":{"$oid":"5c0a15c255eed58e66e7507a"},"exercise":"Flat Close Grip Bench Press","bodypart":"triceps"}
85+
{"_id":{"$oid":"5c0a15c255eed58e66e7507b"},"exercise":"Decline Close Grip Bench Press","bodypart":"triceps"}
86+
{"_id":{"$oid":"5c0a15c255eed58e66e7507c"},"exercise":"Close Grip Push-Ups","bodypart":"triceps"}
87+
{"_id":{"$oid":"5c0a15c255eed58e66e7507d"},"exercise":"Laying Barbell Triceps Extensions","bodypart":"triceps"}
88+
{"_id":{"$oid":"5c0a15c255eed58e66e7507e"},"exercise":"Laying Dumbbell Triceps Extensions","bodypart":"triceps"}
89+
{"_id":{"$oid":"5c0a15c255eed58e66e7507f"},"exercise":"Skull Crushers","bodypart":"triceps"}
90+
{"_id":{"$oid":"5c0a15c255eed58e66e75080"},"exercise":"Overhead Barbell Triceps Extensions","bodypart":"triceps"}
91+
{"_id":{"$oid":"5c0a15c255eed58e66e75081"},"exercise":"Dumbbell Triceps Extensions","bodypart":"triceps"}
92+
{"_id":{"$oid":"5c0a15c255eed58e66e75082"},"exercise":"Cable Press-Downs","bodypart":"triceps"}
93+
{"_id":{"$oid":"5c0a15c255eed58e66e75083"},"exercise":"Bench Dips","bodypart":["triceps","chest"]}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)