Skip to content

Commit

Permalink
edit route
Browse files Browse the repository at this point in the history
relates #12
  • Loading branch information
rehabas committed Apr 2, 2020
2 parents c08501c + fb3a194 commit 0ee2fde
Show file tree
Hide file tree
Showing 39 changed files with 828 additions and 25 deletions.
7 changes: 7 additions & 0 deletions client/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"impliedStrict": true
}
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx"]
}
}
},
"root": true,
"env": {
"browser": true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dev": "NODE_ENV=development nodemon server/index.js",
"project-setup": "npm install && cd client && npm install",
"heroku-postbuild": "cd client && npm install && npm run build",
"test": "NODE_ENV=test jest --runInBand --coverage",
"test": "NODE_ENV=test jest --no-cache --runInBand --coverage",
"kill": "fuser -k 5000/tcp",
"lint": "eslint server/",
"lint-fix": "eslint server/ --fix",
Expand Down
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
3 changes: 1 addition & 2 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const router = require('express').Router();

const user = require('./routes/user');
const admin = require('./routes/admin');
const { getSpecificCohort } = require('./routes/user/cohort');

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

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

const addCohort = async (req, res, next) => {
try {
await cohortSchema.validate(req.body, { abortEarly: false });
const { rows } = await postCohort(req.body);
const { name } = rows[0];
res.status(201).json({
StatusCode: 201,
data: {
cohort: rows[0],
message: `Cohort with Key (name)=(${name}) added successfully`,
},
});
} catch (err) {
if (err.errors) {
res.status(400).json({ statusCode: 400, data: { message: err.errors } });
} else if (err.detail) {
res.status(409).json({
statusCode: 409,
data: {
message: err.detail,
},
});
} else {
next(err);
}
}
};

module.exports = addCohort;
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;
8 changes: 7 additions & 1 deletion server/controllers/routes/admin/cohort/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const addCohort = require('./addCohort');
const deleteCohort = require('./deleteCohort');
const editCohort = require('./editCohort');

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

const {
addProject,
editProject,
deleteProjectData,
getCohortProjects,
} = require('./project');

const { addCohort, deleteCohort, editCohort } = require('./cohort');
const { deleteStudent } = require('./student');

router.post('/cohorts', addCohort);

router
.route('/cohorts/:cohortId')
Expand All @@ -11,14 +21,16 @@ 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'));
})
.delete(deleteCohort);

router.put('/projects/:projectId', editProject);
router.post('/projects', addProject);
router.get('/cohorts/:cohortId/projects', getCohortProjects);
router.delete('/alumni/:studentId', deleteStudent);
router.delete('/projects/:projectId', deleteProjectData);

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

const deleteProjectData = async (req, res, next) => {
try {
const { projectId } = req.params;
if (projectId > 0) {
const check = await deleteProject(projectId);
if (check.rowCount !== 0) {
res.json({
StatusCode: 200,
data: { message: 'Project deleted successfully' },
});
} else {
res.status(404).json({
StatusCode: 404,
data: { message: 'Project does not exist' },
});
}
} else {
res.status(404).json({
StatusCode: 404,
data: { message: 'You enterd wrong project ID' },
});
}
} catch (err) {
next(err);
}
};

module.exports = deleteProjectData;
27 changes: 27 additions & 0 deletions server/controllers/routes/admin/project/editProject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { editProjectQuery } = require('../../../../database/queries');
const { projectSchema } = require('../../../../validation');

const editProject = async (req, res, next) => {
try {
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) {
if (err.errors) {
res.json({
StatusCode: 400,
data: { message: err.errors },
});
} else {
next(err);
}
}
};

module.exports = editProject;
28 changes: 28 additions & 0 deletions server/controllers/routes/admin/project/getCohortProjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { getCohortProjectsQuery } = require('../../../../database/queries');

const getCohortProjects = async (req, res, next) => {
try {
const { cohortId } = req.params;
const projectType = req.query.type;
if (
(cohortId > 0 && projectType.toLowerCase() === 'internal') ||
projectType.toLowerCase() === 'remotely'
) {
const { rows } = await getCohortProjectsQuery(cohortId, projectType);
if (rows.length > 0) {
res.json({ statusCode: 200, data: rows });
} else {
res.json({ statusCode: 200, message: 'No Data' });
}
} else {
res.status(404).json({
statusCode: 404,
message: 'Please check cohort ID you entered or project type',
});
}
} catch (err) {
next(err);
}
};

module.exports = getCohortProjects;
6 changes: 6 additions & 0 deletions server/controllers/routes/admin/project/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const addProject = require('./addProject');
const getCohortProjects = require('./getCohortProjects');
const editProject = require('./editProject');
const deleteProjectData = require('./deleteProjectData');

