Skip to content

Commit

Permalink
refactor: ITP-228
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyRAV committed Nov 8, 2023
1 parent 6a2cd6e commit 3810046
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 9 additions & 3 deletions controllers/historyController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {prisma} from "../config/prismaClient.js";
import {formatDate} from "../services/formatDate.js";

export const userHistory = async (req, res) => {
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) || 20; // default limit to 10 if not specified
const limit = parseInt(req.query.limit, 10) || 10; // default limit to 10 if not specified

try {
const quizzes = await prisma.quizzes.findMany({
Expand All @@ -16,14 +17,19 @@ export const userHistory = async (req, res) => {
select: {
id: true,
quiz_title: true,
total_time_taken: true
created_at: true
}
});

const formattedQuizzes = quizzes.map(quiz => ({
...quiz,
created_at: formatDate(quiz.created_at)
}));

// Also return the total count of records for the frontend to calculate total pages
const totalCount = await prisma.quizzes.count({ where: { user_id } });

res.json({ quizzes, totalCount });
res.json({ quizzes: formattedQuizzes, totalCount });
} catch (error) {
res.status(500).json({ error: error.message });
}
Expand Down
7 changes: 7 additions & 0 deletions services/formatDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function formatDate(dateString) {
const date = new Date(dateString);
const day = date.getDate().toString().padStart(2, '0');
const month = (date.getMonth() + 1).toString().padStart(2, '0'); // +1 cuz months are 0-indexed
const year = date.getFullYear();
return `${day}.${month}.${year}`;
}

0 comments on commit 3810046

Please sign in to comment.