Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Redo data validation #17

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
8,633 changes: 5,925 additions & 2,708 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@
},
"homepage": "https://github.com/Vikelabs/PassrAPI#readme",
"dependencies": {
"@prisma/client": "^4.13.0",
"@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.1.1",
"@rollup/plugin-typescript": "^8.1.1",
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^4.14.1",
"@typescript-eslint/parser": "^4.14.1",
"ajv": "^8.12.0",
"dotenv": "^8.2.0",
"dynamoose": "^2.7.3",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.3.1",
"express": "^4.17.1",
"json-loader": "^0.5.7",
"nodemon": "^2.0.7",
"prettier": "^2.2.1",
"prisma-json-schema-generator": "^3.1.4",
"rollup": "^2.38.2",
"ts-node": "^9.1.1",
"uuid": "^8.3.2"
Expand All @@ -45,6 +49,7 @@
"@types/express": "^4.17.9",
"@types/node": "^14.14.22",
"@types/uuid": "^8.3.1",
"prisma": "^4.13.0",
"typescript": "^4.1.3"
}
}
74 changes: 39 additions & 35 deletions src/routers/course.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,79 @@
import express from 'express';
import Course from '../models/course';
import { create, read, update, del } from '../operators/CourseOperations';
import Ajv from 'ajv';
import schema from '../schema.json';

const courseRouter = express.Router();
const ajv = new Ajv({ removeAdditional: true });

const schemas = schema.components.schemas;
const checkPost = ajv.compile(schemas.CourseCreate);
const checkPut = ajv.compile(schemas.CourseUpdate);

const ERROR_RESPONSE = 'Course not found.';

courseRouter.get('/:id', async (req, res) => {
try {
const id = req.params.id;
const userID = req.header('userID');

if (id && userID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to, you could invert this statement

if(!id || !userID) { 
	throw ...
}

// do normal stuff here

const course = await read(id, userID);
if (course) {
const resData = {
...course,
courseItems: Array.from(course.courseItems || []),
};
res.send(resData);
} else {
throw 'ERROR - course undefined';
}
// TODO: call db operation
res.status(200); // + .json(course)
} else {
throw 'ERROR - id undefined';
}
} catch (err) {
res.status(404).send('Not found.');
res.status(404).send(ERROR_RESPONSE);
}
});

courseRouter.post('/', async (req, res) => {
try {
const userID = req.header('userID');
if (userID) {
const course = new Course({
owner: userID,
name: req.body.name,
});
const created = await create(course);
res.json(created);
const body = req.body;

if (checkPost(body)) throw Error('body invalid');

console.log(body); // TODO: call db operation
res.status(201); // + .json(created)
} else {
throw Error('invalid user id');
}
} catch (err) {
res.status(404).send('Not found.');
res.status(404).send(ERROR_RESPONSE);
}
});

