<commit_before>var express = require('express');
var crypto = require('crypto');
var db = require('../helpers/db')();
var dateHelper = require('../helpers/date')();

var router = express.Router();

/************************/
/* GET user itineraries */
/************************/
router.get('/user/:userid', function (req, res, next) {
    // Get params from client
    var userId = req.params.userid;

    // Get itineraries of a user in database with these parameters if exists
	var itineraries = db.byFields('itinerary', {
		id_User: userId
	}, function(error, results, fields){
		if(error != null)
			res.respond(error, 500);
		else
			res.respond(results);
	});
});

router.get('/:itineraryid', function (req, res, next) {
    // Get params from client
    var itineraryId = req.params.itineraryid;

    // Get itineraries of a user in database with these parameters if exists
	var itineraries = db.byFields('itinerary', {
		id: itineraryId
	}, function(error, results, fields){
		if(error != null)
			res.respond(error, 500);
		else
			res.respond(results.length > 0 ? results[0] : null);
	});
});

/************************/
/* PUT update itinerary */
/************************/
router.put('/:itineraryid', function(req, res, next){
	// Get params from client
	var itineraryId = req.params.itineraryid;

	var name = req.body.name;
	var country = req.body.country;
	var description = req.body.description;

	// Update the itinerary in database
	db.update('itinerary', {
		name: name, 
		country: country, 
		description: description, 
	}, {
		id: itineraryId
	}, function(error, results, fields){
		if(error != null)
			res.respond(error, 500);
		else
			res.respond([]);
	});
});

/**********************/
/* POST add itinerary */
/**********************/
router.post('/', function (req, res, next) {
	// Get params from client
	var name = req.body.name;
	var country = req.body.country;
	var description = req.body.description;
	var userId = req.body.userId;

	// Insert the itinerary in database
	db.add('itinerary', {
		name: name, 
		country: country, 
		description: description, 
        id_User: userId,
		date: dateHelper.getDateTime()
	}, function(error, results, fields){
		if(error != null)
			res.respond(error, 500);
		else
			res.respond({id: results.insertId});
	});
});

/***************************/
/* DELETE delete itinerary */
/***************************/
router.delete('/:itineraryid', function (req, res, next) {
	// Get params from client
	var itineraryId = req.params.itineraryid;

	// Delete the itinerary in database
	db.remove('step', {
        id_Itinerary: itineraryId,
	}, function(error, results, fields){
		if(error != null)
			res.respond(error, 500);
		else {
			db.remove('itinerary', {
				id: itineraryId,
			}, function(error, results, fields){
				if(error != null)
					res.respond(error, 500);<commit_msg>add route to get all itineraries<commit_after>var express = require('express');
var crypto = require('crypto');
var db = require('../helpers/db')();
var dateHelper = require('../helpers/date')();

var router = express.Router();

/************************/
/* GET user itineraries */
/************************/
router.get('/', function (req, res, next) {
	// Get itineraries in database
	var itineraries = db.all('itinerary', function (error, results, fields) {
		if (error != null)
			res.respond(error, 500);
		else {
			if (results == null || results.length == 0)
				res.respond(results, 200);
			else {
				var finalresult = [];

				results.forEach(function (element) {
					db.byFields('user', {
						id: element.id_User
					}, function (error2, results2, fields2) {
						if (error2 != null)
							res.respond(error2, 500);
						else {
							element['user'] = results2[0];
							finalresult.push(element);
						}

						if(finalresult.length == results.length)
							res.respond(finalresult, 200);
					});
				});
			}
		}
	});
});

router.get('/user/:userid', function (req, res, next) {
	// Get params from client
	var userId = req.params.userid;

	// Get itineraries of a user in database with these parameters if exists
	var itineraries = db.byFields('itinerary', {
		id_User: userId
	}, function (error, results, fields) {
		if (error != null)
			res.respond(error, 500);
		else
			res.respond(results);
	});
});

router.get('/:itineraryid', function (req, res, next) {
	// Get params from client
	var itineraryId = req.params.itineraryid;

	// Get itineraries of a user in database with these parameters if exists
	var itineraries = db.byFields('itinerary', {
		id: itineraryId
	}, function (error, results, fields) {
		if (error != null)
			res.respond(error, 500);
		else
			res.respond(results.length > 0 ? results[0] : null);
	});
});

/************************/
/* PUT update itinerary */
/************************/
router.put('/:itineraryid', function (req, res, next) {
	// Get params from client
	var itineraryId = req.params.itineraryid;

	var name = req.body.name;
	var country = req.body.country;
	var description = req.body.description;

	// Update the itinerary in database
	db.update('itinerary', {
		name: name,
		country: country,
		description: description,
	}, {
			id: itineraryId
		}, function (error, results, fields) {
			if (error != null)
				res.respond(error, 500);
			else
				res.respond([]);
		});
});

/**********************/
/* POST add itinerary */
/**********************/
router.post('/', function (req, res, next) {
	// Get params from client
	var name = req.body.name;
	var country = req.body.country;
	var description = req.body.description;
	var userId = req.body.userId;

	// Insert the itinerary in database
	db.add('itinerary', {
		name: name,
		country: country,
		description: description,
		id_User: userId,
		date: dateHelper.getDateTime()
	}, function (error, results, fields) {
		if (error != null)
			res.respond(error, 500);
		else
			res.respond({ id: results.insertId });
	});
});

/***************************/
/* DELETE delete itinerary */
/***************************/
router.delete('/:itineraryid', function (req, res, next) {
	// Get params from client
	var itineraryId = req.params.itineraryid;

	// Delete the itinerary in database
	db.remove('step', {
		id_Itinerary: itineraryId,
	}, function (error, results, fields) {
		if (error != null)
			res.respond(error, 500);
		else {
			db.remove('itinerary', {
				id: itineraryId,
			}, function (error, results, fields) {
				if (error != null)
					res.respond(error, 500);
{'fraction_non_alphanumeric': 0.20737184450983429, 'fraction_numerical': 0.009292240978782717, 'mean_word_length': 3.636037329504666, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '474119', 'n_tokens_mistral': 2333, 'n_tokens_neox': 2032, 'n_words': 540}