This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Redo data validation #17
Open
isaiahdoyle
wants to merge
8
commits into
main
Choose a base branch
from
redo-data-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
084a37a
install ajv & json-loader
isaiahdoyle ab8a788
validate courseItem router
isaiahdoyle 6f1b7ac
validate course router
isaiahdoyle dbf1e7c
validate semester router
isaiahdoyle 9adf7a8
validate user router
isaiahdoyle a5c9c90
remove user PUT and move schema to root
isaiahdoyle 5b2bde2
pr suggestions
isaiahdoyle e032f5f
add missing errors
isaiahdoyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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