Skip to content

Commit 6a2a3f7

Browse files
2 parents a4b553b + 322cdac commit 6a2a3f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+4207
-1608
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: node_js
2-
node_js:
3-
- "iojs"
4-
- "7"
2+
node_js: node
3+
services:
4+
- mongodb

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Fit-Journal with NodeJS
22

3+
View project at www.fit-journal.herokuapp.com (currently still in development)
4+
35
Track your workout history whether it be resistance or cardiovascular training, all in one place!
46
This project is built on Node and Express using CRUD standard practices.
57
Create an account.

app/auth/auth.router.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ const {
1414

1515
const authRouter = express.Router();
1616

17-
//Receives user we can turn into JSON web token to be issued out
18-
//to users to access protected endpoints
1917
function createJwtToken(user) {
2018
return jwt.sign({
2119
user
@@ -26,12 +24,15 @@ function createJwtToken(user) {
2624
});
2725
}
2826
//Login endpoint
29-
authRouter.get('/login.html', (req, res) => {});
27+
authRouter.get('/login', (req, res) => {
28+
const user = request.user.serialize();
29+
const jwtToken = createJwtToken(user);
30+
response.json({ jwtToken, user });
31+
});
3032

3133
authRouter.post('/login', localPassportMiddleware, (request, response) => {
3234
const user = request.user.serialize();
3335
const jwtToken = createJwtToken(user);
34-
// response.redirect('/api/workout');
3536
response.json({
3637
jwtToken,
3738
user
@@ -42,7 +43,6 @@ authRouter.post('/login', localPassportMiddleware, (request, response) => {
4243
authRouter.post('/refresh', jwtPassportMiddleware, (request, response) => {
4344
const user = request.user;
4445
const jwtToken = createJwtToken(user);
45-
// response.redirect('/api/workout');
4646
response.json({
4747
jwtToken,
4848
user
@@ -59,7 +59,7 @@ authRouter.post('/logout', localPassportMiddleware, (req, res) => {
5959

6060
=======
6161
authRouter.post('/logout', (req, res) => {
62-
console.log("logging out")
62+
6363
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0');
6464
res.json({
6565

app/auth/auth.strategy.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,37 @@ const {
1414
JWT_SECRET
1515
} = require('../config');
1616

17-
// Passport authentication strategies are used to define how authentication middleware works.
18-
// For more on how Passport authentication works using strategies, see:
19-
// http://www.passportjs.org/docs/configure/
17+
2018

2119
// The LocalStrategy gets used while trying to access an Endpoint using a User + Password combination
2220
const localStrategy = new LocalStrategy((username, password, passportVerify) => {
2321
let user;
24-
// Step 1: Verify the username exists
22+
2523
User.findOne({
2624
username: username
2725
}).then(_user => {
2826
user = _user;
2927
if (!user) {
3028

31-
// Step 2A: If user is not found on the database, reject promise with an error.
3229
return Promise.reject({
3330
reason: 'LoginError',
3431
message: 'Incorrect username or password'
3532
});
3633
}
37-
// Step 2B: Compare the user's password against the stored password hash by running it against the same algorithm.
3834

3935
return user.validatePassword(password);
4036
}).then(isValid => {
4137
if (!isValid) {
4238

43-
// Step 3A: If password doesn't match the stored password hash, reject promise with an error.
4439
return Promise.reject({
4540
reason: 'LoginError',
4641
message: 'Incorrect username or password'
4742
});
4843
}
49-
// Step 3B: If authentication is succesfull, execute the passportVerify callback correctly.
50-
// To learn more about how the passportVerify callback works, see:
51-
// http://www.passportjs.org/docs/configure/#verify-callback
44+
5245
return passportVerify(null, user);
5346
}).catch(err => {
54-
// Step 4: If an error ocurred at any stage during the process, execute the passportVerify callback correctly.
55-
// To learn more about how the passportVerify callback works, see:
56-
// http://www.passportjs.org/docs/configure/#verify-callback
47+
5748
if (err.reason === 'LoginError') {
5849

5950
return passportVerify(null, false, err.message);

app/config.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
module.exports = {
2-
PORT: process.env.PORT || 8080,
3-
HTTP_STATUS_CODES: {
4-
OK: 200,
5-
CREATED: 201,
6-
NO_CONTENT: 204,
7-
BAD_REQUEST: 400,
8-
UNAUTHORIZED: 401,
9-
NOT_FOUND: 404,
10-
INTERNAL_SERVER_ERROR: 500
11-
},
12-
MONGO_URL: process.env.MONGO_URL || 'mongodb://localhost:27017/fit-journal',
13-
TEST_MONGO_URL: process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test-fit-journal',
14-
JWT_SECRET: process.env.JWT_SECRET || 'default',
15-
JWT_EXPIRY: process.env.JWT_EXPIRY || '7d'
16-
};
2+
PORT: process.env.PORT || 8080,
3+
HTTP_STATUS_CODES: {
4+
OK: 200,
5+
CREATED: 201,
6+
NO_CONTENT: 204,
7+
BAD_REQUEST: 400,
8+
UNAUTHORIZED: 401,
9+
NOT_FOUND: 404,
10+
INTERNAL_SERVER_ERROR: 500,
11+
},
12+
MONGO_URL: process.env.MONGO_URL || 'mongodb://localhost:27017/fit-journal',
13+
// MONGO_URL: process.env.MONGO_URL || 'mongodb://ds237932.mlab.com:37932/fit-journal',
14+
TEST_MONGO_URL: process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test-fit-journal',
15+
JWT_SECRET: process.env.JWT_SECRET || 'default',
16+
JWT_EXPIRY: process.env.JWT_EXPIRY || '7d',
17+
};

app/exercise/exercise.model.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,5 @@ const ExerciseJoiSchema = Joi.object().keys({
4747
});
4848

4949
module.exports = {
50-
Exercise,
51-
ExerciseJoiSchema
50+
Exercise
5251
};

app/exercise/exercise.router.js

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

2020
const newExercise = {
21-
2221
exercise: request.body.exercise,
2322
ex_type: request.body.ex_type,
2423
bodypart: request.body.bodypart
25-
2624
};
2725

2826
const validation = Joi.validate(newExercise, ExerciseJoiSchema);

app/server.js

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ app.use('/api/auth', authRouter);
4444
app.use('/api/user', userRouter); //Redirects all calls to /api/user to userRouter
4545
app.use('/api/home', workoutRouter);
4646
app.use('/api/exercises', exerciseRouter);
47+
app.use(express.static('./public', {
48+
extensions: ['html', 'htm']
49+
// Other options here
50+
}));
4751

4852

4953
app.use('*', function(req, res) {
@@ -67,28 +71,22 @@ function startServer(testEnv) {
6771
} else {
6872
mongoUrl = MONGO_URL;
6973
}
70-
// Step 1: attempt to connect to MongoDB with mongoose
71-
mongoose.connect(mongoUrl, {
72-
useNewUrlParser: true
73-
}, err => {
74-
if (err) {
75-
// Step 2a: if there si an error starting mongo, log error, reject promise and stop code execution
76-
console.error(err);
77-
return reject(err);
78-
} else {
79-
// Step 2B: start express server
80-
server = app.listen(PORT, () => {
81-
//STep 3A: log success message to console and resolve promise
82-
console.log(`Express sever listining on http://localhost:${PORT}`);
83-
resolve();
84-
}).on('error', err => {
85-
// Step 3B: if there was a problem starting the express sever, disconnect from MongoDB immediately
86-
mongoose.disconnect();
74+
mongoose.connect(mongoUrl, { useNewUrlParser: true },
75+
err => {
76+
if (err) {
8777
console.error(err);
88-
reject(err);
89-
});
90-
}
91-
});
78+
return reject(err);
79+
} else {
80+
server = app.listen(PORT, () => {
81+
console.log(`Express sever listining on http://localhost:${PORT}`);
82+
resolve();
83+
}).on('error', err => {
84+
mongoose.disconnect();
85+
console.error(err);
86+
reject(err);
87+
});
88+
}
89+
});
9290
});
9391
}
9492

app/sets/sets.model.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const mongoose = require("mongoose");
2+
const Joi = require("joi");
3+
4+
5+
const setSchema = new mongoose.Schema({
6+
user: {
7+
type: mongoose.Schema.Types.ObjectId,
8+
ref: "user"
9+
},
10+
exercise: {
11+
type: String,
12+
required: true
13+
},
14+
set: {
15+
type: Number,
16+
required: true
17+
},
18+
reps: {
19+
type: Number,
20+
required: true
21+
},
22+
weight: {
23+
type: Number
24+
},
25+
date: {
26+
type: Date,
27+
default: Date.now
28+
}
29+
});
30+
setSchema.methods.serialize = function() {
31+
return {
32+
user: this.user,
33+
exercise: this.exercise,
34+
reps: this.reps,
35+
weight: this.weight,
36+
set: this.set,
37+
date: this.date
38+
}
39+
}
40+
const Set = mongoose.model("set", setSchema);
41+
const SetJoiSchema = Joi.object({
42+
user: Joi.string().required(),
43+
exercise: Joi.string(),
44+
reps: Joi.number(),
45+
set: Joi.number(),
46+
weight: Joi.number()
47+
})
48+
49+
module.exports = {
50+
Set,
51+
setSchema,
52+
SetJoiSchema
53+
}

app/sets/sets.router.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const express = require('express');
2+
const Joi = require('joi');
3+
const setsRouter = 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+
Set,
14+
SetJoiSchema
15+
} = require('./sets.model');
16+
17+
//Create new set
18+
setsRouter.post('/', (req, res) => {
19+
const newSet = {
20+
date: req.body.date,
21+
exercise: req.body.exercise,
22+
reps: req.body.reps,
23+
weight: req.body.weight,
24+
set: req.body.set
25+
26+
}
27+
const validation = Joi.validate(newSet, SetJoiSchema);
28+
if (validation.error) {
29+
console.log("Set Validation Error")
30+
return res.status(HTTP_STATUS_CODES.BAD_REQUEST).json({
31+
error: validation.error
32+
});
33+
}
34+
Set.create(newSet)
35+
.then(createdSet => {
36+
return res.status(HTTP_STATUS_CODES.CREATED).json(createdSet.serialize());
37+
})
38+
.catch(error => {
39+
return res.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).send(error);
40+
})
41+
})
42+
43+
setsRouter.get('/', (req, res) => {
44+
Set.find()
45+
.then(sets => {
46+
return res.status(HTTP_STATUS_CODES.OK).json(
47+
sets.map(set => set.serialize())
48+
);
49+
})
50+
.catch(error => {
51+
return res.status(HTTP_STATUS_CODES.INTERNAL_SERVER_ERROR).json(error);
52+
});
53+
});
54+
module.exports = {
55+
56+
setsRouter
57+
}

0 commit comments

Comments
 (0)