<commit_before><?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
date_default_timezone_set('America/Sao_Paulo');

require __DIR__ . '/vendor/autoload.php';

$app = new \Slim\Slim();
require __DIR__ . '/app/bootstrap.php';

// Middlewares
$app->add(new LogMiddleware());
$app->add(new ResponseTypeMiddleware());
$app->add(new AppMiddleware());

// Attach user authentication
$app->add(new AuthMiddleware());

$app->get('/', function() use ($app) {
	$app->content = models\App::all();
});

/**
 * Misc system utilities
 */
$app->group('/system', function() use ($app) {
	/**
	 * GET /system/time
	 */
	$app->get('/time', function() use ($app) {
		$app->content = time();
	});
});

/**
 * Collection routes
 */
$app->group('/collection', function () use ($app) {

	// Get collection data for CREATE/UPDATE
	$app->container->singleton('collection_data', function () use ($app) {
		//
		// TODO: android and ios clients should deprecate 'data' param, and send it entirelly on BODY
		//
		$data = $app->request->post('d') ?: $app->request->post('data') ?: $app->request->post();
		if (!empty($_FILES)) {
			$data[models\Collection::ATTACHED_FILES] = $_FILES;
		}
		return $data;
	});

	/**
	 * GET /collection/:name
	 */
	$app->get('/:name', function($name) use ($app) {
		$query = models\App::collection($name)->filter($app->request->get('q'));

		// Apply ordering
		if ($s = $app->request->get('s')) {
			foreach($s as $ordering) {
				$query->orderBy(reset($ordering), end($ordering));
			}
		}

		// Apply group
		if ($group = $app->request->get('g')) {
			foreach($group as $field) {
				$query = $query->groupBy($field);
			}
		}

		// limit / offset
		if ($offset = $app->request->get('offset')) {
			$query = $query->skip($offset);
		}
		if ($limit = $app->request->get('limit')) {
			$query = $query->take($limit);
		}

		if ($aggr = $app->request->get('aggr')) {
			// Aggregate 'max'/'min'/'avg'/'sum' methods
			if ($aggr['field']) {
				$app->content = $query->{$aggr['method']}($aggr['field']);
			} else {
				// Aggregate 'count'
				$app->content = $query->{$aggr['method']}();
			}

		} else if ($app->request->get('p')) {
			// Apply pagination
			$app->content = $query->paginate($app->request->get('p'));

		} else if ($app->request->get('f')) {
			// First
			$app->content = $query->first();

		} else {
			$app->content = $query->get();
		}

	});

	/**
	 * POST /collection/:name
	 */
	$app->post('/:name', function($name) use ($app) {
		$model = models\App::collection($name)->create_new($app->collection_data);

		if (!$model->save()) {
			throw new ForbiddenException("Can't save '{$name}'.");
		}

		$app->content = $model;
	});

	/**
	 * PUT /collection/:name
	 */
	$app->put('/:name', function($name) use ($app) {
		$query = models\App::collection($name)->filter($app->request->get('q'));

		if ($operation = $app->request->post('op')) {
			// Operations: increment/decrement
			$app->content = $query->{$operation['method']}($operation['field'], $operation['value']);
		} else {

			// Perform raw update
			//
			// FIXME: 'd' is deprecated. use 'data' instead.
			//
			// Who is using it?
			// - 'plugados-site'
			// - 'clubsocial-possibilidades'
			//
			$app->content = $query->update($app->collection_data);
		}
	});

	/**
	 * TODO: DRY with PUT /collection/:name
	 * PUT /collection/:name/:id
	 * Curently only used on : Only Backbone.DLModel
	 */
	$app->put('/:name/:id', function($name, $id) use ($app) {
		$query = models\App::collection($name)->where('_id', $id);

		if ($operation = $app->request->post('op')) {
			// Operations: increment/decrement
			$app->content = $query->{$operation['method']}($operation['field'], $operation['value']);
		} else {
			// Perform raw update
			$app->content = $query->update($app->collection_data);
		}
	});

	/**
	 * DELETE /collection/:name
	 */
	$app->delete('/:name', function($name) use ($app) {
		$query = \models\App::collection($name)->filter($app->request->post('q'));
		$app->content = array('success' => $query->delete());
	});

	/**
	 * GET /collection/:name/:id
	 */
	$app->get('/:name/:id', function($name, $id) use ($app) {
		$app->content = models\App::collection($name)->find($id);
	});

	/**
	 * POST /collection/:name/:id
	 */
	$app->post('/:name/:id', function($name, $id) use ($app) {
		$app->content = array(
			'success' => models\App::collection($name)->find($id)->update($app->collection_data)
		);
	});

	/**
	 * DELETE /collection/:name/:id
	 */
	$app->delete('/:name/:id', function($name, $id) use ($app) {
		$app->content = array('success' => models\App::collection($name)->find($id)->delete());
	});

	/**
	 * Nested collections
	 */
	// $app->get('/:name/:id', function($name, $id) use ($app) {
	// 	$app->content = array('success' => models\Collection::query()->from($name)->where('_id', $id)->delete());
	// });<commit_msg>return collection attributes when updating by _id<commit_after><?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
date_default_timezone_set('America/Sao_Paulo');

require __DIR__ . '/vendor/autoload.php';

$app = new \Slim\Slim();
require __DIR__ . '/app/bootstrap.php';

// Middlewares
$app->add(new LogMiddleware());
$app->add(new ResponseTypeMiddleware());
$app->add(new AppMiddleware());

// Attach user authentication
$app->add(new AuthMiddleware());

$app->get('/', function() use ($app) {
	$app->content = models\App::all();
});

/**
 * Misc system utilities
 */
$app->group('/system', function() use ($app) {
	/**
	 * GET /system/time
	 */
	$app->get('/time', function() use ($app) {
		$app->content = time();
	});
});

/**
 * Collection routes
 */
$app->group('/collection', function () use ($app) {

	// Get collection data for CREATE/UPDATE
	$app->container->singleton('collection_data', function () use ($app) {
		//
		// TODO: android and ios clients should deprecate 'data' param, and send it entirelly on BODY
		//
		$data = $app->request->post('d') ?: $app->request->post('data') ?: $app->request->post();
		if (!empty($_FILES)) {
			$data[models\Collection::ATTACHED_FILES] = $_FILES;
		}
		return $data;
	});

	/**
	 * GET /collection/:name
	 */
	$app->get('/:name', function($name) use ($app) {
		$query = models\App::collection($name)->filter($app->request->get('q'));

		// Apply ordering
		if ($s = $app->request->get('s')) {
			foreach($s as $ordering) {
				$query->orderBy(reset($ordering), end($ordering));
			}
		}

		// Apply group
		if ($group = $app->request->get('g')) {
			foreach($group as $field) {
				$query = $query->groupBy($field);
			}
		}

		// limit / offset
		if ($offset = $app->request->get('offset')) {
			$query = $query->skip($offset);
		}
		if ($limit = $app->request->get('limit')) {
			$query = $query->take($limit);
		}

		if ($aggr = $app->request->get('aggr')) {
			// Aggregate 'max'/'min'/'avg'/'sum' methods
			if ($aggr['field']) {
				$app->content = $query->{$aggr['method']}($aggr['field']);
			} else {
				// Aggregate 'count'
				$app->content = $query->{$aggr['method']}();
			}

		} else if ($app->request->get('p')) {
			// Apply pagination
			$app->content = $query->paginate($app->request->get('p'));

		} else if ($app->request->get('f')) {
			// First
			$app->content = $query->first();

		} else {
			$app->content = $query->get();
		}

	});

	/**
	 * POST /collection/:name
	 */
	$app->post('/:name', function($name) use ($app) {
		$model = models\App::collection($name)->create_new($app->collection_data);

		if (!$model->save()) {
			throw new ForbiddenException("Can't save '{$name}'.");
		}

		$app->content = $model;
	});

	/**
	 * PUT /collection/:name
	 */
	$app->put('/:name', function($name) use ($app) {
		$query = models\App::collection($name)->filter($app->request->get('q'));

		if ($operation = $app->request->post('op')) {
			// Operations: increment/decrement
			$app->content = $query->{$operation['method']}($operation['field'], $operation['value']);
		} else {

			// Perform raw update
			//
			// FIXME: 'd' is deprecated. use 'data' instead.
			//
			// Who is using it?
			// - 'plugados-site'
			// - 'clubsocial-possibilidades'
			//
			$app->content = $query->update($app->collection_data);
		}
	});

	/**
	 * TODO: DRY with PUT /collection/:name
	 * PUT /collection/:name/:id
	 * Curently only used on : Only Backbone.DLModel
	 */
	$app->put('/:name/:id', function($name, $id) use ($app) {
		$query = models\App::collection($name)->where('_id', $id);

		if ($operation = $app->request->post('op')) {
			// Operations: increment/decrement
			$app->content = $query->{$operation['method']}($operation['field'], $operation['value']);
		} else {
			// Perform raw update
			$app->content = $query->update($app->collection_data);
		}
	});

	/**
	 * DELETE /collection/:name
	 */
	$app->delete('/:name', function($name) use ($app) {
		$query = \models\App::collection($name)->filter($app->request->post('q'));
		$app->content = array('success' => $query->delete());
	});

	/**
	 * GET /collection/:name/:id
	 */
	$app->get('/:name/:id', function($name, $id) use ($app) {
		$app->content = models\App::collection($name)->find($id);
	});

	/**
	 * POST /collection/:name/:id
	 */
	$app->post('/:name/:id', function($name, $id) use ($app) {
		$model = models\App::collection($name)->find($id);
		$model->update($app->collection_data);
		$app->content = $model;
	});

	/**
	 * DELETE /collection/:name/:id
	 */
	$app->delete('/:name/:id', function($name, $id) use ($app) {
		$app->content = array('success' => models\App::collection($name)->find($id)->delete());
	});

	/**
	 * Nested collections
	 */
	// $app->get('/:name/:id', function($name, $id) use ($app) {
	// 	$app->content = array('success' => models\Collection::query()->from($name)->where('_id', $id)->delete());
	// });
{'fraction_non_alphanumeric': 0.2529302899444787, 'fraction_numerical': 0.00020563438206868187, 'mean_word_length': 3.614326375711575, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 210, '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': '3043112', 'n_tokens_mistral': 3689, 'n_tokens_neox': 3372, 'n_words': 601}