<commit_before>// API/controllers/search.js

/**
* @apiDefine SearchObjectPostParam
*
* @apiParam {String} [name] Name of the ingredient (Regex)
* @apiParam {String[]} [tags] Name of the tags that your ingredient must have.
* @apiParam {Object[]} [order] Order of the return of the search
* @apiParam {String} [order.order] Order (can be asc or desc)
* @apiParam {String} [order.field] Field which is order (ex: fat)
* @apiParam {Object[]} metadata Number of items to return, page of the items
* @apiParam {String} metadata.items number of items that are return for the current page
* @apiParam {String} metadata.page Number of the page that you want to ask
*/

/**
* @apiDefine SearchRequestJSON
*
* @apiParamExample {json} Request-Example:
*{
*    "name": "fu",
*    "order": {"order": "desc",
*              "field": "fat"
*    },
*    "tags": ['fruit'],
*    "metadata": {"items": 1,
*                  "page": 1
*    }
*}
*/

var Ingredients = require('../models/ingredients');

/*
** POSTS
*/

/**
* @api {post} /search/ingredients/ Search some Ingredients
* @apiName postSearch
* @apiGroup Search
* @apiVersion 0.1.0
*
* @apiUse SearchObjectPostParam
* 
* @apiUse SearchRequestJSON
*
* @apiSuccessExample Success-Response
*     HTTP/1.1 200 OK
*	  {
*		"metadata": [{"current_page": "2",
*			      "total_page": "24",
*			       "order": {"field": "fat",
*					 "order": "desc"},
*				"name": "Tom"}],
*		"Ingredients": [{"_id" : "561830c5fecdba4f72668fe8",
*      				"name": "Tomato",
*      				"description": "Very yummy fruit."
*				"fat" : 0.3,
*				"carbohydrates" : 5.8,
*				"protein" : 1.3,
*				"tags" : [{
*					"name" : "fruit",
*					"description" : "Tag concerning fruits",
*					"flag" : {
*								"name" : "SAFE",
*								"level" : 0
*							 }
*				   }]
*	  }
*
* @apiError message Nothing find for this search
* @apiErrorExample Invalid Parameter Value
*     HTTP/1.1 404 Not Found
*     {
*       "message": "You must at least set a name or a tag to search"
*     }
*/

exports.postSearchIngredients = function (req, res, flag) {
	var name =  req.body.name;
	var order = ""
	if (req.body.order) {
		var order = req.body.order
		var order_order = req.body.order.order;
		var order_field = req.body.order.field;
		var query = {};
		query[order_field] = order_order;
	}
	var items_number = req.body.metadata.items;
	var items_page = req.body.metadata.page;
	var tag_list = req.body.tags;

	//If name and tags are not set

	if (!name && !tag_list)
		return flag === true ? -1 : res.status(400).send({message : 'You must at least set a name or a tag to search'})

	if (!items_number || !items_page)
		return flag === true ? -1 : res.status(400).send({message : 'You must set the metadata'})

	//If name and tags are set

	if (name && tag_list){

	//Get nbr_page_max
		var total_page
			Ingredients.find({
			"name": { "$regex": name, "$options": "i" },
			"tags.name": { $all: tag_list}
			},
			function (err, docs) {
					total_page = docs.length;				
				}
			)

		if (order === ""){
			Ingredients.find({
				"name": { "$regex": name, "$options": "i" },
				"tags.name": { $all: tag_list}
				},
				function (err, docs) {
					if (err)
						return (res.send(err));
					else if (docs.length <= 0)
						return (res.status(404).send({message : 'Nothing find for this search'}))<commit_msg>Search/Ingredients Fix page_total<commit_after>// API/controllers/search.js

/**
* @apiDefine SearchObjectPostParam
*
* @apiParam {String} [name] Name of the ingredient (Regex)
* @apiParam {String[]} [tags] Name of the tags that your ingredient must have.
* @apiParam {Object[]} [order] Order of the return of the search
* @apiParam {String} [order.order] Order (can be asc or desc)
* @apiParam {String} [order.field] Field which is order (ex: fat)
* @apiParam {Object[]} metadata Number of items to return, page of the items
* @apiParam {String} metadata.items number of items that are return for the current page
* @apiParam {String} metadata.page Number of the page that you want to ask
*/

/**
* @apiDefine SearchRequestJSON
*
* @apiParamExample {json} Request-Example:
*{
*    "name": "fu",
*    "order": {"order": "desc",
*              "field": "fat"
*    },
*    "tags": ['fruit'],
*    "metadata": {"items": 1,
*                  "page": 1
*    }
*}
*/

var Ingredients = require('../models/ingredients');

/*
** POSTS
*/

/**
* @api {post} /search/ingredients/ Search some Ingredients
* @apiName postSearch
* @apiGroup Search
* @apiVersion 0.1.0
*
* @apiUse SearchObjectPostParam
* 
* @apiUse SearchRequestJSON
*
* @apiSuccessExample Success-Response
*     HTTP/1.1 200 OK
*	  {
*		"metadata": [{"current_page": "2",
*			      "total_page": "24",
*			       "order": {"field": "fat",
*					 "order": "desc"},
*				"name": "Tom"}],
*		"Ingredients": [{"_id" : "561830c5fecdba4f72668fe8",
*      				"name": "Tomato",
*      				"description": "Very yummy fruit."
*				"fat" : 0.3,
*				"carbohydrates" : 5.8,
*				"protein" : 1.3,
*				"tags" : [{
*					"name" : "fruit",
*					"description" : "Tag concerning fruits",
*					"flag" : {
*								"name" : "SAFE",
*								"level" : 0
*							 }
*				   }]
*	  }
*
* @apiError message Nothing find for this search
* @apiErrorExample Invalid Parameter Value
*     HTTP/1.1 404 Not Found
*     {
*       "message": "You must at least set a name or a tag to search"
*     }
*/

exports.postSearchIngredients = function (req, res, flag) {
	var name =  req.body.name;
	var order = ""
	if (req.body.order) {
		var order = req.body.order
		var order_order = req.body.order.order;
		var order_field = req.body.order.field;
		var query = {};
		query[order_field] = order_order;
	}
	var items_number = req.body.metadata.items;
	var items_page = req.body.metadata.page;
	var tag_list = req.body.tags;

	//If name and tags are not set

	if (!name && !tag_list)
		return flag === true ? -1 : res.status(400).send({message : 'You must at least set a name or a tag to search'})

	if (!items_number || !items_page)
		return flag === true ? -1 : res.status(400).send({message : 'You must set the metadata'})

	//If name and tags are set

	if (name && tag_list){

	//Get nbr_page_max
		var total_page
			Ingredients.find({
			"name": { "$regex": name, "$options": "i" }
			},
			function (err, docs) {
					total_page = Math.round(docs.length / items_number);
					if (total_page == '0')
						total_page = 1	
				}
			)

		if (order === ""){
			Ingredients.find({
				"name": { "$regex": name, "$options": "i" },
				"tags.name": { $all: tag_list}
				},
				function (err, docs) {
					if (err)
						return (res.send(err));
					else if (docs.length <= 0)
						return (res.status(404).send({message : 'Nothing find for this search'}))
{'fraction_non_alphanumeric': 0.17010155316606929, 'fraction_numerical': 0.015531660692951015, 'mean_word_length': 2.98868374032162, 'pattern_counts': {'":': 53, '<': 5, '<?xml version=': 0, '>': 3, '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': '5094082', 'n_tokens_mistral': 2715, 'n_tokens_neox': 2361, 'n_words': 664}