diff --git a/backend/src/controllers/classController.ts b/backend/src/controllers/classController.ts index 45a9ffc..04c92e4 100644 --- a/backend/src/controllers/classController.ts +++ b/backend/src/controllers/classController.ts @@ -15,4 +15,28 @@ export default class ClassesController { }); } } -} \ No newline at end of file + + public async getClassesByDay(req: Request, res: Response) { + const day = req.params.day; + const classModel = new ClassesModel(); + if (!day) { + return res.status(400).json({ + error: "Missing required parameter: 'day'", + }); + } + const regex = /^\d{4}-\d{2}-\d{2}$/; // YYYY-MM-DD + if (!regex.test(day)) { + return res.status(400).json({ + error: "Invalid day format. Please use YYYY-MM-DD.", + }); + } + try { + const classes = await classModel.getClassesByDay(day); + res.status(200).json(classes); + } catch (error: any) { + return res.status(error.status).json({ + error: error.message, + }); + } + } +} diff --git a/backend/src/models/classModel.ts b/backend/src/models/classModel.ts index 690b9f1..b8dc69c 100644 --- a/backend/src/models/classModel.ts +++ b/backend/src/models/classModel.ts @@ -1,17 +1,33 @@ -import connection from '../config/database.js'; +import connection from "../config/database.js"; export default class ClassesModel { + public getClassesFromDB(): Promise { + return new Promise((resolve, reject) => { + const query = `SELECT * FROM class`; - public getClassesFromDB(): Promise { - return new Promise((resolve, reject) => { - const query = `SELECT * FROM neuron.class`; + connection.query(query, [], (error: any, results: any) => { + if (error) { + reject(error + "Error fetching classes"); + } + resolve(results); + }); + }); + } - connection.query(query, [], (error: any, results: any) => { - if (error) { - return reject(error + 'Error fetching classes'); - } - resolve(results); - }); + public getClassesByDay(day: string): Promise { + return new Promise((resolve, reject) => { + const query = `SELECT * FROM class WHERE CAST(start_date as date) = ?`; + + const values = [day]; + connection.query(query, values, (error: any, result: any) => { + if (error) { + reject({ + status: 500, + message: "Error retrieving classes", }); - } -} \ No newline at end of file + } + resolve(result); + }); + }); + } +} diff --git a/backend/src/routes/classRoutes.ts b/backend/src/routes/classRoutes.ts index 16c4186..d26193b 100644 --- a/backend/src/routes/classRoutes.ts +++ b/backend/src/routes/classRoutes.ts @@ -9,4 +9,8 @@ router.get('/', async (req: Request, res: Response) => { await classController.getAllClasses(req, res); }); +router.get("/schedule/:day", async (req, res) => { + await classController.getClassesByDay(req, res); +}); + export default router; \ No newline at end of file