Skip to content
Open
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
51 changes: 26 additions & 25 deletions db/models/student.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,36 @@ const Sequelize = require('sequelize');
const db = require('../db');

const Student = db.define('student', {
firstName: {
type: Sequelize.STRING,
allowNull: false,
// validate: {
// is: /[\w]+/,
// },
},
lastName: {
type: Sequelize.STRING,
allowNull: false,
// validate: {
// is: /[\w]+/,
// },
},
email: {
type: Sequelize.STRING,
allowNull: false,
validate: {
isEmail: true,
firstName: {
type: Sequelize.STRING,
allowNull: false,
// validate: {
// is: /[\w]+/,
// },
},
lastName: {
type: Sequelize.STRING,
allowNull: false,
// validate: {
// is: /[\w]+/,
// },
},
email: {
type: Sequelize.STRING,
allowNull: false,
validate: {
isEmail: true,
},
},
},
});

Student.beforeCreate(student => {
const nameFirst = student.firstName;
const nameLast = student.lastName;
const nameFirst = student.firstName;
const nameLast = student.lastName;

student.firstName = nameFirst[0].toUpperCase() + nameFirst.slice(1);
student.lastName = nameLast[0].toUpperCase() + nameLast.slice(1);
// Captalize the first letter of first name + last name
student.firstName = nameFirst[0].toUpperCase() + nameFirst.slice(1);
student.lastName = nameLast[0].toUpperCase() + nameLast.slice(1);
});

module.exports = Student;
module.exports = Student;
54 changes: 27 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"name": "study-staturday-week-2",
"version": "1.0.0",
"description": "fun w express and sequelize!",
"main": "index.js",
"scripts": {
"test": "nodemon --exec 'mocha --reporter spec --timeout 1000 test/*.test.js || true'",
"start": "nodemon app.js"
},
"author": "myself",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2",
"morgan": "^1.9.0",
"nodemon": "^1.17.1",
"pg": "^7.4.1",
"pg-hstore": "^2.3.2",
"sequelize": "^4.32.2"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.0.0",
"sinon": "^4.2.2",
"sinon-chai": "^2.14.0",
"supertest": "^3.0.0"
}
}
"name": "study-staturday-week-2",
"version": "1.0.0",
"description": "fun w express and sequelize!",
"main": "index.js",
"scripts": {
"test": "nodemon --exec mocha --reporter spec --timeout 1000 test/*.test.js || true",
"start": "nodemon app.js"
},
"author": "myself",
"license": "ISC",
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2",
"morgan": "^1.9.0",
"nodemon": "^1.17.1",
"pg": "^7.4.1",
"pg-hstore": "^2.3.2",
"sequelize": "^4.32.2"
},
"devDependencies": {
"chai": "^4.1.2",
"mocha": "^5.0.0",
"sinon": "^4.2.2",
"sinon-chai": "^2.14.0",
"supertest": "^3.0.0"
}
}
91 changes: 48 additions & 43 deletions routes/students.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,62 @@
// require the router
const router = require('express').Router();
const Student = require('../db/models/student');

router.get('/', async (req, res, next) => {
try {
const students = await Student.findAll();
res.send(students);
} catch (error) {
next(error);
}
// setup routes
router.get('/', async(req, res, next) => {
try {
const students = await Student.findAll();
res.send(students);
} catch (error) {
next(error);
}
});

router.get('/:id', async (req, res, next) => {
try {
let student = await Student.findById(req.params.id);
if (student) {
res.send(student);
} else {
res.status(404).send('Student not found');
router.get('/:id', async(req, res, next) => {
try {
let student = await Student.findById(req.params.id);
if (student) {
res.send(student);
} else {
res.status(404).send('Student not found');
}
} catch (error) {
next(error);
}
} catch (error) {
next(error);
}
});

router.post('/', async (req, res, next) => {
try {
let student = await Student.create(req.body);
res.status(201).send(student);
} catch (err) {
next(err);
}
router.post('/', async(req, res, next) => {
try {
let student = await Student.create(req.body);
res.status(201).send(student);
} catch (err) {
next(err);
}
});

router.put('/:id', async (req, res, next) => {
try {
let updatedStudentInfo = await Student.update(req.body, {
where: { id: req.params.id },
returning: true,
plain: true,
});
res.send(updatedStudentInfo[1]);
} catch (err) {
next(err);
}
router.put('/:id', async(req, res, next) => {
let updatedStudent = req.params.id;
try {
let updatedStudentInfo = await Student.update(req.body, {
where: { id: updatedStudent },
returning: true,
plain: true,
});
res.send(updatedStudentInfo[1]);
} catch (err) {
next(err);
}
});

router.delete('/:id', async (req, res, next) => {
try {
await Student.destroy({ where: { id: req.params.id } });
res.status(204).send();
} catch (err) {
next(err);
}
router.delete('/:id', async(req, res, next) => {
let deletedStudent = req.params.id;
try {
await Student.destroy({ where: { id: deletedStudent } });
res.status(204).send();
} catch (err) {
next(err);
}
});

module.exports = router;
// export the router
module.exports = router;
62 changes: 61 additions & 1 deletion routes/tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
const router = require('express').Router();
const Test = require('../db/models/test');
const Student = require('../db/models/student');

// setup routes
router.get('/', async(req, res, next) => {
try {
const tests = await Test.findAll();
res.send(tests);
} catch (error) {
next(error);
}
});

module.exports = router;
router.get('/:id', async(req, res, next) => {
let testId = req.params.id
try {
let selectedTest = await Test.findById(testId);
if (selectedTest) {
res.send(selectedTest);

// run this if not a json object
res.json(selectedTest);
} else {
res.status(404).send('Test not found');
}
} catch (error) {
next(error);
}
});

router.post('/student/:studentId', async(req, res, next) => {
let studentId = req.params.studentId;

// a post has a req.body - see the test
console.log(req.body, "REQ.BODY")

try {
// figure out the student row
let student = await Student.findById(studentId)
// create a new row to link these together
let newTest = await Test.create(req.body);

// take new test we made
let studentTest = await newTest.setStudent(student);

res.status(201).send(studentTest);
} catch (err) {
next(err);
}
});

router.delete('/:id', async(req, res, next) => {
let deletedTest = req.params.id;
try {
await Test.destroy({ where: { id: deletedTest } });
res.status(204).send();
} catch (err) {
next(err);
}
});

// export the router
module.exports = router;
Loading