perspective/perspective

View on GitHub
lib/server/users.js

Summary

Maintainability
A
1 hr
Test Coverage
var EventsRepo = require('./events-repo');
var logger = require('./logger');

var Users = function(core) {
  this.core = core;
  this.eventsRepo = new EventsRepo(core.db);
};

Users.prototype = {
  onRouterInit: function(router) {
    var repo = this;

    router.get('/api/users', function *(next) {
      var result = yield repo.eventsRepo.getUsers();
      this.body = result;
      yield next;
    });

    router.post('/api/users', this.core.middleware.body, function *(next) {
      if(this.request.type === 'application/json') {

        this.checkBody('name').notEmpty().match(/[A-Za-z0-9 ]+/, 'Invalid name!');
        if(this.errors) {
          this.response.status = 400;
          this.body = this.errors;
          return;
        }

        var body = this.request.body;

        try {
          this.body = yield repo.eventsRepo.insert({
            name: body.name
          });
          this.response.status = 201;
        } catch (e) {
          this.response.status = 500;
        }

      } else {
        this.response.status = 415;
        yield next;
      }
    });
  }
};

module.exports = Users;