Skip to content

Commit

Permalink
Merge pull request #43 from GSG-G8/29-DELETE-cohort
Browse files Browse the repository at this point in the history
handle Delete cohort route
Close #29
  • Loading branch information
Mu7ammadAbed authored Apr 1, 2020
2 parents b05e9a7 + d131a1e commit 33f0796
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 3 deletions.
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"
}
}
3 changes: 3 additions & 0 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const router = require('express').Router();
const admin = require('./routes/admin');

const { clientError, serverError } = require('./middlewares/errorHandle');

router.use(clientError);
router.use(serverError);

router.use(admin);

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

const deleteCohort = async (req, res, next) => {
try {
const { cohortId } = req.params;
const check = await deleteCohortQuery(cohortId);
if (check.rowCount !== 0) {
res.json({
StatusCode: 200,
data: { message: 'Cohort deleted successfully' },
});
} else {
res.status(404).json({
StatusCode: 404,
data: { message: 'Cohort does not exist' },
});
}
} catch (err) {
next(err);
}
};

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

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

router
.route('/cohorts/:cohortId')
.all((req, res, next) => {
// ToDo: make middleware to check authentication
next();
})
.get((req, res, next) => {
next(new Error('not implemented'));
})
.put((req, res, next) => {
next(new Error('not implemented'));
})
.post((req, res, next) => {
next(new Error('not implemented'));
})
.delete(deleteCohort);

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

const deleteCohortQuery = (cohortId) =>
connection.query('DELETE FROM cohort WHERE id = $1', [cohortId]);

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

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

module.exports = {
deleteCohortQuery,
};
29 changes: 27 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
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(() => dbBuild());

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

describe('Admin, (/cohorts/:cohortId)', () => {
test('Route /cohorts/1 status 200, data.message = Cohort deleted successfully ', (done) => {
return request(app)
.delete('/api/v1/cohorts/1')
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
const { message } = res.body.data;
if (err) return done(err);
const { rows } = await connection.query(
'SELECT * from cohort WHERE id = 1',
);
expect(rows).toHaveLength(0);
expect(message).toBe('Cohort deleted successfully');
done();
});
});
});

0 comments on commit 33f0796

Please sign in to comment.