var express = require('express');
var router = express.Router();
var cps = require('cps-api');
var ProfileSchema = require('../models/profileSchema.js')
var cpsConn = new cps.Connection('tcp://cloud-eu-0.clusterpoint.com:9007', 'Database',
            process.env.CLUSTERPOINT_USERNAME, process.env.CLUSTERPOINT_PASSWORD,
            'document', 'document/id', {account: 1393});

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.post('/signup', function(req, res, next) {
  var newProfile = new ProfileSchema(req.body);
  var document = [newProfile];
  cpsConn.sendRequest(new cps.InsertRequest(document), function (err, resp) {
    if (err){
      res.json(err);
    }
     res.json(resp);
  });
});

router.post('/login', function(req, res, next) {
  var retrieve_req = new cps.RetrieveRequest(req.body.username);
  cpsConn.sendRequest(retrieve_req, function (err, retrieve_resp) {
     if (err){
       res.json(err);
     }
     if (retrieve_resp) {
        res.json(retrieve_resp.results.document[0]);
     }
  }, 'json');
});

router.get('/artists/:id', function(req, res, next){
  var retrieve_req = new cps.RetrieveRequest(req.params.id);
  cpsConn.sendRequest(retrieve_req, function (err, retrieve_resp) {
     if (err){
        res.json(err);
      }
     if (retrieve_resp) {
        res.json(retrieve_resp.results.document[0]);
     }
  }, 'json');;
});

router.patch('/artists/:id', function(req, res, next){
  var replace_request = new cps.PartialReplaceRequest(req.body);
  cpsConn.sendRequest(replace_request, function (err, replace_resp) {
     if (err){
       res.json(err);
     }
     if (replace_resp) {
        res.json(replace_resp);
     }
  }, 'json');
});

router.delete('/artists/:id', function(req, res, next){
  cpsConn.sendRequest(new cps.DeleteRequest({ id: req.params.id }), function (err, delete_resp) {
     if (err){
       res.json(err);
     }
     if(delete_resp){
      res.json(delete_resp);
     }
  });
});


module.exports = router;

@@ -2016,16 +2016,1236 @@
 );%0A%7D);%0A%0A
+router.get('/venues/', function(req, res, next)%7B%0A  var search_req = new cps.SearchRequest(cps.Term(%22venue%22, %22userType%22),%0A      0, 20);%0A    cpsConn.sendRequest(search_req, function (err, search_resp) %7B%0A       if (err) return console.log(err);%0A       console.log(search_resp.results.document);%0A    %7D);%0A%7D);%0A%0Arouter.get('/venues/:id', function(req, res, next)%7B%0A  var retrieve_req = new cps.RetrieveRequest(req.params.id);%0A  cpsConn.sendRequest(retrieve_req, function (err, retrieve_resp) %7B%0A     if (err)%7B%0A        res.json(err);%0A      %7D%0A     if (retrieve_resp) %7B%0A        res.json(retrieve_resp.results.document%5B0%5D);%0A     %7D%0A  %7D, 'json');;%0A%7D);%0A%0Arouter.patch('/venues/:id', function(req, res, next)%7B%0A  var replace_request = new cps.PartialReplaceRequest(req.body);%0A  cpsConn.sendRequest(replace_request, function (err, replace_resp) %7B%0A     if (err)%7B%0A       res.json(err);%0A     %7D%0A     if (replace_resp) %7B%0A        res.json(replace_resp);%0A     %7D%0A  %7D, 'json');%0A%7D);%0A%0Arouter.delete('/venues/:id', function(req, res, next)%7B%0A  cpsConn.sendRequest(new cps.DeleteRequest(%7B id: req.params.id %7D), function (err, delete_resp) %7B%0A     if (err)%7B%0A       res.json(err);%0A     %7D%0A     if(delete_resp)%7B%0A      res.json(delete_resp);%0A     %7D%0A  %7D);%0A%7D);%0A%0A
 %0Amodule.

add all venues end points