Skip to content
Snippets Groups Projects
Commit 7246bbe6 authored by Nigel  Sahl's avatar Nigel Sahl
Browse files

Create GymFilterController.php

parent 9daf0c20
Branches
Tags
No related merge requests found
<?php
require __DIR__ . "/../inc/bootstrap.php";
require_once PROJECT_ROOT_PATH . "/controller/BaseController.php";
require_once PROJECT_ROOT_PATH . "/services/GymService.php";
require_once PROJECT_ROOT_PATH . "/services/MultimediaService.php";
class GymFilterController extends BaseController
{
public function respond()
{
$strErrorDesc = '';
$requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]);
$arrQueryStringParams = $this->getQueryStringParams();
$responseData = [];
try {
switch ($requestMethod) {
case 'PUT':
$jsonData = file_get_contents("php://input");
$params = json_decode($jsonData, true);
$currentPage = $params['page'];
$selectedCity = $params['cityId'];
$selectedSorting = $params['sorting'];
$selectedPriceRange = $params['priceRange'];
$sortingOption = $params['sortingOption'];
$priceRangeOption = $params['priceRangeOption'];
$itemInPage = $params['gymCountInPage'];
$searching = $params['searching'];
$gyms = GymService::getInstance()->getFiltered(
[
'page' => $currentPage,
'cityId' => $selectedCity,
'sorting' => $selectedSorting,
'priceRange' => $selectedPriceRange,
'sortingOption' => $sortingOption,
'priceRangeOption' => $priceRangeOption,
'gymCountInPage' => $itemInPage,
'searching' => $searching
]
);
// make it gyms -> a,b,c
// and itemCount -> 3 in response data
$gyms_response = [];
foreach ($gyms['gyms'] as $gym) {
array_push($gyms_response, $gym->toResponse());
}
$responseData['gyms'] = $gyms_response;
$responseData['itemCount'] = $gyms['itemCount'];
break;
default:
$strErrorDesc = 'Method not supported';
$strErrorHeader = 'HTTP/1.1 400 Bad Request';
break;
}
} catch (Error $e) {
$strErrorDesc = $e->getMessage() . 'Something went wrong!';
$strErrorHeader = 'HTTP/1.1 500 Internal Server Error';
} catch (ServiceException $e) {
$strErrorDesc = (string) "Service Exception Encountered";
$strErrorHeader = 'HTTP/1.1 400 Bad Request';
} catch (Exception $e) {
$strErrorDesc = $e->getMessage();
$strErrorHeader = 'HTTP/1.1 400 Bad Request';
}
// send output
if ($strErrorDesc) {
$this->sendOutput(
json_encode(array('error' => $strErrorDesc)),
array('Content-Type: application/json', $strErrorHeader)
);
}
$this->sendOutput(
json_encode($responseData),
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
);
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment