Skip to content

Commit a4b553b

Browse files
updated auth
2 parents a47b70b + 7867560 commit a4b553b

28 files changed

+4941
-648
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ These instructions will get you a copy of the project up and running on your loc
1212

1313
## Running the tests
1414

15-
Explain how to run the automated tests for this system
15+
Tests completed with faker.js and Chai
16+
To supply data for testing and assertions
1617

1718
### Break down into end to end tests
1819

app/auth/auth.router.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ function createJwtToken(user) {
2626
});
2727
}
2828
//Login endpoint
29-
authRouter.get('/login.html', (req, res) => {
29+
authRouter.get('/login.html', (req, res) => {});
3030

31-
32-
});
3331
authRouter.post('/login', localPassportMiddleware, (request, response) => {
3432
const user = request.user.serialize();
3533
const jwtToken = createJwtToken(user);
@@ -51,13 +49,23 @@ authRouter.post('/refresh', jwtPassportMiddleware, (request, response) => {
5149
});
5250
});
5351

52+
<<<<<<< HEAD
5453
authRouter.post('/logout', localPassportMiddleware, (req, res) => {
5554
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
5655
}
5756
module.exports = {
5857
authRouter
5958
});
6059

60+
=======
61+
authRouter.post('/logout', (req, res) => {
62+
console.log("logging out")
63+
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
64+
res.json({
65+
66+
})
67+
});
68+
>>>>>>> 7867560ecc9fefea91206abd0e5963e313dce6b3
6169
module.exports = {
6270
authRouter
6371
};

app/exercise/exercise.model.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ const mongoose = require("mongoose");
88
const Joi = require("joi");
99

