diff --git a/controllers/historyController.js b/controllers/historyController.js index 928e614..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,8 +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}); } }; -export { userHistory, quizHistory }; +const deleteHistory = async (req, res) => { + const {user: {id: user_id}} = req.user; + try { + const quiz = await prisma.quizzes.delete({ + where: {id: req.params.quizId, user_id} + }); + if (!quiz) { + return res.status(404).json({error: 'Quiz not found'}); + } + res.json(quiz); + } catch (error) { + res.status(500).json({message: 'You are trying to access other users data', 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 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: