From 2f2c4dd6f6f4feab3efd7dfd35786f7ec10be5ed Mon Sep 17 00:00:00 2001 From: Ryczko Date: Sat, 6 Mar 2021 13:51:47 +0100 Subject: [PATCH] [#28] remove answers after flashard remove --- server/src/models/Answer.ts | 4 ++-- server/src/routes/Answer.ts | 9 +++++---- server/src/routes/flashcard.ts | 4 ++++ server/src/routes/index.ts | 3 +++ 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/server/src/models/Answer.ts b/server/src/models/Answer.ts index bc2e85d..9251406 100644 --- a/server/src/models/Answer.ts +++ b/server/src/models/Answer.ts @@ -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 diff --git a/server/src/routes/Answer.ts b/server/src/routes/Answer.ts index 3a39a71..2925b60 100644 --- a/server/src/routes/Answer.ts +++ b/server/src/routes/Answer.ts @@ -5,7 +5,7 @@ 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 }); @@ -13,9 +13,10 @@ router.post('/', async (req: Request, res: Response) => { 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; diff --git a/server/src/routes/flashcard.ts b/server/src/routes/flashcard.ts index 754cbaf..bb8a0dd 100644 --- a/server/src/routes/flashcard.ts +++ b/server/src/routes/flashcard.ts @@ -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(); @@ -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(); diff --git a/server/src/routes/index.ts b/server/src/routes/index.ts index 6b9f6f2..04d797c 100644 --- a/server/src/routes/index.ts +++ b/server/src/routes/index.ts @@ -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(); @@ -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) => {