<commit_before>import restify = require('restify')
import carpoolService = require('../services/carpool.svc')
import userCtrl = require('./create-user.ctrl');
import campusCtrl = require('./campus.ctrl');
import models = require('../models/models');
import errors = require('../errors/errors');

module CarpoolController {
  export interface OutputJSON {
    name: string;
    description: string;
    owner: userCtrl.OutputJSON;
    campus: any; // This should be of type CampusController.OutputJSON
    participants: Array<userCtrl.OutputJSON>;
    href: string;
  }

  export function toOutputJSON(carpool:models.Carpool):OutputJSON {
    return {
      'name': carpool.name,
      'description': carpool.description,
      'owner': userCtrl.toOutputJSON(carpool.owner),
      'campus': campusCtrl.toOutputJSON(carpool.campus),
      'participants': carpool.participants.map(userCtrl.toOutputJSON),
      'href': '/carpools/' + carpool.id
    };
  }
  export function createCarpool(
    req:restify.Request, res:restify.Response, next:restify.Next) {
      var carpoolName:string = req.body.name;
      var campusName:string = req.body.campus;
      var description:string = req.body.description;
      var owner:string = req.body.owner;

      carpoolService.createCarpool(
        carpoolName, campusName, description, owner)
        .then((carpool) => {
          res.send(201, toOutputJSON(carpool));
        })
        .catch(errors.CarpoolOwnerNotFoundException,
          errors.CampusNotFoundException, (err) => {
          next(new restify.NotAcceptableError(err.message));
        })
        .catch(errors.CarpoolExistException, (err) => {
          next(new restify.ConflictError(err.message));
        })
        .catch((err) => {
          next(new restify.InternalServerError(err.message));
        })
        .error((err) => {
          next(new restify.InternalServerError(err.message));
        })
        .then(next);
  }

  export function getCarpool(
    req:restify.Request, res:restify.Response, next:restify.Next) {
    var carpoolid = req.params.carpoolid;

    carpoolService.getCarpoolByID(carpoolid)
      .then((carpool) => {
        res.send(200, toOutputJSON(carpool));
      })
      .catch(errors.CarpoolNotFoundException, (err) => {
        next(new restify.NotFoundError(err.message));
      })
      .catch((err) => {
        next(new restify.InternalServerError(err.message));
      })
      .error((err) => {
        next(new restify.InternalServerError(err.message));
      })
      .then(next);
  }

  export function getCarpools(
    req:restify.Request, res:restify.Response, next:restify.Next) {
    function getCarpoolList() {
      return carpoolService.getCarpools(10);
    }

    getCarpoolList()
      .then((_carpools) => {
        res.send(200, _carpools.map(toOutputJSON));
        next();
      })
      .catch((err) => {
        next(new restify.InternalServerError(err.message))
      });
  }

  export function updateCarpool(req:restify.Request, res:restify.Response, next:restify.Next) {
    var carpoolID = req.params.carpoolID;

    //todo: translate href to campus id
    function getIDFromHref(href:string) : string {
      var hrefComponents = href.split('/');
      return hrefComponents[hrefComponents.length - 1];
    }

    //function update(requestBody) : CarpoolSvc.CarpoolUpdateModel {
    //  return {
    //    "name":""
    //  };
    //}

    //todo: take out id field if present

    carpoolService.updateCarpool(carpoolID, req.body)
      .then(() => {
        res.send(204);
        next();
      })
      .catch(errors.CarpoolNotFoundException, (err) => {
        next(new restify.NotFoundError(err.message));
      })
      .catch((err) => {
        next(new restify.InternalServerError(err.message));
      })
      .error((err) => {
        next(new restify.InternalServerError(err.message));
      })
      .then(next);
  }
}
<commit_msg>Constructing the CarpoolUpdateModel from the request body<commit_after>import restify = require('restify')
import carpoolService = require('../services/carpool.svc')
import userCtrl = require('./create-user.ctrl');
import campusCtrl = require('./campus.ctrl');
import models = require('../models/models');
import errors = require('../errors/errors');

module CarpoolController {
  export interface OutputJSON {
    name: string;
    description: string;
    owner: userCtrl.OutputJSON;
    campus: any; // This should be of type CampusController.OutputJSON
    participants: Array<userCtrl.OutputJSON>;
    href: string;
  }

