-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexerciseService.js
41 lines (35 loc) · 1.34 KB
/
exerciseService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
// Define a function to handle fetching exercises
const getExercisesHandler = async (req, res) => {
try {
// Query the database to retrieve all exercises
const exercises = await prisma.exercises.findMany();
res.json(exercises); // Send exercises as a JSON response
} catch (error) {
console.error('Error fetching exercises:', error);
res.status(500).json({ error: 'Failed to fetch exercises' });
}
};
// Define a function to handle fetching a single exercise by ID
const getExerciseByIdHandler = async (req, res) => {
try {
const exerciseId = parseInt(req.params.id); // Get the exercise ID from the URL parameter
// Query the database to find the exercise by ID
const exercise = await prisma.exercises.findUnique({
where: { "exerciseID": exerciseId },
});
if (exercise) {
res.json(exercise); // Send the exercise as a JSON response
} else {
res.status(404).json({ error: 'Exercise not found' });
}
} catch (error) {
console.error('Error fetching exercise:', error);
res.status(500).json({ error: 'Failed to fetch exercise' });
}
};
module.exports = {
getExercisesHandler,
getExerciseByIdHandler
}