Skip to content
This repository has been archived by the owner on Jun 21, 2019. It is now read-only.

Commit

Permalink
Merge branch 'staging' of https://github.com/HackIllinois/api-2017 in…
Browse files Browse the repository at this point in the history
…to feature/testing
  • Loading branch information
michaelcolomb committed Dec 2, 2016
2 parents 75637f6 + 96915cc commit 7219029
Show file tree
Hide file tree
Showing 37 changed files with 1,107 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ npm-debug.log*
pids
*.pid
*.seed
dump.rdb

node_modules
.npm

.idea
.DS_Store
temp

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/HackIllinois/api-2017?utm_source=badge&utm_medium=badge)

# HackIllinois API (2017)

The back-end services supporting HackIllinois 2017 are stored here. Looking to
Expand Down
1 change: 1 addition & 0 deletions api/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function DatabaseManager() {
this._knex = Knex(KNEX_CONFIG);

this._bookshelf = Bookshelf(this._knex);
this._bookshelf.plugin('pagination')
}

DatabaseManager.prototype.constructor = DatabaseManager;
Expand Down
6 changes: 3 additions & 3 deletions api/v1/controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var _Promise = require('bluebird');
var config = require('../../config');
var errors = require('../errors');
var middleware = require('../middleware');
var requests = require('../requests');
var utils = require('../utils');

var AuthService = require('../services/AuthService');
Expand Down Expand Up @@ -101,11 +102,10 @@ function passwordReset(req, res, next) {
}
router.use(bodyParser.json());
router.use(middleware.auth);
router.use(middleware.request);

router.post('', createToken);
router.post('', middleware.request(requests.BasicAuthRequest), createToken);
router.get('/refresh', refreshToken);
router.post('/reset', passwordReset);
router.post('/reset', middleware.request(requests.ResetPasswordRequest), passwordReset);

router.use(middleware.response);
router.use(middleware.errors);
Expand Down
14 changes: 14 additions & 0 deletions api/v1/controllers/HealthController.js
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;
171 changes: 171 additions & 0 deletions api/v1/controllers/ProjectController.js
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;

119 changes: 119 additions & 0 deletions api/v1/controllers/RegistrationController.js
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;
8 changes: 5 additions & 3 deletions api/v1/controllers/UploadController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var services = require('../services');
var utils = require('../utils');

var Upload = require('../models/Upload');
var UploadRequest = require('../requests/UploadRequest');
var User = require('../models/User');

const UPLOAD_ALREADY_PRESENT = "An upload has already been associated with this user";
Expand Down Expand Up @@ -114,10 +115,11 @@ function getUpload (req, res, next) {
// note that the request middleware is added after the body-parser, else there will be no body
var resumeRouter = ExpressRouter();
resumeRouter.use(bodyParser.raw({ limit: RESUME_UPLOAD_LIMIT, type: RESUME_UPLOAD_TYPE }));
resumeRouter.use(middleware.request);

resumeRouter.post('/', middleware.upload, middleware.permission(utils.roles.NON_PROFESSIONALS), createResumeUpload);
resumeRouter.put('/:id', middleware.upload, _findUpload, middleware.permission(utils.roles.NONE, _isOwner), replaceResumeUpload);
resumeRouter.post('/', middleware.request(UploadRequest), middleware.upload,
middleware.permission(utils.roles.NON_PROFESSIONALS), createResumeUpload);
resumeRouter.put('/:id', middleware.request(UploadRequest), middleware.upload,
_findUpload, middleware.permission(utils.roles.NONE, _isOwner), replaceResumeUpload);
resumeRouter.get('/:id', _findUpload, middleware.permission(utils.roles.ORGANIZERS, _isOwner), getUpload);

// set up the primary router with just the auth middleware since the sub-routers
Expand Down
Loading

0 comments on commit 7219029

Please sign in to comment.