  export function toOutputJSON(carpool:models.Carpool):OutputJSON {
    return {
      'name': carpool.name,
      'description': carpool.description,
      'owner': userCtrl.toOutputJSON(carpool.owner),
      'campus': campusCtrl.toOutputJSON(carpool.campus),
      'participants': carpool.participants.map(userCtrl.toOutputJSON),
      'href': '/carpools/' + carpool.id
    };
  }
  export function createCarpool(
    req:restify.Request, res:restify.Response, next:restify.Next) {
      var carpoolName:string = req.body.name;
      var campusName:string = req.body.campus;
      var description:string = req.body.description;
      var owner:string = req.body.owner;

      carpoolService.createCarpool(
        carpoolName, campusName, description, owner)
        .then((carpool) => {
          res.send(201, toOutputJSON(carpool));
        })
        .catch(errors.CarpoolOwnerNotFoundException,
          errors.CampusNotFoundException, (err) => {
          next(new restify.NotAcceptableError(err.message));
        })
        .catch(errors.CarpoolExistException, (err) => {
          next(new restify.ConflictError(err.message));
        })
        .catch((err) => {
          next(new restify.InternalServerError(err.message));
        })
        .error((err) => {
          next(new restify.InternalServerError(err.message));
        })
        .then(next);
  }

  export function getCarpool(
    req:restify.Request, res:restify.Response, next:restify.Next) {
    var carpoolid = req.params.carpoolid;

    carpoolService.getCarpoolByID(carpoolid)
      .then((carpool) => {
        res.send(200, toOutputJSON(carpool));
      })
      .catch(errors.CarpoolNotFoundException, (err) => {
        next(new restify.NotFoundError(err.message));
      })
      .catch((err) => {
        next(new restify.InternalServerError(err.message));
      })
      .error((err) => {
        next(new restify.InternalServerError(err.message));
      })
      .then(next);
  }

  export function getCarpools(
    req:restify.Request, res:restify.Response, next:restify.Next) {
    function getCarpoolList() {
      return carpoolService.getCarpools(10);
    }

    getCarpoolList()
      .then((_carpools) => {
        res.send(200, _carpools.map(toOutputJSON));
        next();
      })
      .catch((err) => {
        next(new restify.InternalServerError(err.message))
      });
  }

  export function updateCarpool(req:restify.Request, res:restify.Response, next:restify.Next) {
    var carpoolID = req.params.carpoolID;

    //todo: translate href to campus id
    function getIDFromHref(href:string) : string {
      var hrefComponents = href.split('/');
      return hrefComponents[hrefComponents.length - 1];
    }

    //function update(requestBody) : CarpoolSvc.CarpoolUpdateModel {
    //  return {
    //    "name":""
    //  };
    //}

    //todo: take out id field if present

    Promise.resolve()
      // Create the CarpoolUpdateModel from the request body
      .then(() => {
        if (req.body.name) {
          return {"name": req.body.name}
        } else {
          return {};
        }
      })
      .then((obj) => {
        if (req.body.description) {
          obj["description"] = req.body.description;
          return obj;
        } else {
          return obj;
        }
      })
      .then((obj) => {
        if (req.body.campus) {
          obj["campus"] = getIDFromHref(req.body.campus);
          return obj;
        } else {
          return obj;
        }
      })
      .then((carpolUpdate) => {
        return carpoolService.updateCarpool(carpoolID, carpolUpdate);
      })
      .then(() => {
        res.send(204);
        next();
      })
      .catch(errors.CarpoolNotFoundException, (err) => {
        next(new restify.NotFoundError(err.message));
      })
      .catch((err) => {
        next(new restify.InternalServerError(err.message));
      })
      .error((err) => {
        next(new restify.InternalServerError(err.message));
      })
      .then(next);
  }
}

{'fraction_non_alphanumeric': 0.1396843950905903, 'fraction_numerical': 0.0035067212156633548, 'mean_word_length': 2.599495161968868, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '904645', 'n_tokens_mistral': 2683, 'n_tokens_neox': 2618, 'n_words': 430}