Skip to content

Commit

Permalink
[#28] remove answers after flashard remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryczko committed Mar 6, 2021
1 parent 2d96e60 commit 2f2c4dd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
4 changes: 2 additions & 2 deletions server/src/models/Answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import Joi from 'joi';
import mongoose from 'mongoose';

interface IAnswer extends mongoose.Document {
flashcard: mongoose.Schema.Types.ObjectId;
flashcardId: mongoose.Schema.Types.ObjectId;
date: Date;
isCorrect: boolean;
}

const answerSchema = new mongoose.Schema({
flashcard: {
flashcardId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Flashcard',
required: true
Expand Down
9 changes: 5 additions & 4 deletions server/src/routes/Answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ const router = express.Router();
router.post('/', async (req: Request, res: Response) => {
try {
const answer = new Answer({
flashcard: req.body.flashcardId,
flashcardId: req.body.flashcardId,
date: req.body.Date,
isCorrect: req.body.isCorrect
});
const { error } = validateAnswer(req.body);
if (error) {
return res.status(400).send(error.details[0].message);
} else await answer.save();
res.status(201).send('Session data successfully saved');
res.send(answer);
res.status(201).send(answer);
} catch (error) {
res.status(500).send('Something went wrong').end();
res.status(500).send(error).end();
}
});

export default router;
4 changes: 4 additions & 0 deletions server/src/routes/flashcard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import express, { Request, Response } from 'express';
import { Answer } from '../models/Answer';
import { Flashcard, validateFlashcard, validateFlashcardUpdate } from '../models/Flashcard';
import { FlashcardCollection } from '../models/FlashcardCollection';
const router = express.Router();
Expand Down Expand Up @@ -40,6 +41,9 @@ router.delete('/:id', async (req: Request, res: Response) => {
try {
const flashcard = await Flashcard.findByIdAndRemove(req.params.id);
if (!flashcard) return res.status(404).send('The flashcard with the given ID was not found.');

await Answer.deleteMany({ flashcardId: req.params.id });

res.status(204).end();
} catch (error) {
if (error.kind === 'ObjectId') res.status(400).send('Invalid ID.').end();
Expand Down
3 changes: 3 additions & 0 deletions server/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import passport from 'passport';
// TODO: to remove
import { SessionSettings } from '../models/SessionSettings';
import flashcard from './flashcard';
import answer from './Answer';
import flashcardCollection from './flashcardCollection';

const router = express.Router();
Expand All @@ -19,6 +20,8 @@ router.use('/api/google', google);

router.use('/api/flashcard', flashcard);

router.use('/api/answer', answer);

router.use('/api/flashcard-collection', flashcardCollection);

router.get('/api', async (req: Request, res: Response) => {
Expand Down

0 comments on commit 2f2c4dd

Please sign in to comment.