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

implemented validation for gym inputs on getMetadata, getRecords, and… #71

Closed
wants to merge 2 commits into from
Closed
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 5 additions & 3 deletions server/src/controllers/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export const getAllOccupancy = async (req: Request, res: Response) => {
};

export const getOccupancy = async (req: Request, res: Response) => {
const {gym} = req.params;
try {
const {gym} = req.params;
await predict.validateGymReq(gym);
const mostRecentRecord: OccupancyRecord = await db.getRecentRecord(gym);
// const finalOccupancy : BTG_Occupancy = {count: mostRecentRecord.occupancy};

Expand Down Expand Up @@ -73,8 +74,8 @@ export const predictOccupancy = async (req: Request, res: Response) => {
// TODO: Get all records from a specific gym
export const getRecords = async (req: Request, res: Response) => {
const { gym } = req.params;

try {
await predict.validateGymReq(gym);
const data = await db.getRecords(gym);
res.status(HTTP_STATUS.OK).json(data);
} catch (error) {
Expand Down Expand Up @@ -102,8 +103,9 @@ export const getAllMetadata = async (req: Request, res: Response) => {
};

export const getMetadata = async (req: Request, res: Response) => {
const {gym} = req.params;
try {
const {gym} = req.params;
await predict.validateGymReq(gym);
const meta : Metadata = await db.getMetadata(gym);
res.status(HTTP_STATUS.OK).json(meta);
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ export const validatePredictReq = async (gym: string, timestamp: string) => {
throw new Error(`Invalid Gym ${gym}`);
}
};

export const validateGymReq = async (gym: string) => {
const names = await db.getAllNames();
if (!names.includes(gym)) {
throw new Error(`Inavalid Gym ${gym}`);
}
};
File renamed without changes.