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 3 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
5 changes: 5 additions & 0 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const router = require('express').Router();
const {
cohort: { getSpecificCohort },
} = require('./routes/user');

router.get('/', (req, res) => {
res.send('<h1>CA WIKI</h1>');
});

ranasobeid95 marked this conversation as resolved.
Show resolved Hide resolved
router.get('/cohort/:cohortid', getSpecificCohort);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on issue #5 please change the route to be /cohorts/:cohortId


module.exports = router;
10 changes: 10 additions & 0 deletions server/controllers/routes/user/cohort/getSpecificCohort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {
cohort: { getSpecificCohort },
Copy link
Member

@MohammedAlghazali MohammedAlghazali Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest making destructure of getSpecificCohort in /database/queries/index as :

const { getSpecificCohort } = require('./cohort');

module.exports = { getSpecificCohort };

as you make in server/database/queries/cohort/index.js file,

and here in this file you can make require to getSpecificCohort as

Suggested change
cohort: { getSpecificCohort },
const { getSpecificCohort } = require('../../../../database/queries');

so we get more benefit from /database/queries/index file.

} = require('../../../../database/queries');

exports.getSpecificCohort = (req, res) => {
getSpecificCohort(req.params.cohortid)
.then(({ rows }) => res.json(rows))
// eslint-disable-next-line no-console
.catch((err) => console.error(err));
ranasobeid95 marked this conversation as resolved.
Show resolved Hide resolved
};
ranasobeid95 marked this conversation as resolved.
Show resolved Hide resolved
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 cohort = require('./cohort');

module.exports = { cohort };
2 changes: 1 addition & 1 deletion server/database/config/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ const dbBuild = () => {
return connection.query(sql);
};

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

exports.getSpecificCohort = (id) =>
connection.query('select * from cohort where id = $1', [`${id}`]);
ranasobeid95 marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions server/database/queries/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/database/queries/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cohort = require('./cohort');

module.exports = { cohort };
36 changes: 34 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
test('Initial test', () => {
expect(1).toBe(1);
const request = require('supertest');
const connection = require('../server/database/config/connection');
const { dbBuild } = require('../server/database/config/build');

const app = require('../server/app');

beforeAll(() => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need return here 😉

Suggested change
beforeAll(() => {
beforeAll(() => dbBuild());

return dbBuild();
});

afterAll(() => {
return connection.end();
});

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

test('Route /cohort/2 status 200, json header, res.body[0].name =G7', async () => {
const res = await request(app)
.get('/api/v1/cohort/2')
.expect(200)
.expect('Content-Type', /json/);
expect(res.body[0].name).toBe('G7');
});
});