Skip to content

Commit

Permalink
Merge pull request #6 from codeMonkeyMasters/darian-One-join
Browse files Browse the repository at this point in the history
Scaled down to one join table, to make things easier and more logical.
  • Loading branch information
DarianRushworth committed Aug 26, 2020
2 parents 449805b + d6ffff9 commit e44939c
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 127 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@ module.exports = {
},
exerciseId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: "exercises",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
quizQuestionId: {
type: Sequelize.INTEGER,
references: {
model: "exercises",
model: "quizQuestions",
key: "id",
},
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
timeTaken: {
type: Sequelize.TIME,
allowNull: false,
},
exp: {
type: Sequelize.INTEGER,
Expand Down
48 changes: 0 additions & 48 deletions migrations/4-create-completed-quiz.js

This file was deleted.

11 changes: 9 additions & 2 deletions models/completedexercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,24 @@ module.exports = (sequelize, DataTypes) => {
},
exerciseId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: "exercises",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
quizQuestionId: {
type: DataTypes.INTEGER,
references: {
model: "quizQuestions",
key: "id",
},
onUpdate: "CASCADE",
onDelete: "CASCADE",
},
timeTaken: {
type: DataTypes.TIME,
allowNull: false,
},
exp: {
type: DataTypes.INTEGER,
Expand Down
46 changes: 0 additions & 46 deletions models/completedquiz.js

This file was deleted.

3 changes: 2 additions & 1 deletion models/quizquestion.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ module.exports = (sequelize, DataTypes) => {
*/
static associate(models) {
quizQuestion.belongsToMany(models.user, {
through: "completedQuizzes",
through: "completedExercises",
key: "quizQuestionId",
})
quizQuestion.hasOne(models.exercise)
}
};
quizQuestion.init({
Expand Down
4 changes: 2 additions & 2 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module.exports = (sequelize, DataTypes) => {
key: "userId",
})
user.belongsToMany(models.quizQuestion, {
through: "completedQuizzes",
key: "userId",
through: "completedExercises",
key: "quizQuestionId",
})
}
}
Expand Down
16 changes: 12 additions & 4 deletions routers/exercisesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ router.patch(
}

try{
const quizComplete = await CompletedQuizzes.create({
const quizComplete = await CompletedExercises.create({
userId: userIdNeeded,
quizQuestionId: quizIdNeeded,
exp: 10,
Expand Down Expand Up @@ -106,7 +106,7 @@ router.patch(
)

router.patch(
"/:id/completed",
"/:id/completed/:qId",
authMiddleware,
async(req, res, next) => {
const userIdNeeded = req.user.id
Expand All @@ -121,6 +121,12 @@ router.patch(
res.status(400).send("The Url malfunctioned please refresh and try again.")
}

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

const time = req.body.timeTaken
const expInt = parseInt(req.body.exp)
// console.log(`time test: ${time}
Expand All @@ -134,16 +140,17 @@ router.patch(
where: {
userId: userIdNeeded,
exerciseId: exerciseIdNeeded,
quizQuestionId: quizIdNeeded,
}
})
?
await CompletedExercises.update({
timeTaken: time,
exp: expInt,
},{
where: {
userId: userIdNeeded,
exerciseId: exerciseIdNeeded
exerciseId: exerciseIdNeeded,
quizQuestionId: quizIdNeeded,
}
})
:
Expand All @@ -161,6 +168,7 @@ router.patch(
where: {
userId: userIdNeeded,
exerciseId: exerciseIdNeeded,
quizQuestionId: quizIdNeeded,
exp: expInt,
timeTaken: time,
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
{
userId: 1,
exerciseId: 3,
quizQuestionId: 23,
timeTaken: "00:05:00",
exp: 40,
createdAt: new Date(),
Expand All @@ -15,6 +16,7 @@ module.exports = {
{
userId: 1,
exerciseId: 2,
quizQuestionId: 22,
timeTaken: "00:15:00",
exp: 20,
createdAt: new Date(),
Expand All @@ -23,6 +25,7 @@ module.exports = {
{
userId: 2,
exerciseId: 1,
quizQuestionId: 21,
timeTaken: "00:02:35",
exp: 50,
createdAt: new Date(),
Expand Down
20 changes: 0 additions & 20 deletions seeders/4-some-completedQuiz.js

This file was deleted.

0 comments on commit e44939c

Please sign in to comment.