<?php
	// @TODO Comments
	$mysql = new \sven\sys\mysql\mysql();
	$fluent = $mysql->getBuilder();
	if(get_class($fluent) === "sven\sys\sven\uncallable") { \sven\sys\core::addException(new \sven\sys\Exception("\sven\sys\sven\uncallable", "Can't find/use FluentPDO"));}

	$body = \sven\sys\sven\web::getRequestBody();
	$array = (object) [];

	if($auth->loggedIn()) {
		switch($_SERVER["REQUEST_METHOD"]) {
			case 'GET':
				$array = (object) [];
				$query = $fluent->from('t_artikel')->select('t_artikel.*');

				if(isset($_GET['limit']) && !is_nan($_GET['limit'])) $query->limit($_GET['limit']);
				if(isset($_GET['search_for']) && isset($_GET['in'])) $query->where($_GET['in'].' LIKE ?', $_GET['search_for'].'%');

				$rows = $query->fetchAll();
				$data  = [];
				foreach ($rows as $arr) {
					$data[] = $arr;
				}

				$array->data = ["DATA" => $data];
				$array->state = "successed";
				break;
			case 'POST':
				$array = (object) [];

				$array->data = ["POST" => \sven\sys\sven\web::getRequestBody()];
				$array->state = "successed";
				break;
			case 'PATCH':
				$array = (object) ["DATA" => ["PATCH" => \sven\sys\sven\web::getRequestBody()]];
				break;
			case 'DELETE':
				$array = (object) [];
				break;
		}
		if(empty($array)) {
			$array = (object) [
				"msg" => "No output data was passed. Request unfortunately failed.",
				"error" => ["NO_OUTPUT"]
			];
		}
	} else {
		$array = (object) [
			"body" => $body,
			"msg" => "User not logged in.",
			"error" => ["AUTHENTICATION_FAILED"]
		];
	}
	\sven\sys\core::replaceApiOutput($array);

?>