module.exports = {
addProject,
getCohortProjects,
editProject,
deleteProjectData,
};
30 changes: 30 additions & 0 deletions server/controllers/routes/admin/student/deleteStudent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { deleteStudentQuery } = require('../../../../database/queries');

const deleteStudent = async (req, res, next) => {
try {
const { studentId } = req.params;
if (studentId > 0) {
const check = await deleteStudentQuery(studentId);
if (check.rowCount !== 0) {
res.json({
StatusCode: 200,
data: { message: 'Student deleted successfully' },
});
} else {
res.status(404).json({
StatusCode: 404,
data: { message: 'Student does not exist' },
});
}
} else {
res.status(404).json({
StatusCode: 404,
data: { message: 'You enterd wrong student ID' },
});
}
} catch (err) {
next(err);
}
};

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

module.exports = {
deleteStudent,
};
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;
6 changes: 3 additions & 3 deletions server/controllers/routes/user/cohort/getSpecificCohort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const { getCohortQuery } = require('../../../../database/queries');

const getSpecificCohort = async (req, res, next) => {
try {
const { cohortid } = req.params;
if (cohortid > 0) {
const { rows } = await getCohortQuery(cohortid);
const { cohortId } = req.params;
if (cohortId > 0) {
const { rows } = await getCohortQuery(cohortId);
const data = { ...rows[0] };
if (data.id) {
res.json({ statusCode: 200, data });
Expand Down
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,
};
3 changes: 2 additions & 1 deletion server/controllers/routes/user/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const router = require('express').Router();
const { getProjectsData } = require('./project');
const { getCohortsData } = require('./cohort');
const { getCohortsData, getSpecificCohort } = require('./cohort');

router.get('/projects?type=', getProjectsData);
router.get('/cohorts', getCohortsData);
router.get('/cohorts/:cohortid', getSpecificCohort);

module.exports = router;
Empty file.
2 changes: 2 additions & 0 deletions server/controllers/routes/user/project/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { getProjectsData } = require('./getProjectsData');
const { getProjectById } = require('./getProjectById');

module.exports = {
getProjectById,
getProjectsData,
};
2 changes: 1 addition & 1 deletion server/database/config/build.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE admin (

CREATE TABLE cohort (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
name VARCHAR(100) NOT NULL UNIQUE,
description TEXT NOT NULL,
img_url TEXT NOT NULL,
github_link TEXT NOT NULL
Expand Down
28 changes: 27 additions & 1 deletion server/database/config/fakeData.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,36 @@ INSERT INTO project (name , description , img_url , github_link , website_link ,
'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA',
'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3',
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal',1),
('ca-wiki',
'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page',
'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA',
'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3',
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal',2),
('ca-wiki',
'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page',
'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA',
'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3',
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal',1),
('ca-wiki',
'Ca-wiki is a web application which allows clients to view all cohorts that have been enrolled in Code Academy. Clients can view all students who graduated from the academy so that they can view every student and his/her projects he/she participated in, his/her github page',
'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA',
'https://github.com/GSG-G8/ca-wiki/tree/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3',
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Internal',2),

('events-booker',
'This app to help GSG organization in organizing the registration for the events that they do. So When they announced for an event, it will be known who would like to attend and they can ensure if this person attended or not. By this way, the GSG can have information about the people who attended and they can use it in the future as they want.',
'https://d2slcw3kip6qmk.cloudfront.net/marketing/blog/2017Q2/[email protected]',
'https://github.com/GSG-G8/events-booker',
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Remotely project',2);
'https://github.com/GSG-G8/ca-wiki/blob/ed9f4cd9b5dc428f5420fe9a880a27e63f5f04d3/%5Blink%5D','Remotely',2),
('room-booker',
'Gaza sky Geeks (GSG) has many rooms and many staff members with the ability to book any room but there is no way to know who booked what',
'https://lh3.googleusercontent.com/proxy/fp_bF_rbMIKyCfgdgWodyuM9LGt3HwGgM8AMnQ4qxjftKcvEdmhngdaeA8F6xFgRDHVzezPLT6YZarpBcqnMD5WtAvUhKJXcWS7qvS6Bn3CllitLttt_uA',
'https://github.com/GSG-G8/room-booker',
'https://github.com/GSG-G8/room-booker','Remotely',1),
('Applicants System',
'An application to facilitate code academy application process for applicants and help them to complete their applications easily and on time, also help code academy team to track , filter and create applications.',
'https://d2slcw3kip6qmk.cloudfront.net/marketing/blog/2017Q2/[email protected]',
'https://github.com/GSG-G8/applicants-system',
'https://github.com/GSG-G8/applicants-system','Internal',2);

INSERT INTO student_project (student_id , project_id) VALUES (1 , 1) , (2 , 2);
Loading

0 comments on commit 0ee2fde

Please sign in to comment.