Skip to content

Commit

Permalink
modfiy editProject function and schema
Browse files Browse the repository at this point in the history
realtes #32
  • Loading branch information
MohammedAlghazali committed Apr 1, 2020
2 parents cfa2d8a + 213035a commit 2cbe289
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 105 deletions.
6 changes: 3 additions & 3 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const router = require('express').Router();
const admin = require('./routes/admin');

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

router.use(serverError);

router.use(admin);

Expand Down
7 changes: 7 additions & 0 deletions server/controllers/middlewares/errorHandle/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable no-unused-vars */

exports.serverError = (err, req, res, next) => {
res
.status(500)
.json({ StatusCode: '500', title: 'internal server error 500', err });
};
5 changes: 5 additions & 0 deletions server/controllers/middlewares/errorHandle/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { serverError } = require('./error');

module.exports = {
serverError,
};
10 changes: 6 additions & 4 deletions server/controllers/routes/admin/cohort/deleteCohort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ const { deleteCohortQuery } = require('../../../../database/queries');

const deleteCohort = async (req, res, next) => {
try {
const check = await deleteCohortQuery(req.params.cohortId);
const { cohortId } = req.params;
const check = await deleteCohortQuery(cohortId);
if (check.rowCount !== 0) {
res.json({
StatusCode: 200,
data: { message: 'Cohort deleted successfully' },
});
} else {
const err = new Error();
err.message = 'cohort id does not exist';
next(err);
res.status(404).json({
StatusCode: 404,
data: { message: 'Cohort does not exist' },
});
}
} catch (err) {
next(err);
Expand Down
19 changes: 13 additions & 6 deletions server/controllers/routes/admin/project/addProject.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
const { addProjectQuery } = require('../../../../database/queries');
const { addProjectSchema } = require('../../../../validation');
const { projectSchema } = require('../../../../validation');

const addProject = async (req, res, next) => {
try {
await addProjectSchema.validate(req.body);
await projectSchema.validate(req.body, { abortEarly: false });
await addProjectQuery(req.body);
res.json({
StatusCode: 200,
data: { message: 'Cohort Added successfully' },
res.status(201).json({
StatusCode: 201,
data: { message: 'Project Added successfully' },
});
} catch (err) {
next(err);
if (err.errors) {
res.json({
StatusCode: 400,
data: { message: err.errors },
});
} else {
next(err);
}
}
};

Expand Down
17 changes: 13 additions & 4 deletions server/controllers/routes/admin/project/editProject.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
const { editProjectQuery } = require('../../../../database/queries');
const { editProjectSchema } = require('../../../../validation');
const { projectSchema } = require('../../../../validation');

const editProject = async (req, res, next) => {
try {
req.body.projectId = req.params.projectId;
await editProjectSchema.validate(req.body);
await projectSchema.validate(
{ ...req.body, projectId: req.params.projectId },
{ abortEarly: false },
);
await editProjectQuery(req.body);
res.json({
StatusCode: 200,
data: { message: 'project updated successfully' },
});
} catch (err) {
next(err);
if (err.errors) {
res.json({
StatusCode: 400,
data: { message: err.errors },
});
} else {
next(err);
}
}
};

Expand Down
6 changes: 2 additions & 4 deletions server/validation/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const addProjectSchema = require('./validateAddProject');
const editProjectSchema = require('./validateEditProject');
const projectSchema = require('./projectSchema');

module.exports = {
addProjectSchema,
editProjectSchema,
projectSchema,
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const yup = require('yup');

const addProjectSchema = yup.object({
const projectSchema = yup.object({
name: yup.string().required(),
description: yup.string().required(),
imgUrl: yup.string().url().required(),
githubLink: yup.string().url().required(),
websiteLink: yup.string().url().required(),
projectType: yup.string().required(),
projectType: yup.mixed().oneOf(['internal', 'remotely']),
cohortId: yup.number().integer().positive().required(),
projectId: yup.number().integer().positive(),
});

module.exports = addProjectSchema;
module.exports = projectSchema;
14 changes: 0 additions & 14 deletions server/validation/validateEditProject.js

This file was deleted.

137 changes: 70 additions & 67 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,80 @@ const dbBuild = require('../server/database/config/build');

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

beforeEach(() => {
return dbBuild();
});
beforeAll(() => dbBuild());

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

test('PUT Route /projects/1 status 200, json header, message:Cohort updated successfully', (done) => {
const testData = {
name: 'Mooooot',
description: 'project test',
imgUrl: 'https://github.com/GSG-G1',
githubLink: 'https://github.com/GSG-G1',
websiteLink: 'https://github.com/GSG-G1',
projectType: 'https://github.com/GSG-G1',
cohortId: '2',
};
return request(app)
.put('/api/v1/projects/5')
.send(testData)
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const { message } = res.body.data;
const result = await connection.query(
'SELECT * from project WHERE id = 5',
);
expect(message).toBe('project updated successfully');
expect(result.rows[0].name).toBe('Mooooot');
done();
});
describe('Admin, Project', () => {
test('Route /projects status 200, json header, data.message = Cohort Added successfully ', (done) => {
const reqData = {
name: 'Mohmmedzw851@',
description: 'description',
imgUrl: 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4',
githubLink: 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4',
websiteLink:
'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4',
projectType: 'internal',
cohortId: '1',
};
return request(app)
.post('/api/v1/projects')
.send(reqData)
.expect(201)
.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 project WHERE id = 6',
);
expect(rows[0].name).toBe('Mohmmedzw851@');
expect(message).toBe('Project Added successfully');
done();
});
});
});

test('Route /projects status 200, json header, data.message = Cohort Added successfully ', (done) => {
const reqData = {
name: 'Mohmmedzw851@',
description: 'description',
imgUrl: 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4',
githubLink: 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4',
websiteLink: 'https://avatars3.githubusercontent.com/u/52123464?s=200&v=4',
projectType: 'internal',
cohortId: '1',
};
return request(app)
.post('/api/v1/projects')
.send(reqData)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
const { message } = res.body.data;
if (err) return done(err);
expect(message).toBe('Cohort Added successfully');
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();
});
});
});

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, (/projects/:projectId)', () => {
test('PUT Route /projects/1 status 200, json header, message:Cohort updated successfully', (done) => {
const testData = {
name: 'Mooooot',
description: 'project test',
imgUrl: 'https://github.com/GSG-G1',
githubLink: 'https://github.com/GSG-G1',
websiteLink: 'https://github.com/GSG-G1',
projectType: 'remotely',
cohortId: '2',
};
return request(app)
.put('/api/v1/projects/5')
.send(testData)
.expect(200)
.expect('Content-Type', /json/)
.end(async (err, res) => {
if (err) return done(err);
const { message } = res.body.data;
expect(message).toBe('project updated successfully');
done();
});
});
});

0 comments on commit 2cbe289

Please sign in to comment.