Skip to content

Commit

Permalink
Merge pull request #4 from codeMonkeyMasters/darian-QuizSetup
Browse files Browse the repository at this point in the history
Darian quiz setup
  • Loading branch information
DarianRushworth committed Aug 25, 2020
2 parents 7cb57d2 + 28708d0 commit 4e2655b
Show file tree
Hide file tree
Showing 11 changed files with 645 additions and 1 deletion.
4 changes: 4 additions & 0 deletions migrations/0-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ module.exports = {
type: Sequelize.STRING,
allowNull: false,
},
totalExp: {
type: Sequelize.INTEGER,
allowNull: false,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
Expand Down
54 changes: 54 additions & 0 deletions migrations/3-create-quiz-question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('quizQuestions', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
question: {
type: Sequelize.TEXT,
allowNull: false,
},
answer: {
type: Sequelize.TEXT,
allowNull: false,
},
incorrect1: {
type: Sequelize.TEXT,
allowNull: false,
},
incorrect2: {
type: Sequelize.TEXT,
allowNull: false,
},
incorrect3: {
type: Sequelize.TEXT,
allowNull: false,
},
exerciseId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: "exercises",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "SET NULL",
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('quizQuestions');
}
};
48 changes: 48 additions & 0 deletions migrations/4-create-completed-quiz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('completedQuizzes', {
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",
},
quizQuestionId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: "quizQuestions",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
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('completedQuizzes');
}
};
46 changes: 46 additions & 0 deletions models/completedquiz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class completedQuiz 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
}
};
completedQuiz.init({
userId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: "users",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
quizQuestionId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: "quizQuestions",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
exp: {
type: DataTypes.INTEGER,
allowNull: false,
}
}, {
sequelize,
modelName: 'completedQuiz',
});
return completedQuiz;
};
1 change: 1 addition & 0 deletions models/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = (sequelize, DataTypes) => {
* The `models/index` file will call this method automatically.
*/
static associate(models) {
exercise.hasMany(models.quizQuestion)
exercise.belongsToMany(models.user, {
through: "completedExercises",
key: "exerciseId",
Expand Down
55 changes: 55 additions & 0 deletions models/quizquestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class quizQuestion 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) {
quizQuestion.belongsToMany(models.user, {
through: "completedQuizzes",
key: "quizQuestionId",
})
}
};
quizQuestion.init({
question: {
type: DataTypes.TEXT,
allowNull: false,
},
answer: {
type: DataTypes.TEXT,
allowNull: false,
},
incorrect1: {
type: DataTypes.TEXT,
allowNull: false,
},
incorrect2: {
type: DataTypes.TEXT,
allowNull: false,
},
incorrect3: {
type: DataTypes.TEXT,
allowNull: false,
},
exerciseId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: "exercises",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "SET NULL",
}
}, {
sequelize,
modelName: 'quizQuestion',
});
return quizQuestion;
};
8 changes: 8 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ module.exports = (sequelize, DataTypes) => {
through: "completedExercises",
key: "userId",
})
user.belongsToMany(models.quizQuestion, {
through: "completedQuizzes",
key: "userId",
})
}
}
user.init(
Expand All @@ -37,6 +41,10 @@ module.exports = (sequelize, DataTypes) => {
ranking: {
type: DataTypes.STRING,
allowNull: false,
},
totalExp: {
type: DataTypes.INTEGER,
allowNull: false,
}
},
{
Expand Down
104 changes: 103 additions & 1 deletion routers/exercisesRouter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const { Router } = require("express")
const authMiddleware = require("../auth/middleware")

const User = require("../models").user
const Exercises = require("../models").exercise
const CompletedExercises = require("../models").completedExercise
const QuizQuestions = require("../models").quizQuestion
const CompletedQuizzes = require("../models").completedQuiz

const router = new Router()

Expand Down Expand Up @@ -51,6 +54,57 @@ router.get(
}
)

router.patch(
"/:id/quiz/completed",
authMiddleware,
async(req, res, next) => {
const userIdNeeded = req.user.id
// console.log("user ID test", userIdNeeded)
if(!userIdNeeded){
res.status(401).send("Sorry you aren't authorized, please login/sign-up to authorize yourself.")
}

const quizIdNeeded = parseInt(req.params.id)
// console.log("quiz ID test", quizIdNeeded)
if(!quizIdNeeded){
res.status(400).send("The Url malfunctioned please refresh and try again.")
}

try{
const quizComplete = await CompletedQuizzes.create({
userId: userIdNeeded,
quizQuestionId: quizIdNeeded,
exp: 10,
})
console.log("created quiz test", quizComplete)
if(!quizComplete){
res.status(404).send("Your info couldnt be processed, refresh and try again.")
}

const userFound = await User.findByPk(userIdNeeded)
if(!userFound){
res.status(404).send("Oops, we seem to be lost please login/sign-up so we can find you.")
}

const updateExpUser = await userFound.increment("totalExp", { by: 10})
// console.log("exp update test", updateExpUser)
if(!updateExpUser){
res.status(400).send("Your exp wasnt updated please refresh and try again.")
} else {
delete updateExpUser.dataValues["password"]
res.status(202).send({
user: {...updateExpUser.dataValues},
completedQuiz: quizComplete,
})
}


} catch(error){
next(error)
}
}
)

router.patch(
"/:id/completed",
authMiddleware,
Expand Down Expand Up @@ -113,8 +167,56 @@ router.patch(
})
if(!sendExercise){
res.status(404).send("We couldn't find you're completed exercises, please refresh and try again.")
}

const userFound = await User.findByPk(userIdNeeded)
if(!userFound){
res.status(404).send("Oops, we seem to be lost please login/sign-up so we can find you.")
}

const updateUser = await userFound.increment("totalExp", { by: expInt})
if(!updateUser){
res.status(400).send("Oops, it seems yuor exp hasnt updated please refresh and try again.")
} else {
delete updateUser.dataValues["password"]
res.status(202).send({
user: {...updateUser.dataValues},
completed: sendExercise,
})
}

} catch(error){
next(error)
}
}
)

router.get(
"/:id/quiz",
authMiddleware,
async(req, res, next) => {

if(!req.user){
res.status(401).send("Sorry you aren't authorized, please login/sign-up to authorize yourself.")
}

const exerciseIdNeeded = parseInt(req.params.id)
// console.log("questions ID test", exerciseIdNeeded)
if(!exerciseIdNeeded){
res.status(400).send("Url malfunctioned, please refresh and try again.")
}

try{
const fecthQuizzes = await QuizQuestions.findAll({
where: {
exerciseId: exerciseIdNeeded,
}
})
// console.log("fetced quiz questions", fecthQuizzes)
if(!fecthQuizzes){
res.status(404).send("It seems our quizzes have vanished, give us a moment will we find them. Refresh to help us.")
} else {
res.status(202).send(sendExercise)
res.status(202).send(fecthQuizzes)
}

} catch(error){
Expand Down
2 changes: 2 additions & 0 deletions seeders/0-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
email: "test@test.com",
password: bcrypt.hashSync("test1234", SALT_ROUNDS),
ranking: "Code Monkey",
totalExp: 60,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand All @@ -22,6 +23,7 @@ module.exports = {
email: "a@a.com",
password: bcrypt.hashSync("a", SALT_ROUNDS),
ranking: "Code Monkey",
totalExp: 50,
createdAt: new Date(),
updatedAt: new Date(),
},
Expand Down
Loading

0 comments on commit 4e2655b

Please sign in to comment.