diff --git a/routes/students.js b/routes/students.js index 4820e44..4a6422b 100644 --- a/routes/students.js +++ b/routes/students.js @@ -1,4 +1,76 @@ const router = require('express').Router(); +const Student = require('../db/models/student') +const Test = require('../db/models/test') + // router.use(bodyParser.json()); -module.exports = router; + +router.get('/', async(req, res) => { + const students = await Student.findAll({ + attributes: ['firstName'] + }) + res.send(students) +}) + +router.get('/:id', async(req, res, next) => { + try { + const student = await Student.findById(req.params.id) + console.log('------------- get', student) + if (student) { + res.send(student) + } else { res.status(404).send('Sorry, this page is not valid') } + + } catch (error) { + next(error) + } +}) + +router.post('/', async(req, res, next) => { + try { + const newStudent = await Student.create(req.body) + res.status(201).send(newStudent) + } catch (error) { + next(error) + } +}) + +router.put('/:id', async(req, res, next) => { + try { + //best practice to always send req.body instead of specifying what to update + const update = await Student.update((req.body), { + where: { + id: req.params.id + }, + returning: true, + //required for .update + plain: true + //required for .update + }) + console.log('++++++++++++update', update) + res.status(201).send(update[1]) + //use update[1] since update creates an array and the first element is # of rows updated. The object lives at update[1] + } catch (error) { + next(error) + } +}) + +router.delete('/:id', async(req, res, next) => { + try { + const destroy = await Student.destroy({ + where: { + id: req.params.id + }, + }) + console.log('-------------destroy', destroy) + //destroy will return a number, so we send back req.body + res.status(204).send(req.body) + } catch (error) { + next(error) + } +}) + +router.use((req, res) => { + res.status(404).send('Sorry, this page is not valid') +}) + +module.exports = router; \ No newline at end of file diff --git a/test/02_routes.test.js b/test/02_routes.test.js index 62033ee..626acee 100644 --- a/test/02_routes.test.js +++ b/test/02_routes.test.js @@ -11,217 +11,215 @@ const Student = require('../db/models/student'); const Test = require('../db/models/test'); describe('Routes', () => { - before(() => { - return db.sync({ force: true }); - }); - - afterEach(() => { - return Promise.all([ - Student.truncate({ cascade: true }), - Test.truncate({ cascade: true }), - ]); - }); - - describe('Student Routes', () => { - let pepper; - let peter; - let charlie; - - beforeEach(() => { - const creatingStudents = [ - { - firstName: 'Pepper', - lastName: 'Potts', - email: 'saltn@pepper.com', - }, - { - firstName: 'Peter', - lastName: 'Parker', - email: 'spidey@email.com', - }, - { - firstName: 'Charlie', - lastName: 'Brown', - email: 'cb@cbdb.com', - }, - ].map(data => Student.create(data)); - return Promise.all(creatingStudents).then(createdStudents => { - pepper = createdStudents[0]; - peter = createdStudents[1]; - charlie = createdStudents[2]; - }); + before(() => { + return db.sync({ force: true }); }); - describe('GET /students', () => { - xit('retrieves all the students', () => { - return agent - .get('/students') - .expect('Content-Type', /json/) - .expect(200) - .expect(res => { - expect(res.body).to.be.an.instanceOf(Array); - expect(res.body).to.have.length(3); - }); - }); + afterEach(() => { + return Promise.all([ + Student.truncate({ cascade: true }), + Test.truncate({ cascade: true }), + ]); }); - describe('GET /students/:id', () => { - xit('retrieves a single student by their id', () => { - return agent - .get(`/students/${pepper.id}`) - .expect(200) - .expect(res => { - if (typeof res.body === 'string') res.body = JSON.parse(res.body); - expect(res.body.firstName).to.equal('Pepper'); - }); - }); - - xit('returns a 404 error if student does not exist in DB', () => { - return agent.get('/students/09432').expect(404); - }); - }); + describe('Student Routes', () => { + let pepper; + let peter; + let charlie; + + beforeEach(() => { + const creatingStudents = [{ + firstName: 'Pepper', + lastName: 'Potts', + email: 'saltn@pepper.com', + }, + { + firstName: 'Peter', + lastName: 'Parker', + email: 'spidey@email.com', + }, + { + firstName: 'Charlie', + lastName: 'Brown', + email: 'cb@cbdb.com', + }, + ].map(data => Student.create(data)); + return Promise.all(creatingStudents).then(createdStudents => { + pepper = createdStudents[0]; + peter = createdStudents[1]; + charlie = createdStudents[2]; + }); + }); - describe('POST /students', () => { - xit('creates a new Student instance', () => { - return agent - .post('/students') - .send({ - firstName: 'SQL', - lastName: 'PRK', - email: 'sqlprk@db.com', - }) - .expect(201) - .expect('Content-Type', /json/) - .expect(res => { - expect(res.body.firstName).to.equal('SQL'); - }); - }); - }); + describe('GET /students', () => { + it('retrieves all the students', () => { + return agent + .get('/students') + .expect('Content-Type', /json/) + .expect(200) + .expect(res => { + expect(res.body).to.be.an.instanceOf(Array); + expect(res.body).to.have.length(3); + }); + }); + }); - describe('PUT /students/:id', () => { - xit('updates an instance of a student', () => { - return agent - .put(`/students/${pepper.id}`) - .send({ firstName: 'Salty' }) - .expect(200) - .expect('Content-Type', /json/) - .expect(res => { - expect(res.body.firstName).to.equal('Salty'); - }); - }); - }); + describe('GET /students/:id', () => { + it('retrieves a single student by their id', () => { + return agent + .get(`/students/${pepper.id}`) + .expect(200) + .expect(res => { + if (typeof res.body === 'string') res.body = JSON.parse(res.body); + expect(res.body.firstName).to.equal('Pepper'); + }); + }); - describe('DELETE /students/:id', () => { - xit('deletes an instance of a student', () => { - return agent - .delete(`/students/${charlie.id}`) - .expect(204) - .expect(() => { - return Student.findById(charlie.id).then(res => - expect(res).to.equal(null) - ); - }); - }); - }); - }); - - describe('Test Routes', () => { - let funTest; - let badTest; - let hardTest; - let crayTest; - beforeEach(() => { - const creatingTests = [ - { - subject: 'Tree-Climbing', - grade: 81, - }, - { - subject: 'Outdoor Wilderness Survival', - grade: 43, - }, - { - subject: 'Wind-Surfing', - grade: 85, - }, - { - subject: 'Outdoor Wilderness Survival', - grade: 66, - }, - ].map(data => Test.create(data)); - return Promise.all(creatingTests).then(createdTests => { - funTest = createdTests[0]; - badTest = createdTests[1]; - hardTest = createdTests[2]; - crayTest = createdTests[3]; - }); - }); - afterEach(() => { - return Promise.all([ - Student.truncate({ cascade: true }), - Test.truncate({ cascade: true }), - ]); - }); + it('returns a 404 error if student does not exist in DB', () => { + return agent.get('/students/09432').expect(404); + }); + }); - describe('GET /tests', () => { - xit('retrieves all tests', () => { - return agent - .get('/tests') - .expect(200) - .expect(res => { - expect(res.body).to.be.an.instanceOf(Array); - expect(res.body).to.have.length(4); - }); - }); - }); + describe('POST /students', () => { + it('creates a new Student instance', () => { + return agent + .post('/students') + .send({ + firstName: 'SQL', + lastName: 'PRK', + email: 'sqlprk@db.com', + }) + .expect(201) + .expect('Content-Type', /json/) + .expect(res => { + expect(res.body.firstName).to.equal('SQL'); + }); + }); + }); - describe('GET /tests/:id', () => { - xit('gets the test instance by id', () => { - return agent - .get(`/tests/${funTest.id}`) - .expect(200) - .expect(res => { - expect(res.body.subject).to.equal(funTest.subject); - }); - }); - }); + describe('PUT /students/:id', () => { + it('updates an instance of a student', () => { + return agent + .put(`/students/${pepper.id}`) + .send({ firstName: 'Salty' }) + .expect(200) + .expect('Content-Type', /json/) + .expect(res => { + expect(res.body.firstName).to.equal('Salty'); + }); + }); + }); - describe('POST /tests/student/:studentId', () => { - let student; - beforeEach(() => { - return Student.create({ - firstName: 'Pepper', - lastName: 'Potts', - email: 'saltn@pepper.com', - }).then(newStudent => { - student = newStudent; + describe('DELETE /students/:id', () => { + it('deletes an instance of a student', () => { + return agent + .delete(`/students/${charlie.id}`) + .expect(204) + .expect(() => { + return Student.findById(charlie.id).then(res => + expect(res).to.equal(null) + ); + }); + }); }); - }); - xit('creates a new Test instance for a student', () => { - return agent - .post(`/tests/student/${student.id}`) - .send({ - subject: 'Outdoor Wilderness Survival', - grade: 43, - }) - .expect(201) - .expect('Content-Type', /json/) - .expect(res => { - expect(res.body.studentId).to.equal(student.id); - }); - }); }); - describe('DELETE /tests/:id', () => { - xit('deletes an instance of test by its id', () => { - return agent - .delete(`/tests/${crayTest.id}`) - .expect(204) - .expect(() => { - return Test.findById(crayTest.id).then(res => { - expect(res).to.equal(null); + + describe('Test Routes', () => { + let funTest; + let badTest; + let hardTest; + let crayTest; + beforeEach(() => { + const creatingTests = [{ + subject: 'Tree-Climbing', + grade: 81, + }, + { + subject: 'Outdoor Wilderness Survival', + grade: 43, + }, + { + subject: 'Wind-Surfing', + grade: 85, + }, + { + subject: 'Outdoor Wilderness Survival', + grade: 66, + }, + ].map(data => Test.create(data)); + return Promise.all(creatingTests).then(createdTests => { + funTest = createdTests[0]; + badTest = createdTests[1]; + hardTest = createdTests[2]; + crayTest = createdTests[3]; + }); + }); + afterEach(() => { + return Promise.all([ + Student.truncate({ cascade: true }), + Test.truncate({ cascade: true }), + ]); + }); + + describe('GET /tests', () => { + xit('retrieves all tests', () => { + return agent + .get('/tests') + .expect(200) + .expect(res => { + expect(res.body).to.be.an.instanceOf(Array); + expect(res.body).to.have.length(4); + }); + }); + }); + + describe('GET /tests/:id', () => { + xit('gets the test instance by id', () => { + return agent + .get(`/tests/${funTest.id}`) + .expect(200) + .expect(res => { + expect(res.body.subject).to.equal(funTest.subject); + }); + }); + }); + + describe('POST /tests/student/:studentId', () => { + let student; + beforeEach(() => { + return Student.create({ + firstName: 'Pepper', + lastName: 'Potts', + email: 'saltn@pepper.com', + }).then(newStudent => { + student = newStudent; + }); }); - }); - }); + xit('creates a new Test instance for a student', () => { + return agent + .post(`/tests/student/${student.id}`) + .send({ + subject: 'Outdoor Wilderness Survival', + grade: 43, + }) + .expect(201) + .expect('Content-Type', /json/) + .expect(res => { + expect(res.body.studentId).to.equal(student.id); + }); + }); + }); + describe('DELETE /tests/:id', () => { + xit('deletes an instance of test by its id', () => { + return agent + .delete(`/tests/${crayTest.id}`) + .expect(204) + .expect(() => { + return Test.findById(crayTest.id).then(res => { + expect(res).to.equal(null); + }); + }); + }); + }); }); - }); -}); +}); \ No newline at end of file