Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get classes by day api #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion backend/src/controllers/classController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,28 @@ export default class ClassesController {
});
}
}
}

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,
});
}
}
}
40 changes: 28 additions & 12 deletions backend/src/models/classModel.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import connection from '../config/database.js';
import connection from "../config/database.js";

export default class ClassesModel {
public getClassesFromDB(): Promise<any> {
return new Promise((resolve, reject) => {
const query = `SELECT * FROM class`;

public getClassesFromDB(): Promise<any> {
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<any> {
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",
});
}
}
}
resolve(result);
});
});
}
}
4 changes: 4 additions & 0 deletions backend/src/routes/classRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;