You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.4 KiB
TypeScript

import express, { Router, Request, Response } from "express";
import Joi from "joi"
import { client } from "../db";
import { DatabaseError } from "pg";
import Trainer from "../interfaces/trainer";
import { AdminAuth } from "../middlewares/auth";
import { trainerExists } from "../middlewares/trainer";
const router: Router = express.Router();
router.get("/trainer", async (req: Request, res: Response) => {
try {
const databaseResult = await client.query(`
SELECT trainers.id, first_name, last_name, center_id, centers.name as center_name FROM trainers
JOIN users ON trainers.user_id = users.id
JOIN centers on trainers.center_id = centers.id;
`);
const trainers: Trainer[] = databaseResult.rows;
return res.status(200).send(trainers);
} catch (error: DatabaseError | Error | any) {
console.error(error);
return res.sendStatus(500);
}
})
const createTrainerSchema = Joi.object({
user_id: Joi.number().integer().positive().required(),
center_id: Joi.number().integer().positive().required(),
hourly_price: Joi.number().integer().positive().required()
});
router.post("/trainer", AdminAuth, async (req: Request, res: Response) => {
try {
const validation = createTrainerSchema.validate(req.body, { abortEarly: false });
if (validation.error !== undefined) {
return res.status(400).send(validation.error.details);
}
const databaseResult = await client.query(`
WITH inserted AS (
INSERT INTO trainers (user_id, center_id, hourly_price)
VALUES ($1, $2, $3)
RETURNING *
)
SELECT inserted.id, user_id, first_name, last_name, center_id, centers.name as center_name from inserted
JOIN users ON inserted.user_id = users.id
JOIN centers on inserted.center_id = centers.id;
`, [
validation.value.user_id,
validation.value.center_id,
validation.value.hourly_price
]);
const trainers: Trainer[] = databaseResult.rows;
return res.status(200).send(trainers);
} catch (error: DatabaseError | Error | any) {
if (error.constraint == "trainers_user_id_key") {
return res.status(400).send([{
message: "\"user_id\" is already a trainer",
path: [
"user_id"
],
type: "user_id.unique"
}]);
}
if (error.constraint == "trainers_center_id_fkey") {
return res.status(400).send([{
message: "\"center_id\" is not a valid center",
path: [
"center_id"
],
type: "center_id.not_found"
}]);
}
console.error(error);
return res.sendStatus(500);
}
})
const updateTrainerSchema = Joi.object({
center_id: Joi.number().integer().positive().required(),
hourly_price: Joi.number().integer().positive().required()
});
router.put("/trainer/:trainer_id", AdminAuth, trainerExists, async (req: Request, res: Response) => {
try {
const validation = updateTrainerSchema.validate(req.body, { abortEarly: false });
if (validation.error !== undefined) {
return res.status(400).send(validation.error.details);
}
const databaseResult = await client.query(`
WITH updated AS (
UPDATE trainers SET
center_id = $1,
hourly_price = $2
WHERE id = $3
RETURNING *
)
SELECT updated.id, user_id, first_name, last_name, center_id, centers.name as center_name from updated
JOIN users ON updated.user_id = users.id
JOIN centers on updated.center_id = centers.id;
`, [
validation.value.center_id,
validation.value.hourly_price,
req.params.id
]);
const trainers: Trainer[] = databaseResult.rows;
return res.status(200).send(trainers);
} catch (error: DatabaseError | Error | any) {
if (error.constraint == "trainers_center_id_fkey") {
return res.status(400).send([{
message: "\"center_id\" is not a valid center",
path: [
"center_id"
],
type: "center_id.not_found"
}]);
}
console.error(error);
return res.sendStatus(500);
}
})
export default router;