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

25 PUT alumni/:studentId #60

Merged
merged 10 commits into from
Apr 3, 2020
Merged
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
6 changes: 3 additions & 3 deletions server/controllers/routes/admin/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const router = require('express').Router();

const { deleteStudent, putStudentData } = require('./student');
const { addCohort, deleteCohort, editCohort } = require('./cohort');
const {
addProject,
editProject,
deleteProjectData,
getCohortProjects,
} = require('./project');

const { addCohort, deleteCohort, editCohort } = require('./cohort');
const { deleteStudent } = require('./student');

router.post('/cohorts', addCohort);

router
Expand All @@ -31,6 +30,7 @@ router.put('/projects/:projectId', editProject);
router.post('/projects', addProject);
router.get('/cohorts/:cohortId/projects', getCohortProjects);
router.delete('/alumni/:studentId', deleteStudent);
router.put('/alumni/:studentId', putStudentData);
router.delete('/projects/:projectId', deleteProjectData);

module.exports = router;
2 changes: 2 additions & 0 deletions server/controllers/routes/admin/student/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const deleteStudent = require('./deleteStudent');
const putStudentData = require('./putStudentData');

module.exports = {
deleteStudent,
putStudentData,
};
36 changes: 36 additions & 0 deletions server/controllers/routes/admin/student/putStudentData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { putStudent } = require('../../../../database/queries');
const studentSchema = require('../../../../validation/studentSchema');

const putStudentData = async (req, res, next) => {
try {
const { studentId } = req.params;
const data = await studentSchema.validate(
{ ...req.body, studentId },
{ abortEarly: false },
);
const result = await putStudent(data);
if (result.rowCount === 1) {
res.json({
StatusCode: 200,
data: {
message: "Student's data updated successfully",
},
});
} else {
res.status(404).json({
StatusCode: 404,
data: {
message: 'There is no student for this id',
},
});
}
} catch (err) {
if (err.errors) {
res.status(400).json({ statusCode: 400, data: { message: err.errors } });
} else {
next(err);
}
}
};

module.exports = putStudentData;
4 changes: 2 additions & 2 deletions server/database/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { deleteStudentQuery, putStudent } = require('./student');
const {
putSpecificCohort,
getCohorts,
Expand All @@ -12,8 +13,6 @@ const {
getCohortProjectsQuery,
} = require('./project');

const { deleteStudentQuery } = require('./student');

module.exports = {
postCohort,
getCohorts,
Expand All @@ -24,5 +23,6 @@ module.exports = {
getCohortProjectsQuery,
editProjectQuery,
deleteStudentQuery,
putStudent,
deleteProject,
};
2 changes: 2 additions & 0 deletions server/database/queries/student/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const deleteStudentQuery = require('./deleteStudentQuery');
const putStudent = require('./putStudent');

module.exports = {
deleteStudentQuery,
putStudent,
};
11 changes: 11 additions & 0 deletions server/database/queries/student/putStudent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const connection = require('../../config/connection');

const putStudent = (reqData) => {
const { name, email, imgUrl, githubLink, cohortId, studentId } = reqData;
return connection.query(
'UPDATE student SET name = $1, email = $2, img_url = $3, github_link = $4, cohort_id = $5 WHERE id = $6',
[name, email, imgUrl, githubLink, cohortId, studentId],
);
};

module.exports = putStudent;
2 changes: 2 additions & 0 deletions server/validation/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const cohortSchema = require('./cohortSchema ');
const { editCohortSchema } = require('./editCohort');
const projectSchema = require('./projectSchema');
const studentSchema = require('./studentSchema');

module.exports = {
cohortSchema,
projectSchema,
editCohortSchema,
studentSchema,
};
12 changes: 12 additions & 0 deletions server/validation/studentSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const yup = require('yup');

const studentSchema = yup.object({
name: yup.string().required(),
email: yup.string().email(),
imgUrl: yup.string().url().required(),
githubLink: yup.string().url().required(),
cohortId: yup.number().required(),
studentId: yup.number().required(),
});

module.exports = studentSchema;
87 changes: 87 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,90 @@ describe('Delete specific student by ID', () => {
});
});
});

describe('Admin, Put project', () => {
const validData = {
name: 'Rehab',
email: '[email protected]',
imgUrl: 'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
githubLink: 'https://github.com/rehabas',
cohortId: 1,
};
const invalidData = {
name: 'Rehab',
email: 'email',
imgUrl: 'img url',
githubLink: 'github link',
cohortId: 1,
};

test('PUT Route /alumni/1 status 200, json header, put data ', (done) => {
request(app)
.put('/api/v1/alumni/1')
.send(validData)
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
const { rows } = await connection.query(
'SELECT * from student WHERE id = 1',
);
expect(rows).toHaveLength(1);
expect(rows[0]).toEqual({
id: 1,
name: 'Rehab',
email: '[email protected]',
img_url:
'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
github_link: 'https://github.com/rehabas',
cohort_id: 1,
});
expect(message).toBe("Student's data updated successfully");
done();
});
});

test('PUT Route /alumni/11 status 404, json header, put data ', (done) => {
request(app)
.put('/api/v1/alumni/11')
.send(validData)
.expect(404)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
const { rows } = await connection.query(
'SELECT * from student WHERE id = 11',
);
expect(rows).toHaveLength(0);
expect(message).toBe('There is no student for this id');
done();
});
});

test('PUT Route /alumni/1 status 400, json header, put invalid data ', (done) => {
request(app)
.put('/api/v1/alumni/1')
.send(invalidData)
.expect(400)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
await connection.query('SELECT * from student WHERE id = 1');
expect(message).toEqual([
'email must be a valid email',
'imgUrl must be a valid URL',
'githubLink must be a valid URL',
]);
done();
});
});
});