/**
*  Copyright 2014 IBM
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/

var topologyPoolModel = require('../models/poolmodel');
var Topology = require('../models/topologymodel');
var minstance= require('../models/instancemodel');



exports.findAllView = function(req, res) {
   console.log('req.params.id:'+req.params.id);
   minstance.find({poolRef:req.params.id}, function(err, instances) {
   if (err) {
     console.log ( 'error get instances associated with pool');
     console.err(err);
     return;
   }   
   res.render('topology/instances/instanceindex', {
      title: 'Associated Instances',
      docs: instances
    });
  });
};

exports.findAll = function(req, res) {
   console.log('req.params.id:'+req.params.id);
   minstance.find({poolRef:req.params.id}, function(err, instances) {
   if (err) {
     console.log ( 'error get instances associated with pool');
     console.err(err);
     res.send(404);
     return;
   }   
   res.send(instances);
  });
};

exports.find = function(req, res) {
   console.log('req.params.id:'+req.params.id);
   minstance.findbyId(req.params.id, function(err, instance) {
   if (err) {
     console.log ( 'error get instance');
     console.err(err);
     res.send(404);
     return;
   }   
   res.send(instance);
  });
};



exports.deleteView = function(req, res) {
   console.log('req.params.id:'+req.params.id);
   minstance.findById(req.params.id, function(err, instance) {
   if (err) {
     console.log ( 'error get instance to remove');
     console.err(err);
     return;
   }   
   res.render('topology/instances/instanceindex', {
      title: 'Associated Instances',
      docs: instances
    });
  });
};

exports.find = function(req, res) {
	console.log('instance.find: finding ' + req.params.id);
	topologyPool.findById(req.params.id, function(err, doc) {
		console.log('printing doc');
		console.log(doc);
		console.log('printing error');
		console.log(err);
		if (! doc) {
			res.send(404);
		}else {
			res.send(doc);
		}
	});
};


@@ -2189,334 +2189,4 @@
 %7D;%0A%0A
-exports.find = function(req, res) %7B%0A%09console.log('instance.find: finding ' + req.params.id);%0A%09topologyPool.findById(req.params.id, function(err, doc) %7B%0A%09%09console.log('printing doc');%0A%09%09console.log(doc);%0A%09%09console.log('printing error');%0A%09%09console.log(err);%0A%09%09if (! doc) %7B%0A%09%09%09res.send(404);%0A%09%09%7Delse %7B%0A%09%09%09res.send(doc);%0A%09%09%7D%0A%09%7D);%0A%7D;%0A%0A

update instance api