Skip to content

Commit

Permalink
update master
Browse files Browse the repository at this point in the history
relates #26
  • Loading branch information
MohammedAlghazali committed Apr 3, 2020
2 parents 944a7b9 + ef66b41 commit 4c0d0c5
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 4 deletions.
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,
};
43 changes: 43 additions & 0 deletions server/controllers/routes/admin/student/putStudentData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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 if (err.detail) {
res.status(409).json({
statusCode: 409,
data: {
message: err.detail,
},
});
} else {
next(err);
}
}
};

module.exports = putStudentData;
3 changes: 2 additions & 1 deletion server/database/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getAlumniQuery, deleteStudentQuery } = require('./student');
const { getAlumniQuery, deleteStudentQuery, putStudent } = require('./student');

const {
putSpecificCohort,
Expand Down Expand Up @@ -26,6 +26,7 @@ module.exports = {
getCohortProjectsQuery,
editProjectQuery,
deleteStudentQuery,
putStudent,
getProjectById,
deleteProject,
};
2 changes: 2 additions & 0 deletions server/database/queries/student/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const getAlumniQuery = require('./getAlumni');
const deleteStudentQuery = require('./deleteStudentQuery');
const putStudent = require('./putStudent');

module.exports = {
getAlumniQuery,
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().required(),
imgUrl: yup.string().url().required(),
githubLink: yup.string().url().required(),
cohortId: yup.number().required(),
studentId: yup.number().required(),
});

module.exports = studentSchema;
147 changes: 147 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,150 @@ 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,
};
const duplicateData = {
name: 'Rehab',
email: '[email protected]',
imgUrl: 'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
githubLink: 'https://github.com/rehabas',
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/1 status 200, json header, put data with same email', (done) => {
request(app)
.put('/api/v1/alumni/1')
.send({
name: 'sara',
email: '[email protected]',
imgUrl: 'https://avatars3.githubusercontent.com/u/49806841?s=460&v=4',
githubLink: 'https://github.com/rehabas',
cohortId: 1,
})
.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: 'sara',
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();
});
});

test('PUT Route /alumni/1 status 409, json header, put data ', (done) => {
request(app)
.put('/api/v1/alumni/1')
.send(duplicateData)
.expect(409)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const {
data: { message },
} = res.body;
expect(message).toBe(
`Key (email)=(${duplicateData.email}) already exists.`,
);
done();
});
});
});

0 comments on commit 4c0d0c5

Please sign in to comment.