Skip to content

Commit

Permalink
Merge pull request #49 from GSG-G8/23-put-cohortid
Browse files Browse the repository at this point in the history
23 put cohortid
  • Loading branch information
MohammedAlghazali authored Apr 2, 2020
2 parents 9c23fed + ff5e871 commit 5b3a966
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 12 deletions.
2 changes: 1 addition & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const { serverError } = require('./controllers/middlewares/errorHandle');

const app = express();

app.use(compression());
app.disable('x-powered-by');
app.set('port', process.env.PORT || 5000);

app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(compression());
app.use('/api/v1/', controller);

app.use(express.static(join(__dirname, '..', 'client', 'build')));
Expand Down
2 changes: 0 additions & 2 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const router = require('express').Router();
const user = require('./routes/user');
const admin = require('./routes/admin');
const { getSpecificCohort } = require('./routes/user/cohort');

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

Expand Down
31 changes: 31 additions & 0 deletions server/controllers/routes/admin/cohort/editCohort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { putSpecificCohort } = require('../../../../database/queries');
const { editCohortSchema } = require('../../../../validation/index');

const editCohort = async (req, res, next) => {
try {
const { cohortId } = req.params;
const data = await editCohortSchema.validate(
{ ...req.body, cohortId },
{
abortEarly: false,
},
);
const result = await putSpecificCohort(data);
if (result.rowCount === 1) {
res.json({ statusCode: 200, message: 'Changed Succefully' });
} else {
res.status(404).json({
statusCode: 404,
message: "Sorry There's no cohort for this id to change",
});
}
} catch (err) {
if (err.errors) {
res.status(400).json({ statusCode: 400, message: err.errors });
} else {
next(err);
}
}
};

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

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

const { editCohort } = require('./cohort');
const { deleteCohort } = require('./cohort');
const { addProject, editProject } = require('./project');

Expand All @@ -11,9 +13,7 @@ router
.get((req, res, next) => {
next(new Error('not implemented'));
})
.put((req, res, next) => {
next(new Error('not implemented'));
})
.put(editCohort)
.post((req, res, next) => {
next(new Error('not implemented'));
})
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/routes/user/cohort/getCohortsData.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ const getCohortsData = async (req, res, next) => {
next(err);
}
};
module.exports = { getCohortsData };
module.exports = getCohortsData;
7 changes: 5 additions & 2 deletions server/controllers/routes/user/cohort/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const { getCohortsData } = require('./getCohortsData');
const getCohortsData = require('./getCohortsData');
const getSpecificCohort = require('./getSpecificCohort');

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

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

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

module.exports = router;
4 changes: 2 additions & 2 deletions server/database/queries/cohort/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const putSpecificCohort = require('./putSpecificCohort');
const getCohorts = require('./getCohorts');

const getCohortQuery = require('./getSpecificCohort');

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

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

const putSpecificCohort = (req) => {
const { name, description, imgUrl, githubLink, cohortId } = req;
return connection.query(
'UPDATE cohort SET name = $1, description = $2, img_url = $3, github_link = $4 WHERE id = $5 ',
[name, description, imgUrl, githubLink, cohortId],
);
};

module.exports = putSpecificCohort;
2 changes: 2 additions & 0 deletions server/database/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { putSpecificCohort } = require('./cohort');
const { getCohorts } = require('./cohort');
const { getCohortQuery } = require('./cohort');
const { deleteCohortQuery } = require('./cohort');
Expand All @@ -6,6 +7,7 @@ const { addProjectQuery, editProjectQuery } = require('./project');
module.exports = {
getCohorts,
deleteCohortQuery,
putSpecificCohort,
getCohortQuery,
addProjectQuery,
editProjectQuery,
Expand Down
9 changes: 9 additions & 0 deletions server/validation/editCohort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const yup = require('yup');

exports.editCohortSchema = yup.object({
cohortId: yup.number().required(),
name: yup.string().required(),
description: yup.string().required(),
imgUrl: yup.string().url().required(),
githubLink: yup.string().url().required(),
});
3 changes: 3 additions & 0 deletions server/validation/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { editCohortSchema } = require('./editCohort');

const projectSchema = require('./projectSchema');

module.exports = {
projectSchema,
editCohortSchema,
};
94 changes: 94 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,100 @@ describe('Get Specific Cohort', () => {
});
});

describe('Post Cohort', () => {
const data = {
name: 'G1',
description: 'Code GazaSkyGeeksAcademy, 1st Cohort',
imgUrl: 'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4',
githubLink: 'https://github.com/GSG-G1',
};
const wrongData = {
name: 'G2',
description: 'Code GazaSkyGeeksAcademy, 2nd Cohort',
imgUrl: 'This is cohort Image',
githubLink: 'https://github.com/GSG-G1',
};

test('PUT Route /cohorts/1 status 200, json header, send data ', (done) => {
return request(app)
.put('/api/v1/cohorts/1')
.send(data)
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const { message } = res.body;
const { rows } = await connection.query(
'SELECT * from cohort WHERE id = 1',
);
expect(message).toBe('Changed Succefully');
expect(rows).toHaveLength(1);
expect(rows[0]).toEqual({
id: 1,
name: 'G1',
description: 'Code GazaSkyGeeksAcademy, 1st Cohort',
img_url:
'https://avatars0.githubusercontent.com/u/59821022?s=200&v=4',
github_link: 'https://github.com/GSG-G1',
});
done();
});
});

test('PUT Route /cohorts/4 status 404, json header, send data ', (done) => {
return request(app)
.put('/api/v1/cohorts/4')
.send(data)
.expect(404)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const { message } = res.body;
const { rows } = await connection.query(
'SELECT * from cohort WHERE id = 4',
);
expect(message).toBe("Sorry There's no cohort for this id to change");
expect(rows).toHaveLength(0);
done();
});
});

test('PUT Route /cohorts/1 status 400, json header, send wrong data and test the received message', (done) => {
return request(app)
.put('/api/v1/cohorts/1')
.send(wrongData)
.expect(400)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const { message } = res.body;
await connection.query('SELECT * from cohort WHERE id = 1');
expect(message[0]).toBe('imgUrl must be a valid URL');
done();
});
});
});

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();
});
});
});


describe('Admin, Post Project', () => {
test('Route /projects status 200, json header, data.message = Cohort Added successfully ', (done) => {
const reqData = {
Expand Down

0 comments on commit 5b3a966

Please sign in to comment.