Skip to content

Commit dc18203

Browse files
put back exercise
1 parent 1073f2d commit dc18203

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

app/exercise/exercise.model.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Exercises should have the following:
2+
// 1. Name
3+
// 2. Bodypart
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+
exercise: {
12+
type: String,
13+
required: true
14+
},
15+
bodypart: {
16+
type: String,
17+
required: true
18+
},
19+
ex_type: {
20+
type: String,
21+
enum: ["cardiovascular", "strength training"]
22+
}
23+
24+
});
25+
26+
exerciseSchema.methods.serialize = function() {
27+
28+
return {
29+
id: this._id,
30+
exercise: this.exercise,
31+
bodypart: this.bodypart,
32+
ex_type: this.ex_type
33+
};
34+
};
35+
36+
const Exercise = mongoose.model("exercise", exerciseSchema);
37+
38+
const ExerciseJoiSchema = Joi.object().keys({
39+
// user: Joi.string().optional(),
40+
exercise: Joi.string()
41+
.min(1)
42+
.required(),
43+
bodypart: Joi.string()
44+
.min(1)
45+
.required(),
46+
ex_type: Joi.string().min(1)
47+
});
48+
49+
module.exports = {
50+
Exercise
51+
};

app/exercise/exercise.router.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
exercise: request.body.exercise,
22+
ex_type: request.body.ex_type,
23+
bodypart: request.body.bodypart
24+
};
25+
26+
const validation = Joi.validate(newExercise, ExerciseJoiSchema);
27+
if (validation.error) {
28+
29+
return response.status(HTTP_STATUS_CODES.BAD_REQUEST).json({
30+
error: validation.error
31+
});
32+
}
33+
34+
Exercise.create(newExercise)
35+
.then(createdExercise => {
36+
return response.status(HTTP_STATUS_CODES.CREATED).json(createdExercise.serialize());
37+
})
38+
.catch(error => {
39+
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).json(error);
40+
})
41+
})
42+
43+
44+
// RETRIEVE Exercises
45+
exerciseRouter.get('/', (request, response) => {
46+
47+
Exercise.find()
48+
.then(exercises => {
49+
return response.status(HTTP_STATUS_CODES.OK).json(
50+
exercises.map(exercise => exercise.serialize())
51+
52+
);
53+
})
54+
.catch(error => {
55+
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).json(error);
56+
});
57+
});
58+
// RETRIEVE ONE EXERCISE
59+
exerciseRouter.get('/:id', (request, response) => {
60+
61+
Exercise.findById(request.params.id)
62+
.then(exercise => {
63+
64+
65+
return response.json(exercise);
66+
67+
})
68+
.catch(error => {
69+
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).json(error);
70+
});
71+
});
72+
73+
module.exports = {
74+
exerciseRouter
75+
};

0 commit comments

Comments
 (0)