var express = require('express'),
	http 	= require('http'),
	request = require('request'),
	port    = process.env.PORT || 3000,
    apiStart = "https://user:"
	apiKey  = 'PtDnqJYXMyc+KnBQWMpykJmKunkbJfv2fTqOgx21a6A',
    apiCode = "@api.datamarket.azure.com/Bing/Search/v1/Image?Query=%27"
	mongoose = require('mongoose'),
	bodyParser = require('body-parser');
	app  	= express();


var logSchema = new mongoose.Schema({
    log: {
        type: String
    	}
	},
    {
    	timestamps: true

    }
);

var Log = mongoose.model("Log", logSchema);

mongoose.connect('mongodb://dev:dev123@ds013320.mlab.com:13320/imglayer234')

app.use(bodyParser.urlencoded({extended:false}));


app.get('/',function (req,res) {
	var newLog = { log: 'just a test' };
     Log.create(newLog, function(err, log) {

     });
	res.send("Hello from the root route");
});



app.get('/search/:term',function(req,res) {
    
    
    var number = req.query.offset || 10;

        
	//sample request--this is not yet dynamic :term from request should take the place of xbos,also we should have dynamic limit and take less data from the json body of the request
    request(apiStart+apiKey+apiCode+ req.params.term + '%27&$top='+number+'&$format=JSON', function (error, response, body) {     
	   
	    if (!error && response.statusCode == 200) {
	    	var  parsedBody =  JSON.parse(body);
	    	var  queryImages = parsedBody.d.results
	    	var  output = {results:[]};
	    	queryImages.forEach(function(Image){
	    		output.results.push({

	    			'ID':Image.ID,
	    			'ImageURL':Image.MediaUrl,
	    			'SourceURL':Image.SourceUrl,
	    			'Title':Image.Title,
	    		});
	    	})
	    	res.json(output);
  	}
})
});

app.get('/logs',function(req,res){
  var q = Log.find({}).sort({'created_at':-1}).limit(10).exec(function(err,data){res.send(data)});
  
});

app.listen(port,function(){
	console.log('Server running on port '+ port)
});




@@ -709,99 +709,8 @@
  %7B%0A%09
-var newLog = %7B log: 'just a test' %7D;%0A     Log.create(newLog, function(err, log) %7B%0A%0A     %7D);
 %0A%09re
@@ -815,22 +815,22 @@
     var 
-number
+offset
  = req.q
@@ -844,17 +844,16 @@
 fset %7C%7C 
-1
 0;%0A%0A    
@@ -1105,16 +1105,28 @@
 top=
-'+number
+10'+'&$skip='+offset
 +'&$
@@ -1583,28 +1583,273 @@
    %09
-res.json(output);%0A  
+var newLog = %7B log: req.params.term %7D;%0A %09%09    Log.create(newLog, function(err, log) %7B%0A%0A %09%09    %09if(err)%7B%0A %09%09    %09%09console.log(err);%0A %09%09    %09%7Delse%7B%0A %09%09    %09%09console.log(log)%0A %09%09    %09%7D%0A%0A%09    %09 %7D);%0A%09    %09res.json(output);%0A  %09%09%7Delse if(error)%7B%0A  %09%09%09res.redirect('/')%0A  %09
 %09%7D%0A%7D
@@ -1913,16 +1913,21 @@
 ind(%7B%7D).
+%0A  %09%09
 sort(%7B'c
@@ -1936,18 +1936,17 @@
 ated
-_a
+A
 t':-1%7D).
 limi
@@ -1941,16 +1941,21 @@
 t':-1%7D).
+%0A  %09%09
 limit(10
@@ -1956,16 +1956,64 @@
 mit(10).
+%0A  %09%09select(%7B%22createdAt%22:1,%22log%22:1,_id:0%7D).%0A  %09%09
 exec(fun

add logs functionality,fix offset