From 9c95c3b536d575e8c3a357fe197c61dd3aa9c5a7 Mon Sep 17 00:00:00 2001 From: Konrad Ryczko Date: Sun, 7 Mar 2021 22:27:47 +0100 Subject: [PATCH] [#28] remove answers after flashard remove (#38) --- server/src/models/Answer.ts | 4 ++-- server/src/routes/Answer.ts | 9 +++++---- server/src/routes/flashcard.ts | 3 +++ server/src/routes/index.ts | 3 +++ 4 files changed, 13 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 b824413..ef93c0d 100644 --- a/server/src/routes/flashcard.ts +++ b/server/src/routes/flashcard.ts @@ -1,5 +1,6 @@ import express, { Request, Response } from 'express'; import passport from 'passport'; +import { Answer } from '../models/Answer'; import { Flashcard, validateFlashcard, validateFlashcardUpdate } from '../models/Flashcard'; import { FlashcardCollection } from '../models/FlashcardCollection'; const router = express.Router(); @@ -48,6 +49,8 @@ router.delete('/:id', passport.authenticate('jwt', { session: false }), async (r await collection.update({ $pull: { flashcards: flashcard._id } }); await flashcard.deleteOne(); + 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) => {