Skip to content

Commit

Permalink
Merge pull request ARC-Solutions#6 from ARC-Solutions/development/del…
Browse files Browse the repository at this point in the history
…eteHistory

Development/delete history
  • Loading branch information
RickyRAV authored Dec 15, 2023
2 parents 8234ae0 + 596c126 commit e21e308
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
39 changes: 29 additions & 10 deletions controllers/historyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ 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;
const limit = parseInt(req.query.limit, 10) || 10; // default limit to 10 if not specified

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,
Expand All @@ -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: {
Expand All @@ -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 = {
Expand All @@ -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};
4 changes: 3 additions & 1 deletion routes/historyRoutes.js
Original file line number Diff line number Diff line change
@@ -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);
};
42 changes: 42 additions & 0 deletions services/swaggerDefinitions.YAML
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit e21e308

Please sign in to comment.