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 12 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
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error",
"jest/prefer-to-have-length": "warn",
"jest/valid-expect": "error"
"jest/valid-expect": "error",
"consistent-return":"off",
"jest/no-test-callback":"off"
}
}
7 changes: 4 additions & 3 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const router = require('express').Router();
const {
cohort: { getSpecificCohort },
} = require('./routes/user');

router.get('/', (req, res) => {
res.send('<h1>CA WIKI</h1>');
});
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;
21 changes: 21 additions & 0 deletions server/controllers/routes/user/cohort/getSpecificCohort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 = async (req, res, next) => {

Choose a reason for hiding this comment

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

@Mu7ammadAbed Since you don't use user in the actual api route, it might make more sense to put these files at server/controllers/routes/cohort in the repo instead of server/controllers/routes/user/cohort. That might make it easier for other developers to find them in the future (and also means you don't have to write as many ../../.. 😅

try {
const { cohortid } = req.params;
const { rows } = await getSpecificCohort(cohortid);
const data = { ...rows[0] };
if (data.id) {
res.json({ statusCode: 200, data });
} else {
res.status(404).json({
ranasobeid95 marked this conversation as resolved.
Show resolved Hide resolved
statusCode: 404,
message: "Sorry There's no cohort for this id",
});
}
} catch (err) {
next(err);
}
};
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 };
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]);
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 };
42 changes: 40 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
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, data.name =G8 ', (done) => {
return request(app)
.get('/api/v1/cohort/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 /cohort/10 status 404, json header, data.message = "Sorry There\'s no cohort for this id" ', (done) => {
return request(app)
.get('/api/v1/cohort/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();
});
});
});