courseRouter.put('/', async (req, res) => {
courseRouter.put('/:id', async (req, res) => {
try {
const id = req.params.id;
const userID = req.header('userID');
if (userID) {
const course = new Course({
id: req.body.id,
name: req.body.name,
});
const updated = await update(course, userID);
res.json(updated);
if (id && userID) {
const body = req.body;

if (checkPut(body)) throw Error('body invalid');

console.log(body); // TODO: call db operation
res.status(200); // + .json(updated)
} else {
throw Error(`invalid ${id ? 'user' : 'course'} id`);
}
} catch (err) {
res.status(404).send('Not found.');
res.status(404).send(ERROR_RESPONSE);
}
});

courseRouter.delete('/', async (req, res) => {
courseRouter.delete('/:id', async (req, res) => {
try {
const id = req.params.id;
const userID = req.header('userID');

if (userID) {
await del(req.body.id, userID);
res.send('Delete courseRouter');
if (id && userID) {
// TODO: call db operation
res.status(200);
}
} catch (err) {
res.status(404).send('Not found.');
res.status(404).send(ERROR_RESPONSE);
}
});

Expand Down
64 changes: 28 additions & 36 deletions src/routers/courseItem.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import express from 'express';
import { create, read, update, del } from '../operators/courseItemOperations';
import CourseItem from '../models/courseItem';
import Ajv from 'ajv';
import schema from '../schema.json';

const cItemRouter = express.Router();
const ajv = new Ajv({ removeAdditional: true });

const schemas = schema.components.schemas;
const checkPost = ajv.compile(schemas.CourseItemCreate);
const checkPut = ajv.compile(schemas.CourseItemUpdate);

const ERROR_RESPONSE = 'Course item not found.';

Expand All @@ -11,10 +16,10 @@ cItemRouter.get('/:id', async (req, res) => {
const id = req.params.id;
const userID = req.header('userID');
if (id && userID) {
const courseItem = await read(id, userID);
res.send(courseItem);
// TODO: call db operation
res.status(200); // + .json(courseItem)
} else {
throw 'ERROR - id undefined';
throw Error('id not found');
}
} catch (err) {
res.status(404).send(ERROR_RESPONSE);
Expand Down Expand Up @@ -42,54 +47,41 @@ cItemRouter.post('/', async (req, res) => {
const userID = req.header('userID');
if (userID) {
const body = req.body;
const courseItem = new CourseItem({
id: body.id,
owner: userID,
name: body.name,
weight: numberify(body.weight),
grade: numberify(body.grade),
date: body.date,
createdAt: body.createdAt,
updatedAt: body.updatedAt,
});
const created = await create(courseItem);
res.json(created);

if (checkPost(body)) throw Error('body invalid');

console.log(body); // TODO: call db operation
res.status(201); // + .json(created)
}
} catch (err) {
res.status(404).send(ERROR_RESPONSE);
}
});

cItemRouter.put('/', async (req, res) => {
cItemRouter.put('/:id', async (req, res) => {
try {
const id = req.params.id;
const userID = req.header('userID');
if (userID) {
if (id && userID) {
const body = req.body;
const name = body.name;
const weight = numberify(body.weight);
const grade = numberify(body.grade);
const date = body.date;
const courseItem = new CourseItem({
id: body.id,
...(name ? { name } : {}),
...(weight ? { weight } : {}),
...(grade ? { grade } : {}),
...(date ? { date } : {}),
});
const updated = await update(courseItem, userID);
res.json(updated);

if (checkPut(body)) throw Error('body invalid');

console.log(body); // TODO: call db operation
res.status(200); // + .json(updated)
}
} catch (err) {
res.status(404).send(ERROR_RESPONSE);
}
});

cItemRouter.delete('/', async (req, res) => {
cItemRouter.delete('/:id', async (req, res) => {
try {
const id = req.params.id;
const userID = req.header('userID');
if (userID) {
await del(req.body.id, userID);
res.send('Delete cItemRouter');
if (id && userID) {
// TODO: call db operation
res.status(200);
}
} catch (err) {
res.status(404).send(ERROR_RESPONSE);
Expand Down
70 changes: 41 additions & 29 deletions src/routers/semester.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import express from 'express';
import { create, read, update, del } from '../operators/semesterOperations';
import Semester from '../models/semester';
import Ajv from 'ajv';
import schema from '../schema.json';

const semesterRouter = express.Router();
const ajv = new Ajv({ removeAdditional: true });

const schemas = schema.components.schemas;
const checkPost = ajv.compile(schemas.SemesterCreate);
const checkPut = ajv.compile(schemas.SemesterUpdate);

const ERROR_RESPONSE = 'Semester not found.';

semesterRouter.get('/:id', async (req, res) => {
try {
const id = req.params.id;
const userID = req.header('userID');
if (userID) {
const semester = await read(req.params.id, userID);
const resData = {
...semester,
courses: Array.from(semester?.courses || []),
};
res.send(resData);
if (id && userID) {
/* leaving this here as a reminder to add courses array if necessary */
// const semester = await read(req.params.id, userID);
// const resData = {
// ...semester,
// courses: Array.from(semester?.courses || []),
// };
isaiahdoyle marked this conversation as resolved.
Show resolved Hide resolved

// TODO: call db operation
res.status(200); // + .json(semester)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need the else statement here as well (or invert the if statement and throw)

}
} catch (err) {
res.status(404).send(ERROR_RESPONSE);
Expand All @@ -26,43 +35,46 @@ semesterRouter.post('/', async (req, res) => {
try {
const userID = req.header('userID');
if (userID) {
const semester = new Semester({
owner: userID,
name: req.body.name,
courseItems: [],
});
const created = await create(semester);
res.json(created);
const body = req.body;

if (checkPost(body)) throw Error('body invalid');

console.log(body); // TODO: call db operation
res.status(201); // + .json(created);
} else {
throw Error('invalid user id');
}
} catch (err) {
res.status(404).send(ERROR_RESPONSE);
}
});

semesterRouter.put('/', async (req, res) => {
semesterRouter.put('/:id', async (req, res) => {
try {
const id = req.params.id;
const userID = req.header('userID');
if (userID) {
if (id && userID) {
const body = req.body;
const semester = new Semester({
id: body.id,
owner: userID,
name: body.name,
});
const updated = await update(semester, userID);
res.json(updated);

if (checkPut(body)) throw Error('body invalid');

console.log(body); // TODO: call db operation
res.status(200); // + .json(updated)
} else {
throw Error(`invalid ${id ? 'user' : 'semester'} id`);
}
} catch (err) {
res.status(404).send(ERROR_RESPONSE);
}
});

semesterRouter.delete('/', async (req, res) => {
semesterRouter.delete('/:id', async (req, res) => {
try {
const id = req.params.id;
const userID = req.header('userID');
if (userID) {
await del(req.body.id, userID);
res.send('Semester deleted');
if (id && userID) {
// TODO: call db operation
res.status(200);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need the else statement here

}
} catch (err) {
res.status(404).send(ERROR_RESPONSE);
Expand Down
Loading