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

23 put cohortid #49

Merged
merged 18 commits into from
Apr 2, 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
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,

Choose a reason for hiding this comment

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

@Mu7ammadAbed You could remove statusCode from the json body because you already called status(404) on the response.

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

Choose a reason for hiding this comment

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

@Mu7ammadAbed Here also you could remove statusCode from the json body.

} 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 } = 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 ',

Choose a reason for hiding this comment

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

@Mu7ammadAbed My comment here might be helpful your endpoint - you can design PUT endpoints to accept any number of arguments, instead of always expecting every argument to be provided by the frontend.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can you provide me with a resource for it ?
I saw what you send to us yesterday and I didn't really get it, I tried so much and searched a lot without reaching any place.

Choose a reason for hiding this comment

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

Here's an example of the query syntax: https://stackoverflow.com/a/19358694
You can use a question mark in the query and then pass an object in the arguments array to match with the question mark. In your code, I think that means it could look like this:
return connection.query('UPDATE cohort SET ? WHERE id = ?', [req, req.cohortId]);

Once you do that, you should be able PUT any number / combination of the arguments (for example, only name or only img_url or both name and description together)

Hopefully that works - and if not, sorry for sending you on the wrong path. This change isn't incredibly important for this project, but maybe it would help for a future project.

[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 } = require('./project');
module.exports = {
getCohorts,
deleteCohortQuery,
putSpecificCohort,
getCohortQuery,
addProjectQuery,
};
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