Skip to content

Commit

Permalink
update master 2
Browse files Browse the repository at this point in the history
relates #26
  • Loading branch information
MohammedAlghazali committed Apr 2, 2020
2 parents 9a17e7a + e4bac90 commit a09740d
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 219 deletions.
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;
4 changes: 3 additions & 1 deletion server/controllers/routes/admin/cohort/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const editCohort = require('./editCohort');
const addCohort = require('./addCohort');
const deleteCohort = require('./deleteCohort');
const editCohort = require('./editCohort');

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

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

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

router
.route('/cohorts/:cohortId')
.all((req, res, next) => {
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions server/controllers/routes/user/project/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { getProjectById } = require('./getProjectById');

module.exports = { getProjectById };
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
2 changes: 2 additions & 0 deletions server/database/queries/cohort/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const postCohort = require('./postCohort');
const putSpecificCohort = require('./putSpecificCohort');
const getCohorts = require('./getCohorts');
const getCohortQuery = require('./getSpecificCohort');
const deleteCohortQuery = require('./deleteCohort');

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

const postCohort = (reqData) => {
const { name, description, imgUrl, githubLink } = reqData;
return connection.query(
'INSERT INTO cohort (name, description, img_url, github_link) VALUES ($1, $2, $3, $4) RETURNING *;',
[name, description, imgUrl, githubLink],
);
};

module.exports = postCohort;
6 changes: 4 additions & 2 deletions server/database/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { getAlumniQuery, deleteStudentQuery } = require('./student');

const {
getCohorts,
getCohortQuery,
postCohort,
deleteCohortQuery,
getCohortQuery,
getCohorts,
putSpecificCohort,
} = require('./cohort');
const {
Expand All @@ -13,6 +14,7 @@ const {
} = require('./project');

module.exports = {
postCohort,
getCohorts,
deleteCohortQuery,
putSpecificCohort,
Expand Down
10 changes: 10 additions & 0 deletions server/validation/cohortSchema .js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const yup = require('yup');

const cohortSchema = yup.object({
name: yup.string().required(),
description: yup.string().required(),
imgUrl: yup.string().url().required(),
githubLink: yup.string().url().required(),
});

module.exports = cohortSchema;
3 changes: 2 additions & 1 deletion server/validation/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const cohortSchema = require('./cohortSchema ');
const { editCohortSchema } = require('./editCohort');

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

module.exports = {
cohortSchema,
projectSchema,
editCohortSchema,
};
Loading

0 comments on commit a09740d

Please sign in to comment.