This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'staging' of https://github.com/HackIllinois/api-2017 in…
…to feature/testing
- Loading branch information
Showing
37 changed files
with
1,107 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,12 @@ npm-debug.log* | |
pids | ||
*.pid | ||
*.seed | ||
dump.rdb | ||
|
||
node_modules | ||
.npm | ||
|
||
.idea | ||
.DS_Store | ||
temp | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var middleware = require('../middleware'); | ||
|
||
var router = require('express').Router(); | ||
|
||
function healthCheck(req, res, next) { | ||
next(); | ||
return null; | ||
} | ||
|
||
router.get('', healthCheck); | ||
router.use(middleware.response); | ||
router.use(middleware.errors); | ||
|
||
module.exports.router = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
var _ = require('lodash'); | ||
var bodyParser = require('body-parser'); | ||
var middleware = require('../middleware'); | ||
var router = require('express').Router(); | ||
var _Promise = require('bluebird'); | ||
|
||
var errors = require('../errors'); | ||
var config = require('../../config'); | ||
var requests = require('../requests'); | ||
var roles = require('../utils/roles'); | ||
|
||
var ProjectService = require('../services/ProjectService'); | ||
var PermissionService = require('../services/PermissionService'); | ||
|
||
|
||
function _validGetAllRequest(page, count, published){ | ||
if(_.isNaN(page)){ | ||
var message = "Invalid page parameter"; | ||
var source = "page"; | ||
return _Promise.reject(new errors.InvalidParameterError(message, source)); | ||
} | ||
if(_.isNaN(count)){ | ||
var message = "Invalid count parameter"; | ||
var source = "count"; | ||
return _Promise.reject(new errors.InvalidParameterError(message, source)); | ||
} | ||
if(_.isNaN(published) || (published != 0 && published != 1)){ | ||
var message = "Invalid published parameter"; | ||
var source = "published"; | ||
return _Promise.reject(new errors.InvalidParameterError(message, source)); | ||
} | ||
return _Promise.resolve(true); | ||
} | ||
|
||
function createProject (req, res, next) { | ||
attributes = req.body; | ||
|
||
PermissionService | ||
.canCreateProject(req.user) | ||
.then(function (isAuthed) { | ||
return ProjectService.createProject(attributes); | ||
}) | ||
.then(function (newProject) { | ||
res.body = newProject.toJSON(); | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch(function (error){ | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
function getProject (req, res, next) { | ||
var id = req.params.id; | ||
|
||
ProjectService | ||
.findProjectById(id) | ||
.then(function (project) { | ||
res.body = project.toJSON(); | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch(function (error) { | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
function getAllProjects (req, res, next) { | ||
_.defaults(req.params, {'page': 1}); | ||
_.defaults(req.query, {'count': 10, 'published': 1}); | ||
var page = parseInt(req.params.page); | ||
var count = parseInt(req.query.count); | ||
var published = parseInt(req.query.published); | ||
|
||
_validGetAllRequest(page, count, published) | ||
.then(function () { | ||
return ProjectService.getAllProjects(page, count, published); | ||
}) | ||
.then(function (results) { | ||
res.body = {}; | ||
res.body.projects = results; | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch(function (error) { | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
function updateProject (req, res, next) { | ||
var id = req.params.id; | ||
var attributes = req.body; | ||
|
||
ProjectService | ||
.findProjectById(id) | ||
.then(function (project) { | ||
return ProjectService.updateProject(project, attributes); | ||
}) | ||
.then(function (project) { | ||
res.body = project.toJSON(); | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch(function (error) { | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
function addProjectMentor (req, res, next) { | ||
var project_id = req.body.project_id; | ||
var mentor_id = req.body.mentor_id; | ||
|
||
ProjectService | ||
.addProjectMentor(project_id, mentor_id) | ||
.then(function (projectMentor) { | ||
res.body = projectMentor.toJSON(); | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch( function (error) { | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
function deleteProjectMentor (req, res, next) { | ||
var project_id = req.body.project_id; | ||
var mentor_id = req.body.mentor_id; | ||
|
||
ProjectService | ||
.deleteProjectMentor(project_id, mentor_id) | ||
.then(function () { | ||
res.body = {} | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch( function (error) { | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
router.use(bodyParser.json()); | ||
router.use(middleware.auth); | ||
|
||
router.post('/mentor', middleware.request(requests.ProjectMentorRequest), | ||
middleware.permission(roles.ORGANIZERS), addProjectMentor); | ||
router.delete('/mentor', middleware.request(requests.ProjectMentorRequest), | ||
middleware.permission(roles.ORGANIZERS), deleteProjectMentor); | ||
router.post('/', middleware.request(requests.ProjectRequest), | ||
middleware.permission(roles.ORGANIZERS), createProject); | ||
router.get('/:id', middleware.permission(roles.ALL), getProject); | ||
router.put('/:id', middleware.request(requests.ProjectRequest), | ||
middleware.permission(roles.ORGANIZERS), updateProject); | ||
router.get('/all/:page', middleware.permission(roles.ALL), getAllProjects); | ||
|
||
router.use(middleware.response); | ||
router.use(middleware.errors); | ||
|
||
module.exports.router = router; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
var bodyParser = require('body-parser'); | ||
|
||
var services = require('../services'); | ||
var middleware = require('../middleware'); | ||
var requests = require('../requests'); | ||
var roles = require('../utils/roles'); | ||
|
||
var router = require('express').Router(); | ||
|
||
function _isAuthenticated (req) { | ||
return req.auth && (req.user !== undefined); | ||
} | ||
|
||
function createMentor(req, res, next) { | ||
delete req.body.status; | ||
|
||
services.RegistrationService.createMentor(req.user, req.body) | ||
.then(function (mentor) { | ||
res.body = mentor.toJSON(); | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch(function (error) { | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
function fetchMentorByUser(req, res, next) { | ||
services.RegistrationService | ||
.findMentorByUser(req.user) | ||
.then(function(mentor){ | ||
res.body = mentor.toJSON(); | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch(function (error) { | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
function fetchMentorById(req, res, next) { | ||
services.RegistrationService.findMentorById(req.params.id) | ||
.then(function(mentor){ | ||
res.body = mentor.toJSON(); | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch(function (error) { | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
function updateMentorByUser(req, res, next) { | ||
if (!req.user.hasRoles(roles.ORGANIZERS)) { | ||
delete req.body.status; | ||
} | ||
|
||
services.RegistrationService | ||
.findMentorByUser(req.user) | ||
.then(function (mentor) { | ||
return services.RegistrationService.updateMentor(mentor, req.body); | ||
}) | ||
.then(function(mentor){ | ||
res.body = mentor.toJSON(); | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch(function (error) { | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
function updateMentorById(req, res, next) { | ||
if (!req.user.hasRoles(roles.ORGANIZERS)) { | ||
delete req.body.status; | ||
} | ||
|
||
services.RegistrationService | ||
.findMentorById(req.params.id) | ||
.then (function (mentor) { | ||
return services.RegistrationService.updateMentor(mentor, req.body); | ||
}) | ||
.then(function (mentor) { | ||
res.body = mentor.toJSON(); | ||
|
||
next(); | ||
return null; | ||
}) | ||
.catch(function (error) { | ||
next(error); | ||
return null; | ||
}); | ||
} | ||
|
||
router.use(bodyParser.json()); | ||
router.use(middleware.auth); | ||
router.use(middleware.request); | ||
|
||
router.post('/mentor', middleware.request(requests.MentorRequest), | ||
middleware.permission(roles.NONE, _isAuthenticated), createMentor); | ||
router.get('/mentor', middleware.permission(roles.MENTOR), fetchMentorByUser); | ||
router.get('/mentor/:id', middleware.permission(roles.ORGANIZERS), fetchMentorById); | ||
router.put('/mentor', middleware.request(requests.MentorRequest), | ||
middleware.permission(roles.MENTOR), updateMentorByUser); | ||
router.put('/mentor/:id', middleware.request(requests.MentorRequest), | ||
middleware.permission(roles.ORGANIZERS), updateMentorById); | ||
|
||
router.use(middleware.response); | ||
router.use(middleware.errors); | ||
|
||
module.exports.router = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.