-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
224 additions
and
4 deletions.
There are no files selected for viewing
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,5 +1,7 @@ | ||
const deleteStudent = require('./deleteStudent'); | ||
const putStudentData = require('./putStudentData'); | ||
|
||
module.exports = { | ||
deleteStudent, | ||
putStudentData, | ||
}; |
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 |
---|---|---|
@@ -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; |
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,7 +1,9 @@ | ||
const getAlumniQuery = require('./getAlumni'); | ||
const deleteStudentQuery = require('./deleteStudentQuery'); | ||
const putStudent = require('./putStudent'); | ||
|
||
module.exports = { | ||
getAlumniQuery, | ||
deleteStudentQuery, | ||
putStudent, | ||
}; |
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 |
---|---|---|
@@ -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; |
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,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, | ||
}; |
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 |
---|---|---|
@@ -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; |
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 |
---|---|---|
|
@@ -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(); | ||
}); | ||
}); | ||
}); |