Compare commits

..

2 Commits

2 changed files with 41 additions and 35 deletions

View File

@ -1,41 +1,9 @@
import { Application, Router } from "@oak/oak"; import { Application } from "@oak/oak";
import pool from "../shared/db.ts"; import router from "./routes.ts";
const app = new Application(); const app = new Application();
const router = new Router();
// Define the endpoint Sufail will call from the Vite frontend
router.get("/api/employees", async (ctx) => {
try {
const [rows] = await pool.query(
`SELECT
e.employee_code,
p.first_name,
p.last_name,
d.name AS department,
j.title AS designation,
c.work_email
FROM employees e
JOIN partners p ON e.partner_id = p.partner_id
JOIN contracts c ON e.employee_id = c.employee_id
JOIN departments d ON c.department_id = d.department_id
JOIN job_positions j ON c.job_id = j.job_id`
);
ctx.response.body = {
success: true,
count: (rows as any[]).length,
data: rows
};
} catch (error) {
ctx.response.status = 500;
ctx.response.body = {
success: false,
error: (error as Error).message
};
}
});
// Register the routes and allowed methods from routes.ts
app.use(router.routes()); app.use(router.routes());
app.use(router.allowedMethods()); app.use(router.allowedMethods());

View File

@ -0,0 +1,38 @@
import { Router } from "@oak/oak";
import pool from "../shared/db.ts";
const router = new Router();
// GET all employees with their relational data
router.get("/api/employees", async (ctx) => {
try {
const [rows] = await pool.query(
`SELECT
e.employee_code,
p.first_name,
p.last_name,
d.name AS department,
j.title AS designation,
c.work_email
FROM employees e
JOIN partners p ON e.partner_id = p.partner_id
JOIN contracts c ON e.employee_id = c.employee_id
JOIN departments d ON c.department_id = d.department_id
JOIN job_positions j ON c.job_id = j.job_id`
);
ctx.response.body = {
success: true,
count: (rows as any[]).length,
data: rows
};
} catch (error) {
ctx.response.status = 500;
ctx.response.body = {
success: false,
error: (error as Error).message
};
}
});
export default router;