/**
 * PluginController
 *
 * @module      :: Controller
 * @description :: Server-side logic for managing plugins
 * @help        :: See http://links.sailsjs.org/docs/controllers
 */

module.exports = {

  /**
   * Overridden from default blueprint.
   *
   * POST /plugin -> PluginController.create
   */
  create: function (req, res) {
    var params = req.params.all();
    var user = req.session.user;

    // Check if the plugins exists. If so, just update versions. Otherwise, create and then
    // update versions.
    Plugin.findOne().where({
      packageName: params.packageName,
      user: user.id
    }).exec(function(findErr, plugin) {
      if (findErr) {
        return ErrorManager.handleError(findErr, res);
      }

      if (!plugin) {
        var pluginObj = {
          name: params.name,
          packageName: params.packageName,
          user: user
        };

        Plugin.create(pluginObj).exec(function(createErr, plugin) {
          if (createErr) {
            return ErrorManager.handleError(createErr, res);
          }

          // Update info and versions for the newly created plugin
          PluginService.updateVersionMetadata(plugin, function(updateMetadataErr, updatedPlugin) {
            if (updateMetadataErr) {
              if (updateMetadataErr.type == 'NPMREGNOTFOUND') { // not found on npm repo? destroy it.
                plugin.destroy(function(destroyErr) {
                  if (destroyErr) {
                    sails.log.error(destroyErr);
                  }
                  return ErrorManager.handleError(updateMetadataErr, res);
                });
              } else { // other errors
                return ErrorManager.handleError(updateMetadataErr, res);
              }
            } else {
              return res.ok(updatedPlugin);
            }
          });
        });
      } else {
        // Plugin exists, update info and versions
        PluginService.updateVersionMetadata(plugin, function(updateMetadataErr, updatedPlugin) {
          if (updateMetadataErr) {
            if (updateMetadataErr.type == 'NPMREGNOTFOUND') { // not found on npm repo? destroy it.
              plugin.destroy(function(destroyErr) {
                if (destroyErr) {
                  sails.log.error(destroyErr);
                }
                return ErrorManager.handleError(updateMetadataErr, res);
              });
            } else { // other errors
              return ErrorManager.handleError(updateMetadataErr, res);
            }
          } else {
            return res.ok(updatedPlugin);
          }
        });
      }
    });
  },

  /**
   * Overridden from default blueprint.
   *
   * PUT /plugin/:id -> PluginController.update
   */
  update: function(req, res) {
    var params = req.params.all();
    var user = req.session.user;

    Plugin.findOne().where({
      id: params.id
    }).exec(function(findErr, plugin) {
      if (findErr) {
        return ErrorManager.handleError(findErr, res);
      }

      if (plugin.user != user.id)
        return res.forbidden('You have no permissions to perform this action.');

      //only the name can be manually changed, all other data comes from npm
      plugin.name = params.name;

      PluginService.updateVersionMetadata(plugin, function(updateMetadataErr, updatedPlugin) {
        if (updateMetadataErr) {
          if (updateMetadataErr.type == 'NPMREGNOTFOUND') { // not found on npm repo? destroy it.
            plugin.destroy(function(destroyErr) {
              if (destroyErr) {
                sails.log.error(destroyErr);
              }
              return ErrorManager.handleError(updateMetadataErr, res);
            });
          } else { // other errors
            return ErrorManager.handleError(updateMetadataErr, res);
          }
        } else {
          return res.ok(updatedPlugin);
        }
      });
    });
  }
};
@@ -3878,11 +3878,712 @@
  %7D);%0A  %7D
+,%0A%0A  /**%0A   * Overridden from default blueprint.%0A   *%0A   * DELETE /plugin/:id -%3E PluginController.destroy%0A   */%0A  destroy: function(req, res) %7B%0A    var params = req.params.all();%0A    var user = req.session.user;%0A%0A    Plugin.findOne().where(%7B%0A      id: params.id%0A    %7D).exec(function(findErr, plugin) %7B%0A      if (findErr) %7B%0A        return ErrorManager.handleError(err, res);%0A      %7D%0A%0A      if (plugin.user != user.id)%0A        return res.forbidden('You have no permissions to perform this action.');%0A%0A      plugin.destroy(function(destroyErr) %7B%0A        if (destroyErr) %7B%0A          return ErrorManager.handleError(err, res);%0A        %7D%0A%0A        return res.ok('Successfully deleted');%0A      %7D)%0A    %7D);%0A  %7D,
 %0A%7D;

Add destroy method for plugins