From 7861f1650c0681fe288d7f47b93c6371da13e921 Mon Sep 17 00:00:00 2001 From: rayhanp1402 <rayhan.hanif14maulana@gmail.com> Date: Wed, 25 Oct 2023 17:38:42 +0700 Subject: [PATCH] Bungkus Controller dengan ControllerWrapper --- src/api/city/index.php | 4 +- src/api/gym/filter/index.php | 4 +- src/api/gym/index.php | 4 +- src/api/gym/media/index.php | 4 +- src/api/gym/rating/index.php | 4 +- src/api/login/index.php | 4 +- src/api/logout/index.php | 4 +- src/api/signup/index.php | 4 +- src/api/user/index.php | 4 +- src/controller/BaseController.php | 22 +- src/controller/CityController.php | 101 ++---- src/controller/ControllerWrapper.php | 36 +- src/controller/GymController.php | 471 ++++++++++++------------- src/controller/GymFilterController.php | 109 +++--- src/controller/GymMediaController.php | 177 ++++------ src/controller/LoginController.php | 91 ++--- src/controller/LogoutController.php | 47 +-- src/controller/RatingController.php | 116 +++--- src/controller/SignupController.php | 111 +++--- src/controller/UserController.php | 158 ++++----- 20 files changed, 630 insertions(+), 845 deletions(-) diff --git a/src/api/city/index.php b/src/api/city/index.php index faad827..b70f4d3 100644 --- a/src/api/city/index.php +++ b/src/api/city/index.php @@ -1,8 +1,10 @@ <?php require_once __DIR__ . "/../../controller/CityController.php"; +require_once __DIR__ . "/../../controller/ControllerWrapper.php"; $controller = new CityController(); +$wrapper = new ControllerWrapper($controller); -$controller->respond(); +$wrapper->respond(); ?> \ No newline at end of file diff --git a/src/api/gym/filter/index.php b/src/api/gym/filter/index.php index aee42de..8661a49 100644 --- a/src/api/gym/filter/index.php +++ b/src/api/gym/filter/index.php @@ -1,8 +1,10 @@ <?php require_once __DIR__ . "/../../../controller/GymFilterController.php"; +require_once __DIR__ . "/../../../controller/ControllerWrapper.php"; $controller = new GymFilterController(); +$wrapper = new ControllerWrapper($controller); -$controller->respond(); +$wrapper->respond(); ?> \ No newline at end of file diff --git a/src/api/gym/index.php b/src/api/gym/index.php index 5a7ef0b..e08dd6f 100644 --- a/src/api/gym/index.php +++ b/src/api/gym/index.php @@ -1,8 +1,10 @@ <?php require_once __DIR__ . "/../../controller/GymController.php"; +require_once __DIR__ . "/../../controller/ControllerWrapper.php"; $controller = new GymController(); +$wrapper = new ControllerWrapper($controller); -$controller->respond(); +$wrapper->respond(); ?> \ No newline at end of file diff --git a/src/api/gym/media/index.php b/src/api/gym/media/index.php index f0c76a3..d619222 100644 --- a/src/api/gym/media/index.php +++ b/src/api/gym/media/index.php @@ -1,8 +1,10 @@ <?php require_once __DIR__ . "/../../../controller/GymMediaController.php"; +require_once __DIR__ . "/../../../controller/ControllerWrapper.php"; $controller = new GymMediaController(); +$wrapper = new ControllerWrapper($controller); -$controller->respond(); +$wrapper->respond(); ?> \ No newline at end of file diff --git a/src/api/gym/rating/index.php b/src/api/gym/rating/index.php index d7e8aac..82c1d9c 100644 --- a/src/api/gym/rating/index.php +++ b/src/api/gym/rating/index.php @@ -1,8 +1,10 @@ <?php require_once __DIR__ . "/../../../controller/RatingController.php"; +require_once __DIR__ . "/../../../controller/ControllerWrapper.php"; $controller = new RatingController(); +$wrapper = new ControllerWrapper($controller); -$controller->respond(); +$wrapper->respond(); ?> \ No newline at end of file diff --git a/src/api/login/index.php b/src/api/login/index.php index bd2592c..cda7b5f 100644 --- a/src/api/login/index.php +++ b/src/api/login/index.php @@ -1,8 +1,10 @@ <?php require_once __DIR__ . "/../../controller/LoginController.php"; +require_once __DIR__ . "/../../controller/ControllerWrapper.php"; $controller = new LoginController(); +$wrapper = new ControllerWrapper($controller); -$controller->respond(); +$wrapper->respond(); ?> \ No newline at end of file diff --git a/src/api/logout/index.php b/src/api/logout/index.php index b933b7a..edd6dbe 100644 --- a/src/api/logout/index.php +++ b/src/api/logout/index.php @@ -1,8 +1,10 @@ <?php require_once __DIR__ . "/../../controller/LogoutController.php"; +require_once __DIR__ . "/../../controller/ControllerWrapper.php"; $controller = new LogoutController(); +$wrapper = new ControllerWrapper($controller); -$controller->respond(); +$wrapper->respond(); ?> \ No newline at end of file diff --git a/src/api/signup/index.php b/src/api/signup/index.php index d404c4d..36c4cf6 100644 --- a/src/api/signup/index.php +++ b/src/api/signup/index.php @@ -1,8 +1,10 @@ <?php require_once __DIR__ . "/../../controller/SignupController.php"; +require_once __DIR__ . "/../../controller/ControllerWrapper.php"; $controller = new SignupController(); +$wrapper = new ControllerWrapper($controller); -$controller->respond(); +$wrapper->respond(); ?> \ No newline at end of file diff --git a/src/api/user/index.php b/src/api/user/index.php index 921c27d..92ddb94 100644 --- a/src/api/user/index.php +++ b/src/api/user/index.php @@ -1,8 +1,10 @@ <?php require_once __DIR__ . "/../../controller/UserController.php"; +require_once __DIR__ . "/../../controller/ControllerWrapper.php"; $controller = new UserController(); +$wrapper = new ControllerWrapper($controller); -$controller->respond(); +$wrapper->respond(); ?> \ No newline at end of file diff --git a/src/controller/BaseController.php b/src/controller/BaseController.php index ecf68da..964392e 100644 --- a/src/controller/BaseController.php +++ b/src/controller/BaseController.php @@ -56,15 +56,15 @@ class BaseController * @param mixed $data * @param string $httpHeader */ - protected function sendOutput($data, $httpHeaders = array()) - { - header_remove('Set-Cookie'); - if (is_array($httpHeaders) && count($httpHeaders)) { - foreach ($httpHeaders as $httpHeader) { - header($httpHeader); - } - } - echo $data; - exit; - } + // protected function sendOutput($data, $httpHeaders = array()) + // { + // header_remove('Set-Cookie'); + // if (is_array($httpHeaders) && count($httpHeaders)) { + // foreach ($httpHeaders as $httpHeader) { + // header($httpHeader); + // } + // } + // echo $data; + // exit; + // } } \ No newline at end of file diff --git a/src/controller/CityController.php b/src/controller/CityController.php index 383edbf..98f37d2 100644 --- a/src/controller/CityController.php +++ b/src/controller/CityController.php @@ -8,85 +8,58 @@ require_once PROJECT_ROOT_PATH . "/services/CityService.php"; class CityController extends BaseController { - public function respond() + public function respond($requestMethod, &$responseData, &$strErrorDesc, &$strErrorHeader) { - $strErrorDesc = ''; - $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); $arrQueryStringParams = $this->getQueryStringParams(); - try { - switch ($requestMethod) { - case 'POST': - $body = $this->getBody(); - $name = $body->name; + switch ($requestMethod) { + case 'POST': + $body = $this->getBody(); + $name = $body->name; - if (empty($name)) { - throw new Exception("Please enter city name."); - } + if (empty($name)) { + throw new Exception("Please enter city name."); + } - if (strlen($name) > 50) { - throw new Exception("City name must be up to 50 characters long."); - } + if (strlen($name) > 50) { + throw new Exception("City name must be up to 50 characters long."); + } - try { - CityService::getInstance()->add($name); - } catch (ServiceException $e) { - if ($e->getErrorType() == ServiceErrorType::ENTITY_EXIST) { - throw new Exception("City already exist"); - } + try { + CityService::getInstance()->add($name); + } catch (ServiceException $e) { + if ($e->getErrorType() == ServiceErrorType::ENTITY_EXIST) { + throw new Exception("City already exist"); } + } - break; - case 'PUT': - $body = $this->getBody(); + break; + case 'PUT': + $body = $this->getBody(); - $cityId = (int) $body->city_id; - $newName = $body->new_name; - CityService::getInstance()->edit($cityId, $newName); - $responseData = ['Edited City ID' => $cityId, 'New Name' => $newName]; - break; + $cityId = (int) $body->city_id; + $newName = $body->new_name; + CityService::getInstance()->edit($cityId, $newName); + $responseData = ['Edited City ID' => $cityId, 'New Name' => $newName]; + break; - case 'DELETE': - $cityId = $arrQueryStringParams['city_id']; + case 'DELETE': + $cityId = $arrQueryStringParams['city_id']; - if (!isset($cityId)) { - throw new Exception("City id not provided!"); - } + if (!isset($cityId)) { + throw new Exception("City id not provided!"); + } - CityService::getInstance()->delete($cityId); - $responseData = ['Deleted City ID' => $cityId]; - 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'; - } + CityService::getInstance()->delete($cityId); + $responseData = ['Deleted City ID' => $cityId]; + break; - // send output - if ($strErrorDesc) { - $this->sendOutput( - json_encode(array('error' => $strErrorDesc)), - array('Content-Type: application/json', $strErrorHeader) - ); + default: + $strErrorDesc = 'Method not supported'; + $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + break; } - - $this->sendOutput( - json_encode($responseData), - array('Content-Type: application/json', 'HTTP/1.1 200 OK') - ); } } \ No newline at end of file diff --git a/src/controller/ControllerWrapper.php b/src/controller/ControllerWrapper.php index 7bdf4bb..701ea8f 100644 --- a/src/controller/ControllerWrapper.php +++ b/src/controller/ControllerWrapper.php @@ -1,9 +1,6 @@ <?php -require __DIR__ . "/../inc/bootstrap.php"; -require_once PROJECT_ROOT_PATH . "/controller/BaseController.php"; -require_once PROJECT_ROOT_PATH . "/services/UserService.php"; - +require_once __DIR__ . "/BaseController.php"; class ControllerWrapper { @@ -14,14 +11,29 @@ class ControllerWrapper $this->controller = $controller; } + protected function sendOutput($data, $httpHeaders = array()) + { + header_remove('Set-Cookie'); + if (is_array($httpHeaders) && count($httpHeaders)) { + foreach ($httpHeaders as $httpHeader) { + header($httpHeader); + } + } + echo $data; + exit; + } + public function respond() { + $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); + $strErrorDesc = ''; $strErrorHeader = ''; $responseData = []; + try { - $responseData += $this->controller->respond(); + $this->controller->respond($requestMethod, $responseData, $strErrorDesc, $strErrorHeader); } catch (Error $e) { $strErrorDesc = $e->getMessage() . ' . Something went wrong!'; $strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; @@ -35,16 +47,16 @@ class ControllerWrapper // send output if ($strErrorDesc) { - $this->controller->sendOutput( + $this->sendOutput( json_encode(array('error' => $strErrorDesc)), array('Content-Type: application/json', $strErrorHeader) ); } - - $this->controller->sendOutput( - json_encode($responseData), - array('Content-Type: application/json', 'HTTP/1.1 200 OK') - ); - + else { + $this->sendOutput( + json_encode($responseData), + array('Content-Type: application/json', 'HTTP/1.1 200 OK') + ); + } } } \ No newline at end of file diff --git a/src/controller/GymController.php b/src/controller/GymController.php index 97a43d6..b842392 100644 --- a/src/controller/GymController.php +++ b/src/controller/GymController.php @@ -10,62 +10,220 @@ require_once PROJECT_ROOT_PATH . "/services/MultimediaService.php"; class GymController extends BaseController { - public function respond() + public function respond($requestMethod, &$responseData, &$strErrorDesc, &$strErrorHeader) { - $strErrorDesc = ''; - $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); $arrQueryStringParams = $this->getQueryStringParams(); - $responseData = []; - - try { - switch ($requestMethod) { - case 'GET': - $gyms = GymService::getInstance()->getAll(); - foreach ($gyms as $gym) { - array_push($responseData, $gym->toResponse()); - } - break; - case 'POST': - $name = trim($_POST["name"]); - $len_name = strlen($name); - if (empty($name)) { - throw new Exception("Please enter gym name."); - } + switch ($requestMethod) { + case 'GET': + $gyms = GymService::getInstance()->getAll(); + foreach ($gyms as $gym) { + array_push($responseData, $gym->toResponse()); + } - if ($len_name > 50) { - throw new Exception("Gym name must be up to 50 characters long."); - } + break; + case 'POST': + $name = trim($_POST["name"]); + $len_name = strlen($name); + if (empty($name)) { + throw new Exception("Please enter gym name."); + } - if ($len_name < 5) { - throw new Exception("Gym name must be at least 5 characters long."); - } + if ($len_name > 50) { + throw new Exception("Gym name must be up to 50 characters long."); + } - $cityId = (int) (trim($_POST["city-id"])); + if ($len_name < 5) { + throw new Exception("Gym name must be at least 5 characters long."); + } - if ($cityId == 0) { - throw new Exception(0); - } + $cityId = (int) (trim($_POST["city-id"])); - $description = trim($_POST["description"]); - $len_description = strlen($description); - - if (strlen($description) > 255) { - throw new Exception("Description is limited to 255 characters long."); - } + if ($cityId == 0) { + throw new Exception(0); + } + + $description = trim($_POST["description"]); + $len_description = strlen($description); + + if (strlen($description) > 255) { + throw new Exception("Description is limited to 255 characters long."); + } + + $description = isset($_POST["description"]) ? $_POST["description"] : ""; + + $monthlyPrice = isset($_POST["monthly_price"]) ? $_POST["monthly-price"] : 0; + + if ($monthlyPrice < 0) { + throw new Exception("Monthly price must be positive."); + } + + if ($monthlyPrice > 100000000) { + throw new Exception("Monthly price must be less than 100000000."); + } + + $pictureId = $videoId = null; + + $pictureErr = ""; - $description = isset($_POST["description"]) ? $_POST["description"] : ""; + $pictureFile = $_FILES['gym-image']; + $pictureName = $pictureFile['name']; - $monthlyPrice = isset($_POST["monthly_price"]) ? $_POST["monthly-price"] : 0; + $expResult = explode('.', $pictureName); + $pictureFileExtension = strtolower(end($expResult)); - if ($monthlyPrice < 0) { - throw new Exception("Monthly price must be positive."); + if ($pictureFileExtension != 'png') { + throw new Exception("Incorrect picture format"); + } + + if ($pictureFile['size'] > 10000000) { + throw new Exception("Video file size too big"); + } + + + try { + $pictureId = MultimediaService::getInstance()->addPicture($pictureFile); + } catch (FileException $e) { + switch ($e->getErrorType()) { + default: + $pictureErr = "Mohon coba lagi"; + break; } + } + + if ($pictureErr != "") { + throw new Exception($pictureErr); + } + + $videoErr = ""; + + $videoFile = $_FILES['gym-video']; + $videoName = $videoFile['name']; - if ($monthlyPrice > 100000000) { - throw new Exception("Monthly price must be less than 100000000."); + $expResult = explode('.', $videoName); + $videoFileExtension = strtolower(end($expResult)); + + if ($videoFileExtension != 'mp4') { + throw new Exception("Incorrect video format"); + } + + if ($videoFile['size'] > 10000000) { + throw new Exception("Video file size too big"); + } + + try { + $videoId = MultimediaService::getInstance()->addVideo($videoFile); + } catch (FileException $e) { + switch ($e->getErrorType()) { + default: + $videoErr = "Mohon coba lagi"; + break; } + } + + if ($videoErr != "") { + throw new Exception($videoErr); + } + + try { + GymService::getInstance()->add( + $name, + $description, + $cityId, + $pictureId, + $videoId, + $monthlyPrice + ); + } catch (ServiceException $e) { + if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { + throw new Exception("No user with that username exists!"); + } + } + + break; + case 'PUT': + $body = $this->getBody(); + + $gymId = $body->gym_id; + $oldGym = GymService::getInstance()->getById($gymId); + + $name = $body->name; + $len_name = strlen($name); + + if (empty($name)) { + throw new Exception("Please enter gym name."); + } + + if ($len_name > 50) { + throw new Exception("Gym name must be up to 50 characters long."); + } + + if ($len_name < 5) { + throw new Exception("Gym name must be at least 5 characters long."); + } + + $cityId = $body->city_id; + + if ($cityId == 0) { + throw new Exception(0); + } + + $description = $body->description; + + if (strlen($description) > 255) { + throw new Exception("Description is limited to 255 characters long."); + } + + $description = isset($_POST["description"]) ? $_POST["description"] : ""; + + $monthlyPrice = isset($body->monthly_price) ? $body->monthly_price : 0; + + if ($monthlyPrice < 0) { + throw new Exception("Monthly price must be positive."); + } + + if ($monthlyPrice > 100000000) { + throw new Exception("Monthly price must be less than 100000000."); + } + + try { + GymService::getInstance()->edit( + $oldGym->gym_id, + $name, + $description, + $cityId, + $oldGym->picture_id, + $oldGym->video_id, + $monthlyPrice + ); + } catch (ServiceException $e) { + if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { + throw new Exception("No user with that username exists!"); + } + } + break; + case 'PATCH': + $body = $this->getBody(); + $updateRating = $body->update_rating; + if (isset($updateRating)) { + $gymId = $body->gym_id; + try { + GymService::getInstance()->updateRating($gymId); + } catch (ServiceException $e) { + if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { + throw new Exception("No user with that username exists!"); + } else { + throw new Exception("Something went wrong!"); + } + } + } else { + $gymId = $arrQueryStringParams['gym_id']; + if (!isset($gymId)) { + throw new Exception("Gym_id is not provided!"); + } + + $oldGym = GymService::getInstance()->getById($gymId); $pictureId = $videoId = null; @@ -130,224 +288,39 @@ class GymController extends BaseController throw new Exception($videoErr); } - try { - GymService::getInstance()->add( - $name, - $description, - $cityId, - $pictureId, - $videoId, - $monthlyPrice - ); - } catch (ServiceException $e) { - if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { - throw new Exception("No user with that username exists!"); - } - } - - break; - case 'PUT': - $body = $this->getBody(); - - $gymId = $body->gym_id; - $oldGym = GymService::getInstance()->getById($gymId); - - $name = $body->name; - $len_name = strlen($name); - - if (empty($name)) { - throw new Exception("Please enter gym name."); - } - - if ($len_name > 50) { - throw new Exception("Gym name must be up to 50 characters long."); - } - - if ($len_name < 5) { - throw new Exception("Gym name must be at least 5 characters long."); - } - - $cityId = $body->city_id; - - if ($cityId == 0) { - throw new Exception(0); - } - - $description = $body->description; - - if (strlen($description) > 255) { - throw new Exception("Description is limited to 255 characters long."); - } - - $description = isset($_POST["description"]) ? $_POST["description"] : ""; - - $monthlyPrice = isset($body->monthly_price) ? $body->monthly_price : 0; - - if ($monthlyPrice < 0) { - throw new Exception("Monthly price must be positive."); - } - - if ($monthlyPrice > 100000000) { - throw new Exception("Monthly price must be less than 100000000."); - } - try { GymService::getInstance()->edit( $oldGym->gym_id, - $name, - $description, - $cityId, - $oldGym->picture_id, - $oldGym->video_id, - $monthlyPrice + $oldGym->name, + $oldGym->description, + $oldGym->city_id, + $pictureId == null ? $oldGym->picture_id : $pictureId, + $videoId == null ? $oldGym->video_id : $videoId, + $oldGym->monthly_price, ); } catch (ServiceException $e) { if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { throw new Exception("No user with that username exists!"); + } else { + throw new Exception("Something went wrong! in method PATCH"); } } - break; - case 'PATCH': - $body = $this->getBody(); - $updateRating = $body->update_rating; - if (isset($updateRating)) { - $gymId = $body->gym_id; - try { - GymService::getInstance()->updateRating($gymId); - } catch (ServiceException $e) { - if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { - throw new Exception("No user with that username exists!"); - } else { - throw new Exception("Something went wrong!"); - } - } - } else { - $gymId = $arrQueryStringParams['gym_id']; - if (!isset($gymId)) { - throw new Exception("Gym_id is not provided!"); - } - - $oldGym = GymService::getInstance()->getById($gymId); - - $pictureId = $videoId = null; - - $pictureErr = ""; - - $pictureFile = $_FILES['gym-image']; - $pictureName = $pictureFile['name']; - - $expResult = explode('.', $pictureName); - $pictureFileExtension = strtolower(end($expResult)); - - if ($pictureFileExtension != 'png') { - throw new Exception("Incorrect picture format"); - } - - if ($pictureFile['size'] > 10000000) { - throw new Exception("Video file size too big"); - } - - - try { - $pictureId = MultimediaService::getInstance()->addPicture($pictureFile); - } catch (FileException $e) { - switch ($e->getErrorType()) { - default: - $pictureErr = "Mohon coba lagi"; - break; - } - } - - if ($pictureErr != "") { - throw new Exception($pictureErr); - } - - $videoErr = ""; - - $videoFile = $_FILES['gym-video']; - $videoName = $videoFile['name']; - - $expResult = explode('.', $videoName); - $videoFileExtension = strtolower(end($expResult)); - - if ($videoFileExtension != 'mp4') { - throw new Exception("Incorrect video format"); - } - - if ($videoFile['size'] > 10000000) { - throw new Exception("Video file size too big"); - } - - try { - $videoId = MultimediaService::getInstance()->addVideo($videoFile); - } catch (FileException $e) { - switch ($e->getErrorType()) { - default: - $videoErr = "Mohon coba lagi"; - break; - } - } - - if ($videoErr != "") { - throw new Exception($videoErr); - } - - try { - GymService::getInstance()->edit( - $oldGym->gym_id, - $oldGym->name, - $oldGym->description, - $oldGym->city_id, - $pictureId == null ? $oldGym->picture_id : $pictureId, - $videoId == null ? $oldGym->video_id : $videoId, - $oldGym->monthly_price, - ); - } catch (ServiceException $e) { - if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { - throw new Exception("No user with that username exists!"); - } else { - throw new Exception("Something went wrong! in method PATCH"); - } - } - } - break; - case 'DELETE': - if (!isset($arrQueryStringParams['gym_id'])) { - throw new Exception("gym_id not provided"); - } - $deletedId = (int) $arrQueryStringParams['gym_id']; - GymService::getInstance()->delete($deletedId); - $responseData += ['Success' => 'Yes', 'Gym ID' => $deletedId]; - 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) - ); + } + break; + case 'DELETE': + if (!isset($arrQueryStringParams['gym_id'])) { + throw new Exception("gym_id not provided"); + } + $deletedId = (int) $arrQueryStringParams['gym_id']; + GymService::getInstance()->delete($deletedId); + $responseData += ['Success' => 'Yes', 'Gym ID' => $deletedId]; + break; + + default: + $strErrorDesc = 'Method not supported'; + $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + break; } - $this->sendOutput( - json_encode($responseData), - array('Content-Type: application/json', 'HTTP/1.1 200 OK') - ); } } \ No newline at end of file diff --git a/src/controller/GymFilterController.php b/src/controller/GymFilterController.php index 2e98cf0..535b0b0 100644 --- a/src/controller/GymFilterController.php +++ b/src/controller/GymFilterController.php @@ -10,79 +10,52 @@ require_once PROJECT_ROOT_PATH . "/services/MultimediaService.php"; class GymFilterController extends BaseController { - public function respond() + public function respond($requestMethod, &$responseData, &$strErrorDesc, &$strErrorHeader) { - $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) - ); + 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; } - $this->sendOutput( - json_encode($responseData), - array('Content-Type: application/json', 'HTTP/1.1 200 OK') - ); } } \ No newline at end of file diff --git a/src/controller/GymMediaController.php b/src/controller/GymMediaController.php index 3dd0c7a..929735d 100644 --- a/src/controller/GymMediaController.php +++ b/src/controller/GymMediaController.php @@ -10,139 +10,112 @@ require_once PROJECT_ROOT_PATH . "/services/MultimediaService.php"; class GymMediaController extends BaseController { - public function respond() + public function respond($requestMethod, &$responseData, &$strErrorDesc, &$strErrorHeader) { - $strErrorDesc = ''; - $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); $arrQueryStringParams = $this->getQueryStringParams(); - $responseData = []; - try { - switch ($requestMethod) { - case 'POST': - $gymId = $_POST["gym-id"]; + switch ($requestMethod) { + case 'POST': + $gymId = $_POST["gym-id"]; - if (!isset($gymId)) { - throw new Exception("Gym_id is not provided!"); - } - - $oldGym = GymService::getInstance()->getById($gymId); + if (!isset($gymId)) { + throw new Exception("Gym_id is not provided!"); + } - $pictureId = $videoId = null; + $oldGym = GymService::getInstance()->getById($gymId); - $pictureErr = ""; + $pictureId = $videoId = null; + $pictureErr = ""; - if (isset($_FILES['gym-image'])) { - $pictureFile = $_FILES['gym-image']; - $pictureName = $pictureFile['name']; - $expResult = explode('.', $pictureName); - $pictureFileExtension = strtolower(end($expResult)); + if (isset($_FILES['gym-image'])) { + $pictureFile = $_FILES['gym-image']; + $pictureName = $pictureFile['name']; - if ($pictureFileExtension != 'png') { - throw new Exception("Incorrect picture format"); - } + $expResult = explode('.', $pictureName); + $pictureFileExtension = strtolower(end($expResult)); - if ($pictureFile['size'] > 10000000) { - throw new Exception("Video file size too big"); - } + if ($pictureFileExtension != 'png') { + throw new Exception("Incorrect picture format"); + } + if ($pictureFile['size'] > 10000000) { + throw new Exception("Video file size too big"); + } - try { - $pictureId = MultimediaService::getInstance()->addPicture($pictureFile); - } catch (FileException $e) { - switch ($e->getErrorType()) { - default: - $pictureErr = "Mohon coba lagi"; - break; - } - } - if ($pictureErr != "") { - throw new Exception($pictureErr); + try { + $pictureId = MultimediaService::getInstance()->addPicture($pictureFile); + } catch (FileException $e) { + switch ($e->getErrorType()) { + default: + $pictureErr = "Mohon coba lagi"; + break; } } - $videoErr = ""; + if ($pictureErr != "") { + throw new Exception($pictureErr); + } + } + $videoErr = ""; - if (isset($_FILES['gym-video'])) { - $videoFile = $_FILES['gym-video']; - $videoName = $videoFile['name']; - $expResult = explode('.', $videoName); - $videoFileExtension = strtolower(end($expResult)); + if (isset($_FILES['gym-video'])) { + $videoFile = $_FILES['gym-video']; + $videoName = $videoFile['name']; - if ($videoFileExtension != 'mp4') { - throw new Exception("Incorrect video format"); - } + $expResult = explode('.', $videoName); + $videoFileExtension = strtolower(end($expResult)); - if ($videoFile['size'] > 10000000) { - throw new Exception("Video file size too big"); - } - - try { - $videoId = MultimediaService::getInstance()->addVideo($videoFile); - } catch (FileException $e) { - switch ($e->getErrorType()) { - default: - $videoErr = "Mohon coba lagi"; - break; - } - } + if ($videoFileExtension != 'mp4') { + throw new Exception("Incorrect video format"); + } - if ($videoErr != "") { - throw new Exception($videoErr); - } + if ($videoFile['size'] > 10000000) { + throw new Exception("Video file size too big"); } try { - GymService::getInstance()->edit( - $oldGym->gym_id, - $oldGym->name, - $oldGym->description, - $oldGym->city_id, - $pictureId == null ? $oldGym->picture_id : $pictureId, - $videoId == null ? $oldGym->video_id : $videoId, - $oldGym->monthly_price, - ); - } catch (ServiceException $e) { - if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { - throw new Exception("No user with that username exists!"); + $videoId = MultimediaService::getInstance()->addVideo($videoFile); + } catch (FileException $e) { + switch ($e->getErrorType()) { + default: + $videoErr = "Mohon coba lagi"; + break; } } - 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) - ); + if ($videoErr != "") { + throw new Exception($videoErr); + } + } + + try { + GymService::getInstance()->edit( + $oldGym->gym_id, + $oldGym->name, + $oldGym->description, + $oldGym->city_id, + $pictureId == null ? $oldGym->picture_id : $pictureId, + $videoId == null ? $oldGym->video_id : $videoId, + $oldGym->monthly_price, + ); + } catch (ServiceException $e) { + if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { + throw new Exception("No user with that username exists!"); + } + } + break; + + default: + $strErrorDesc = 'Method not supported'; + $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + break; } - $this->sendOutput( - json_encode($responseData), - array('Content-Type: application/json', 'HTTP/1.1 200 OK') - ); } } \ No newline at end of file diff --git a/src/controller/LoginController.php b/src/controller/LoginController.php index f1ba86c..9e17c85 100644 --- a/src/controller/LoginController.php +++ b/src/controller/LoginController.php @@ -7,78 +7,51 @@ require_once PROJECT_ROOT_PATH . "/services/UserService.php"; class LoginController extends BaseController { - public function respond() + public function respond($requestMethod, &$responseData, &$strErrorDesc, &$strErrorHeader) { session_start(); - $strErrorDesc = ''; - $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); + $arrQueryStringParams = $this->getQueryStringParams(); - $responseData = []; - try { - switch ($requestMethod) { - case 'POST': - $body = $this->getBody(); + switch ($requestMethod) { + case 'POST': + $body = $this->getBody(); - $username = $body->username; - $password = $body->password; + $username = $body->username; + $password = $body->password; - if (empty($username)) { - throw new Exception("Please enter username."); - } + if (empty($username)) { + throw new Exception("Please enter username."); + } - // Check if password is empty - if (empty($password)) { - throw new Exception("Please enter your password."); - } + // Check if password is empty + if (empty($password)) { + throw new Exception("Please enter your password."); + } - try { - $user = UserService::getInstance()->login($username, $password); - $_SESSION['username'] = $user->get('username'); - $_SESSION['is_admin'] = $user->get('is_admin'); + try { + $user = UserService::getInstance()->login($username, $password); + $_SESSION['username'] = $user->get('username'); + $_SESSION['is_admin'] = $user->get('is_admin'); - $responseData += $user->toResponse(); + $responseData += $user->toResponse(); - } catch (ServiceException $e) { - if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { - throw new Exception("No user with that username exists!"); - } else if ($e->getErrorType() == ServiceErrorType::PASSWORD_INCORRECT) { - throw new Exception("Password is incorrect!"); - } else { - throw new Exception("Something went wrong!"); - } + } catch (ServiceException $e) { + if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { + throw new Exception("No user with that username exists!"); + } else if ($e->getErrorType() == ServiceErrorType::PASSWORD_INCORRECT) { + throw new Exception("Password is incorrect!"); + } else { + throw new Exception("Something went wrong!"); } + } - 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) - ); + break; + default: + $strErrorDesc = 'Method not supported'; + $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + break; } - - $this->sendOutput( - json_encode($responseData), - array('Content-Type: application/json', 'HTTP/1.1 200 OK') - ); } } \ No newline at end of file diff --git a/src/controller/LogoutController.php b/src/controller/LogoutController.php index 12cbf78..bcf0e62 100644 --- a/src/controller/LogoutController.php +++ b/src/controller/LogoutController.php @@ -7,48 +7,21 @@ require_once PROJECT_ROOT_PATH . "/services/UserService.php"; class LogoutController extends BaseController { - public function respond() + public function respond($requestMethod, &$responseData, &$strErrorDesc, &$strErrorHeader) { session_start(); - $strErrorDesc = ''; - $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); - $arrQueryStringParams = $this->getQueryStringParams(); - - $responseData = []; - try { - switch ($requestMethod) { - case 'POST': - UserService::getInstance()->logout(); - 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'; - } + $arrQueryStringParams = $this->getQueryStringParams(); - // send output - if ($strErrorDesc) { - $this->sendOutput( - json_encode(array('error' => $strErrorDesc)), - array('Content-Type: application/json', $strErrorHeader) - ); + switch ($requestMethod) { + case 'POST': + UserService::getInstance()->logout(); + break; + default: + $strErrorDesc = 'Method not supported'; + $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + break; } - - $this->sendOutput( - json_encode($responseData), - array('Content-Type: application/json', 'HTTP/1.1 200 OK') - ); } } \ No newline at end of file diff --git a/src/controller/RatingController.php b/src/controller/RatingController.php index 66b876c..f7e7600 100644 --- a/src/controller/RatingController.php +++ b/src/controller/RatingController.php @@ -9,85 +9,55 @@ require_once PROJECT_ROOT_PATH . "/services/UserService.php"; class RatingController extends BaseController { - public function respond() + public function respond($requestMethod, &$responseData, &$strErrorDesc, &$strErrorHeader) { session_start(); - $strErrorDesc = ''; - $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); $arrQueryStringParams = $this->getQueryStringParams(); - $responseData = []; - // $responseData += ['Method' => $requestMethod]; - - try { - switch ($requestMethod) { - case 'GET': - $gymId = (int) $arrQueryStringParams['gym_id']; - $ratings = RatingService::getInstance()->getAllFromGym($gymId); - $responseData += ['ratings' => $ratings]; - - break; - case 'POST': - $body = $this->getBody(); - - $newRating = $body->new_rating; - $gymId = $body->gym_id; - - if ( $newRating < 1 || $newRating > 5) { - throw new Exception("Rating must be between 1 and 5."); - } - - $newRatingModel = RatingService::getInstance()->add($gymId, $newRating); - $responseData += ['new_rating' => $newRating, 'gym_id' => $gymId]; - break; - case 'PUT': - - $body = $this->getBody(); - - $newRating = $body->new_rating; - $gymId = $body->gym_id; - - if ( $newRating < 1 || $newRating > 5) { - throw new Exception("Rating must be between 1 and 5."); - } - - $newRatingModel = RatingService::getInstance()->edit($gymId, $newRating); - $responseData += ['updated_rating' => $newRatingModel->rating]; - break; - case "DELETE": - $deletedId = (int) $arrQueryStringParams['gym_id']; - RatingService::getInstance()->delete($deletedId); - 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 = "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) - ); + switch ($requestMethod) { + case 'GET': + $gymId = (int) $arrQueryStringParams['gym_id']; + $ratings = RatingService::getInstance()->getAllFromGym($gymId); + $responseData += ['ratings' => $ratings]; + + break; + case 'POST': + $body = $this->getBody(); + + $newRating = $body->new_rating; + $gymId = $body->gym_id; + + if ( $newRating < 1 || $newRating > 5) { + throw new Exception("Rating must be between 1 and 5."); + } + + $newRatingModel = RatingService::getInstance()->add($gymId, $newRating); + $responseData += ['new_rating' => $newRating, 'gym_id' => $gymId]; + break; + case 'PUT': + + $body = $this->getBody(); + + $newRating = $body->new_rating; + $gymId = $body->gym_id; + + if ( $newRating < 1 || $newRating > 5) { + throw new Exception("Rating must be between 1 and 5."); + } + + $newRatingModel = RatingService::getInstance()->edit($gymId, $newRating); + $responseData += ['updated_rating' => $newRatingModel->rating]; + break; + case "DELETE": + $deletedId = (int) $arrQueryStringParams['gym_id']; + RatingService::getInstance()->delete($deletedId); + break; + default: + $strErrorDesc = 'Method not supported'; + $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + break; } - - $this->sendOutput( - json_encode($responseData), - array('Content-Type: application/json', 'HTTP/1.1 200 OK') - ); } } \ No newline at end of file diff --git a/src/controller/SignupController.php b/src/controller/SignupController.php index 0ddd6fb..7ccd563 100644 --- a/src/controller/SignupController.php +++ b/src/controller/SignupController.php @@ -7,94 +7,67 @@ require_once PROJECT_ROOT_PATH . "/services/UserService.php"; class SignupController extends BaseController { - public function respond() + public function respond($requestMethod, &$responseData, &$strErrorDesc, &$strErrorHeader) { session_start(); - $strErrorDesc = ''; - $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); + $arrQueryStringParams = $this->getQueryStringParams(); - $responseData = []; - try { - switch ($requestMethod) { - case 'POST': - $body = $this->getBody(); + switch ($requestMethod) { + case 'POST': + $body = $this->getBody(); - $username = $body->username; - $password = $body->password; - $name = $body->name; - $city_id = $body->city_id; + $username = $body->username; + $password = $body->password; + $name = $body->name; + $city_id = $body->city_id; - if (empty($username)) { - throw new Exception("Please enter username."); - } + if (empty($username)) { + throw new Exception("Please enter username."); + } - if (strlen($username) > 50) { - throw new Exception("Username must be up to 50 characters long."); - } + if (strlen($username) > 50) { + throw new Exception("Username must be up to 50 characters long."); + } - // Check if password is empty - if (empty($password)) { - throw new Exception("Please enter your password."); - } + // Check if password is empty + if (empty($password)) { + throw new Exception("Please enter your password."); + } - if (strlen($password) > 50) { - throw new Exception("Password must be up to 50 characters long."); - } + if (strlen($password) > 50) { + throw new Exception("Password must be up to 50 characters long."); + } - if (empty($name)) { - throw new Exception("Please enter your name."); - } + if (empty($name)) { + throw new Exception("Please enter your name."); + } - if (strlen($name) > 50) { - throw new Exception("Name must be up to 50 characters long."); - } + if (strlen($name) > 50) { + throw new Exception("Name must be up to 50 characters long."); + } - try { - $user = UserService::getInstance()->register($username, $name, $password, $city_id); + try { + $user = UserService::getInstance()->register($username, $name, $password, $city_id); - $_SESSION['username'] = $user->get('username'); - $_SESSION['is_admin'] = $user->get('is_admin'); + $_SESSION['username'] = $user->get('username'); + $_SESSION['is_admin'] = $user->get('is_admin'); - $responseData += $user->toResponse(); + $responseData += $user->toResponse(); - } catch (ServiceException $e) { - if ($e->getErrorType() == ServiceErrorType::ENTITY_EXIST) { - throw new Exception("User with that username already exists!"); - } + } catch (ServiceException $e) { + if ($e->getErrorType() == ServiceErrorType::ENTITY_EXIST) { + throw new Exception("User with that username already exists!"); } + } - 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'; + break; + default: + $strErrorDesc = 'Method not supported'; + $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + break; } - - - // 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 diff --git a/src/controller/UserController.php b/src/controller/UserController.php index 79fb19a..53fce51 100644 --- a/src/controller/UserController.php +++ b/src/controller/UserController.php @@ -8,116 +8,90 @@ require_once PROJECT_ROOT_PATH . "/services/UserService.php"; class UserController extends BaseController { - public function respond() + public function respond($requestMethod, &$responseData, &$strErrorDesc, &$strErrorHeader) { session_start(); - $strErrorDesc = ''; - $requestMethod = strtoupper($_SERVER["REQUEST_METHOD"]); $arrQueryStringParams = $this->getQueryStringParams(); $responseData = ['Method' => $requestMethod]; - try { - switch ($requestMethod) { - case 'PUT': - $body = $this->getBody(); - - $username = $body->username; - $name = $body->name; - $city_id = $body->city_id; - $password = $body->password; - $len_username = strlen($username); - $len_name = strlen($name); - $len_password = strlen($password); - - if (empty($username)) { - throw new Exception("Please enter username."); + switch ($requestMethod) { + case 'PUT': + $body = $this->getBody(); + + $username = $body->username; + $name = $body->name; + $city_id = $body->city_id; + $password = $body->password; + $len_username = strlen($username); + $len_name = strlen($name); + $len_password = strlen($password); + + if (empty($username)) { + throw new Exception("Please enter username."); + } + + if (preg_match('/\s/', $username)) { + throw new Exception("Username must not contain spaces."); + } + + if ($len_username > 50) { + throw new Exception("Username must be up to 50 characters long."); + } + + if ($len_username < 5) { + throw new Exception("Username must be at least 5 characters long."); + } + + if ($len_password > 0) { + if ($len_password > 50) { + throw new Exception("Password must be up to 50 characters long."); } - if (preg_match('/\s/', $username)) { - throw new Exception("Username must not contain spaces."); + if ($len_password < 6) { + throw new Exception("Password must be at least 6 characters long."); } + } - if ($len_username > 50) { - throw new Exception("Username must be up to 50 characters long."); + if (! empty($name)) { + // throw new Exception("Please enter your name."); + if ($len_name > 50) { + throw new Exception("Name must be up to 50 characters long."); } - if ($len_username < 5) { - throw new Exception("Username must be at least 5 characters long."); + if ($len_name < 5) { + throw new Exception("Name must be at least 5 characters long."); } + } + + try { + $user = UserService::getInstance()->edit($username, $name, $password, $city_id); - if ($len_password > 0) { - if ($len_password > 50) { - throw new Exception("Password must be up to 50 characters long."); - } + $_SESSION['username'] = $user->get('username'); + $_SESSION['is_admin'] = $user->get('is_admin'); - if ($len_password < 6) { - throw new Exception("Password must be at least 6 characters long."); - } - } - - if (! empty($name)) { - // throw new Exception("Please enter your name."); - if ($len_name > 50) { - throw new Exception("Name must be up to 50 characters long."); - } - - if ($len_name < 5) { - throw new Exception("Name must be at least 5 characters long."); - } - } - - try { - $user = UserService::getInstance()->edit($username, $name, $password, $city_id); - - $_SESSION['username'] = $user->get('username'); - $_SESSION['is_admin'] = $user->get('is_admin'); + $responseData += $user->toResponse(); - $responseData += $user->toResponse(); - - } catch (ServiceException $e) { - if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { - throw new Exception("User with that username already exists!"); - } - } - break; - case 'DELETE': - if (!UserService::getInstance()->isLoggedIn()) { - throw new Exception("You must be logged in!"); + } catch (ServiceException $e) { + if ($e->getErrorType() == ServiceErrorType::ENTITY_NOT_EXIST) { + throw new Exception("User with that username already exists!"); } - - UserService::getInstance()->delete(UserService::getInstance()->getUsername()); - UserService::getInstance()->logout(); - 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) - ); + } + break; + case 'DELETE': + if (!UserService::getInstance()->isLoggedIn()) { + throw new Exception("You must be logged in!"); + } + + UserService::getInstance()->delete(UserService::getInstance()->getUsername()); + UserService::getInstance()->logout(); + break; + + default: + $strErrorDesc = 'Method not supported'; + $strErrorHeader = 'HTTP/1.1 400 Bad Request'; + break; } - $this->sendOutput( - json_encode($responseData), - array('Content-Type: application/json', 'HTTP/1.1 200 OK') - ); } } \ No newline at end of file -- GitLab