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

GET cohort by id #41

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

const { serverError } = require('./middlewares/errorHandle');
const admin = require('./routes/admin');
const { getSpecificCohort } = require('./routes/user');

router.use(serverError);

router.get('/cohorts/:cohortid', getSpecificCohort);
router.use(admin);

router.use(serverError);

module.exports = router;
4 changes: 1 addition & 3 deletions server/controllers/routes/admin/cohort/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const deleteCohort = require('./deleteCohort');

module.exports = {
deleteCohort,
};
module.exports = { deleteCohort };
28 changes: 28 additions & 0 deletions server/controllers/routes/user/cohort/getSpecificCohort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { getCohortQuery } = require('../../../../database/queries');

const getSpecificCohort = async (req, res, next) => {
try {
const { cohortid } = req.params;
if (cohortid > 0) {
const { rows } = await getCohortQuery(cohortid);
const data = { ...rows[0] };
if (data.id) {
res.json({ statusCode: 200, data });
} else {
res.status(404).json({
statusCode: 404,
message: "Sorry There's no cohort for this id",
});
}
} else {
res.status(404).json({
ranasobeid95 marked this conversation as resolved.
Show resolved Hide resolved
statusCode: 404,
message: 'You enterd wrong cohort ID',
});
}
} catch (err) {
next(err);
}
};

module.exports = getSpecificCohort;
3 changes: 3 additions & 0 deletions server/controllers/routes/user/cohort/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const getSpecificCohort = require('./getSpecificCohort');

module.exports = { getSpecificCohort };
3 changes: 3 additions & 0 deletions server/controllers/routes/user/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { getSpecificCohort } = require('./cohort');

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

const getCohortQuery = (id) =>
connection.query('select * from cohort where id = $1', [id]);

module.exports = getCohortQuery;
3 changes: 3 additions & 0 deletions server/database/queries/cohort/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const getCohortQuery = require('./getSpecificCohort');

const deleteCohortQuery = require('./deleteCohort');

module.exports = {
deleteCohortQuery,
getCohortQuery,
};
2 changes: 2 additions & 0 deletions server/database/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const { getCohortQuery } = require('./cohort');
const { deleteCohortQuery } = require('./cohort');
const { addProjectQuery } = require('./project');

module.exports = {
deleteCohortQuery,
getCohortQuery,
addProjectQuery,
};
27 changes: 27 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ beforeAll(() => dbBuild());

afterAll(() => connection.end());

describe('Cohort', () => {
test('Route /cohorts/1 status 200, json header, data.name =G8 ', (done) => {
return request(app)
.get('/api/v1/cohorts/1')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { data } = res.body;
expect(data.name).toBe('G8');
done();
});
});
ranasobeid95 marked this conversation as resolved.
Show resolved Hide resolved

test('Route /cohorts/10 status 404, json header, data.message = "Sorry There\'s no cohort for this id" ', (done) => {
return request(app)
.get('/api/v1/cohorts/10')
.expect(404)
ranasobeid95 marked this conversation as resolved.
Show resolved Hide resolved
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) return done(err);
const { message } = res.body;
expect(message).toBe("Sorry There's no cohort for this id");
done();
});
});
});
describe('Admin, Project', () => {
test('Route /projects status 200, json header, data.message = Cohort Added successfully ', (done) => {
const reqData = {
Expand Down