1010
const exerciseSchema = new mongoose.Schema({
11-
// user: {
12-
// type: mongoose.Schema.Types.ObjectId,
13-
// ref: "user"
14-
// },
1511
exercise: {
1612
type: String,
1713
required: true
@@ -24,18 +20,13 @@ const exerciseSchema = new mongoose.Schema({
2420
type: String,
2521
enum: ["cardiovascular", "strength training"]
2622
}
23+
2724
});
2825

2926
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-
}
27+
3628
return {
3729
id: this._id,
38-
// user: user,
3930
exercise: this.exercise,
4031
bodypart: this.bodypart,
4132
ex_type: this.ex_type
@@ -56,5 +47,6 @@ const ExerciseJoiSchema = Joi.object().keys({
5647
});
5748

5849
module.exports = {
59-
Exercise
50+
Exercise,
51+
ExerciseJoiSchema
6052
};

app/exercise/exercise.router.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const {
1818
exerciseRouter.post('/', (request, response) => {
1919

2020
const newExercise = {
21-
// user: request.user.id,
21+
2222
exercise: request.body.exercise,
2323
ex_type: request.body.ex_type,
2424
bodypart: request.body.bodypart
@@ -48,10 +48,9 @@ exerciseRouter.get('/', (request, response) => {
4848
console.log("Retrieving All Exercises");
4949
Exercise.find()
5050
.then(exercises => {
51-
// Step 2A: Return the correct HTTP status code, and the users correctly formatted via serialization.
52-
5351
return response.status(HTTP_STATUS_CODES.OK).json(
5452
exercises.map(exercise => exercise.serialize())
53+
5554
);
5655
})
5756
.catch(error => {
@@ -60,14 +59,16 @@ exerciseRouter.get('/', (request, response) => {
6059
});
6160
});
6261
// RETRIEVE ONE EXERCISE
63-
exerciseRouter.get('/:exerciseid', (request, response) => {
62+
exerciseRouter.get('/:id', (request, response) => {
63+
6464
// Step 1: Attempt to retrieve a specific user using Mongoose.Model.findById()
6565
// https://mongoosejs.com/docs/api.html#model_Model.findById
66-
Exercise.findById(request.params.exerciserid)
66+
Exercise.findById(request.params.id)
6767
.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');
68+
console.log(exercise)
69+
// Step 2A: Return the correct HTTP status code, and the user correctly formatted via serialization.
70+
// return response.status(HTTP_STATUS_CODES.OK).json(user.serialize());
71+
return response.json(exercise);
7172

7273
})
7374
.catch(error => {

app/user/user.router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ userRouter.get('/', (request, response) => {
7878
})
7979
.catch(error => {
8080
// Step 2B: If an error ocurred, return an error HTTP status code and the error in JSON format.
81-
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).json(error);
81+
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).send(error);
8282
});
8383
});
8484
// RETRIEVE ONE USER

app/workout/workout.model.js

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,65 @@
88
// https://en.wikipedia.org/wiki/List_of_weight_training_exercises
99
const mongoose = require("mongoose");
1010
const Joi = require("joi");
11+
const dateFns = require('date-fns');
1112

1213
const workoutSchema = new mongoose.Schema({
13-
user: { type: mongoose.Schema.Types.ObjectId, ref: "user" },
14-
exercise: { type: String, required: true },
15-
set: { type: Number, required: true },
16-
reps: { type: Number, required: true },
17-
weight: { type: Number },
18-
date: { type: Date }
14+
user: {
15+
type: mongoose.Schema.Types.ObjectId,
16+
ref: "user"
17+
},
18+
date: {
19+
type: Date,
20+
default: Date.now
21+
},
22+
sets: [{
23+
exercise: {
24+
type: String,
25+
required: true
26+
},
27+
set: {
28+
type: Number,
29+
required: true
30+
},
31+
reps: {
32+
type: Number,
33+
required: true
34+
},
35+
weight: {
36+
type: Number
37+
}
38+
}]
1939
});
2040

2141
workoutSchema.methods.serialize = function() {
22-
let user;
23-
if (typeof this.user.serialize === "function") {
24-
user = this.user.serialize();
25-
} else {
26-
user = this.user;
27-
}
28-
return {
29-
id: this._id,
30-
user: user,
31-
exercise: this.exercise,
32-
reps: this.reps,
33-
weight: this.weight,
34-
date: this.date
35-
};
42+
let user;
43+
if (typeof this.user.serialize === "function") {
44+
user = this.user.serialize();
45+
} else {
46+
user = this.user;
47+
}
48+
return {
49+
id: this._id,
50+
user: user,
51+
exercise: this.exercise,
52+
reps: this.reps,
53+
weight: this.weight,
54+
date: this.date
55+
};
3656
};
3757

3858
const Workout = mongoose.model("workout", workoutSchema);
3959

4060
const WorkoutJoiSchema = Joi.object().keys({
41-
user: Joi.string().optional(),
42-
exercise: Joi.string()
43-
.min(1)
44-
.required(),
45-
reps: Joi.number()
46-
.min(1)
47-
.required(),
48-
weight: Joi.number().min(1),
49-
date: Joi.date()
61+
user: Joi.string().optional(),
62+
exercise: Joi.string().min(1).required(),
63+
reps: Joi.number(),
64+
set: Joi.number(),
65+
weight: Joi.number(),
66+
date: Joi.date().min('1-1-1980')
5067
});
5168

52-
module.exports = { Workout };
69+
module.exports = {
70+
Workout,
71+
WorkoutJoiSchema
72+
};

app/workout/workout.router.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ const {
1818
workoutRouter.post('/', (request, response) => {
1919

2020
const newWorkout = {
21-
// user: request.user.id,
21+
user: request.body.user,
2222
exercise: request.body.exercise,
2323
reps: request.body.reps,
2424
weight: request.body.weight,
25-
set: request.body.set
25+
set: request.body.set,
26+
date: request.body.date
27+
2628
};
29+
console.log(newWorkout)
2730

2831
const validation = Joi.validate(newWorkout, WorkoutJoiSchema);
2932
if (validation.error) {
@@ -35,17 +38,31 @@ workoutRouter.post('/', (request, response) => {
3538

3639
Workout.create(newWorkout)
3740
.then(createdWorkout => {
41+
console.log("Workout Created")
3842
return response.status(HTTP_STATUS_CODES.CREATED).json(createdWorkout.serialize());
3943
})
4044
.catch(error => {
41-
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).json(error);
45+
console.log(error)
46+
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).send(error);
4247
})
4348
})
4449
// jwtPassportMiddleware,
45-
workoutRouter.get('/', jwtPassportMiddleware, (request, response) => {
50+
workoutRouter.get('/', (request, response) => {
4651
console.log("Your Workouts")
47-
response.sendFile(path.resolve('./app/views/auth/home.html'));
48-
// response.send('Text here')
52+
Workout.find()
53+
.then(workouts => {
54+
// Step 2A: Return the correct HTTP status code, and the users correctly formatted via serialization.
55+
56+
return response.status(HTTP_STATUS_CODES.OK).json(
57+
workouts.map(workout => workout.serialize())
58+
);
59+
60+
61+
})
62+
.catch(error => {
63+
// Step 2B: If an error ocurred, return an error HTTP status code and the error in JSON format.
64+
return response.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).json(error);
65+
});
4966
})
5067

5168
module.exports = {

0 commit comments

Comments
 (0)