Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
/node_modules
.env
6 changes: 4 additions & 2 deletions config/config.json → config/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
require("dotenv").config()

module.exports = {
"development": {
"url": "YOUR_ELEPHANTSQL_URL_HERE",
"url": process.env.DATABASE_URL_DEV,
"dialect": "postgres",
"operatorsAliases": "0"
},
Expand Down
2 changes: 1 addition & 1 deletion config/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
SALT_ROUNDS: 10,
PORT: process.env.PORT || 4000
PORT: process.env.PORT || 4000,
};
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
require("dotenv").config()

const express = require("express");
const loggerMiddleWare = require("morgan");
const corsMiddleWare = require("cors");
const { PORT } = require("./config/constants");
const authRouter = require("./routers/auth");
const authMiddleWare = require("./auth/middleware");
const exercises = require("./routers/exercisesRouter")
const profile = require("./routers/ProfileRouter")

const app = express();

Expand Down Expand Up @@ -113,11 +117,8 @@ if (process.env.DELAY) {
*
*/

/**
* Routes
*
* Define your routes here (now that middlewares are configured)
*/
app.use("/exercises", exercises)
app.use("/profile", profile)

// GET endpoint for testing purposes, can be removed
app.get("/", (req, res) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ module.exports = {
primaryKey: true,
type: Sequelize.INTEGER,
},
name: {
fullName: {
type: Sequelize.STRING,
allowNull: false,
},
image : {
type: Sequelize.STRING,
allowNull: false,
},
Expand All @@ -21,6 +25,10 @@ module.exports = {
type: Sequelize.STRING,
allowNull: false,
},
ranking: {
type: Sequelize.STRING,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
Expand Down
32 changes: 32 additions & 0 deletions migrations/1-create-exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('exercises', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
content: {
type: Sequelize.TEXT,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('exercises');
}
};
52 changes: 52 additions & 0 deletions migrations/2-create-completed-exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('completedExercises', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: "users",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
exerciseId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: "exercises",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
timeTaken: {
type: Sequelize.TIME,
allowNull: false,
},
exp: {
type: Sequelize.INTEGER,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('completedExercises');
}
};
50 changes: 50 additions & 0 deletions models/completedexercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class completedExercise extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};
completedExercise.init({
userId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: "users",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
exerciseId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: "exercises",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
timeTaken: {
type: DataTypes.TIME,
allowNull: false,
},
exp: {
type: DataTypes.INTEGER,
allowNull: false,
}
}, {
sequelize,
modelName: 'completedExercise',
});
return completedExercise;
};
33 changes: 33 additions & 0 deletions models/exercise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class exercise extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
exercise.belongsToMany(models.user, {
through: "completedExercises",
key: "exerciseId",
})
}
};
exercise.init({
name: {
type: DataTypes.STRING,
allowNull: false,
},
content: {
type: DataTypes.TEXT,
allowNull: false,
}
}, {
sequelize,
modelName: 'exercise',
});
return exercise;
};
2 changes: 1 addition & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require("path");
const Sequelize = require("sequelize");
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/../config/config.json")[env];
const config = require(__dirname + "/../config/config.js")[env];
const db = {};

let sequelize;
Expand Down
15 changes: 13 additions & 2 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ module.exports = (sequelize, DataTypes) => {
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
user.belongsToMany(models.exercise, {
through: "completedExercises",
key: "userId",
})
}
}
user.init(
{
name: {
fullName: {
type: DataTypes.STRING,
allowNull: false,
},
image: {
type: DataTypes.STRING,
allowNull: false,
},
Expand All @@ -27,6 +34,10 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.STRING,
allowNull: false,
},
ranking: {
type: DataTypes.STRING,
allowNull: false,
}
},
{
sequelize,
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"dependencies": {
"bcrypt": "^5.0.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"morgan": "^1.9.1",
Expand Down
Loading