/**
 * Using Rails-like standard naming convention for endpoints.
 * GET     /api/bridges              ->  index
 * POST    /api/bridges              ->  create
 * GET     /api/bridges/:id          ->  show
 * PUT     /api/bridges/:id          ->  upsert
 * PATCH   /api/bridges/:id          ->  patch
 * DELETE  /api/bridges/:id          ->  destroy
 */

'use strict';

import jsonpatch from 'fast-json-patch';
import Bridge from './bridge.model';

function respondWithResult(res, statusCode) {
  statusCode = statusCode || 200;
  return function(entity) {
    if (entity) {
      return res.status(statusCode).json(entity);
    }
    return null;
  };
}

function patchUpdates(patches) {
  return function(entity) {
    try {
      // eslint-disable-next-line prefer-reflect
      jsonpatch.apply(entity, patches, /*validate*/ true);
    } catch(err) {
      return Promise.reject(err);
    }

    return entity.save();
  };
}

function removeEntity(res) {
  return function(entity) {
    if (entity) {
      return entity.remove()
        .then(() => {
          res.status(204).end();
        });
    }
  };
}

function handleEntityNotFound(res) {
  return function(entity) {
    if (!entity) {
      res.status(404).end();
      return null;
    }
    return entity;
  };
}

function handleError(res, statusCode) {
  statusCode = statusCode || 500;
  return function(err) {
    res.status(statusCode).send(err);
  };
}

// Gets a list of Bridges
export function index(req, res) {
  return Bridge.find().exec()
    .then(respondWithResult(res))
    .catch(handleError(res));
}

// Gets a single Bridge from the DB
export function show(req, res) {
  return Bridge.findById(req.params.id).exec()
    .then(handleEntityNotFound(res))
    .then(respondWithResult(res))
    .catch(handleError(res));
}

// Creates a new Bridge in the DB
export function create(req, res) {
  return Bridge.create(req.body)
    .then(respondWithResult(res, 201))
    .catch(handleError(res));
}

// Upserts the given Bridge in the DB at the specified ID
export function upsert(req, res) {
  if (req.body._id) {
    Reflect.deleteProperty(req.body, '_id');
  }
  return Bridge.findOneAndUpdate({_id: req.params.id}, req.body, {new: true, upsert: true, setDefaultsOnInsert: true, runValidators: true}).exec()

    .then(respondWithResult(res))
    .catch(handleError(res));
}

// Updates an existing Bridge in the DB
export function patch(req, res) {
  if (req.body._id) 

  return Bridge.findById(req.params.id).exec()
    .then(handleEntityNotFound(res))
    .then(patchUpdates(req.body))
    .then(respondWithResult(res))
    .catch(handleError(res));
}

// Deletes a Bridge from the DB
export function destroy(req, res) {
  return Bridge.findById(req.params.id).exec()
    .then(handleEntityNotFound(res))
    .then(removeEntity(res))
    .catch(handleError(res));
}

{
    Reflect.deleteProperty(req.body, '_id');
  }