From 358eaa0054365095f8d7ddebde46543258be9315 Mon Sep 17 00:00:00 2001 From: RickyRaveanu Date: Fri, 15 Dec 2023 18:53:17 +0100 Subject: [PATCH 1/2] basic --- controllers/historyController.js | 17 ++++++++++++++++- routes/historyRoutes.js | 4 +++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/controllers/historyController.js b/controllers/historyController.js index 928e614..e90b615 100644 --- a/controllers/historyController.js +++ b/controllers/historyController.js @@ -73,4 +73,19 @@ const quizHistory = async (req, res) => { } }; -export { userHistory, quizHistory }; +const deleteHistory = async (req,res) => { + try { + const quiz = await prisma.quizzes.delete({ + where: {id:req.params.quizId} + }); + console.log('quiz deleted') + if(!quiz){ + return res.status(404).json({ error: 'Quiz not found'}); + } + res.json(quiz); + } catch (error) { + res.status(500).json({error: error.message}) + } +}; + +export { userHistory, quizHistory, deleteHistory }; diff --git a/routes/historyRoutes.js b/routes/historyRoutes.js index 72a8416..4c2568a 100644 --- a/routes/historyRoutes.js +++ b/routes/historyRoutes.js @@ -1,10 +1,12 @@ import {isAuthenticated} from "../middlewares/isAuthenticated.js"; import { quizHistory, - userHistory + userHistory, + deleteHistory } from '../controllers/historyController.js'; export default (app) => { app.get('/quizzes', isAuthenticated, userHistory); app.get('/quizzes/:quizId', isAuthenticated, quizHistory); + app.delete('/quizzes/:quizId', isAuthenticated, deleteHistory); }; \ No newline at end of file From 596c1266156a78d74379778a9b6b7b07d12e5ca1 Mon Sep 17 00:00:00 2001 From: RickyRAV Date: Fri, 15 Dec 2023 19:29:42 +0100 Subject: [PATCH 2/2] feat: itp-276 --- controllers/historyController.js | 36 +++++++++++++++------------ services/swaggerDefinitions.YAML | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 16 deletions(-) diff --git a/controllers/historyController.js b/controllers/historyController.js index e90b615..5f0b751 100644 --- a/controllers/historyController.js +++ b/controllers/historyController.js @@ -2,7 +2,7 @@ import {prisma} from "../config/prismaClient.js"; import {formatDate} from "../services/formatDate.js"; const userHistory = async (req, res) => { - const { user: { id: user_id } } = req.user; + const {user: {id: user_id}} = req.user; // Retrieve offset and limit from the query parameters, and provide default values if they are not provided const offset = parseInt(req.query.offset, 10) || 0; @@ -10,10 +10,10 @@ const userHistory = async (req, res) => { try { const quizzes = await prisma.quizzes.findMany({ - where: { user_id }, + where: {user_id}, skip: offset, // skip a certain number of records take: limit, // take a certain number of records - orderBy: { created_at: 'desc' }, + orderBy: {created_at: 'desc'}, select: { id: true, quiz_title: true, @@ -27,19 +27,20 @@ const userHistory = async (req, res) => { })); // Also return the total count of records for the frontend to calculate total pages - const totalCount = await prisma.quizzes.count({ where: { user_id } }); + const totalCount = await prisma.quizzes.count({where: {user_id}}); - res.json({ quizzes: formattedQuizzes, totalCount }); + res.json({quizzes: formattedQuizzes, totalCount}); } catch (error) { - res.status(500).json({ error: error.message }); + res.status(500).json({error: error.message}); } }; const quizHistory = async (req, res) => { // console.log('req.params.id:', req.params.quizId); + const {user: {id: user_id}} = req.user; try { const quiz = await prisma.quizzes.findUnique({ - where: { id: req.params.quizId }, + where: {id: req.params.quizId, user_id}, include: { questions: { include: { @@ -49,7 +50,10 @@ const quizHistory = async (req, res) => { } }); if (!quiz) { - return res.status(404).json({ error: 'Quiz not found' }); + return res.status(404).json({ + message: 'You are trying to access other users data', + error: 'Quiz not found' + }); } const formattedQuiz = { @@ -69,23 +73,23 @@ const quizHistory = async (req, res) => { }; res.json(formattedQuiz); } catch (error) { - res.status(500).json({ error: error.message }); + res.status(500).json({error: error.message}); } }; -const deleteHistory = async (req,res) => { +const deleteHistory = async (req, res) => { + const {user: {id: user_id}} = req.user; try { const quiz = await prisma.quizzes.delete({ - where: {id:req.params.quizId} + where: {id: req.params.quizId, user_id} }); - console.log('quiz deleted') - if(!quiz){ - return res.status(404).json({ error: 'Quiz not found'}); + if (!quiz) { + return res.status(404).json({error: 'Quiz not found'}); } res.json(quiz); } catch (error) { - res.status(500).json({error: error.message}) + res.status(500).json({message: 'You are trying to access other users data', error: error.message}) } }; -export { userHistory, quizHistory, deleteHistory }; +export {userHistory, quizHistory, deleteHistory}; diff --git a/services/swaggerDefinitions.YAML b/services/swaggerDefinitions.YAML index 72f68ef..6af8fda 100644 --- a/services/swaggerDefinitions.YAML +++ b/services/swaggerDefinitions.YAML @@ -585,6 +585,48 @@ paths: properties: error: type: string + delete: + tags: [ History ] + summary: Delete Quiz by ID + security: + - BearerAuth: [ ] + parameters: + - name: quiz_id + in: path + required: true + schema: + type: string + responses: + '200': + description: Quiz retrieved successfully + content: + application/json: + schema: + type: object + properties: + id: + type: string + user_id: + type: string + quiz_title: + type: string + created_at: + type: string + total_time_taken: + type: integer + correct_answers_count: + type: integer + '500': + description: Entry doesn't exist + content: + application/json: + schema: + type: object + properties: + message: + type: string + error: + type: string components: schemas: