From e14b9a8eeb38aecd0acc041b20914dc403fe2eee Mon Sep 17 00:00:00 2001 From: unknown <13521043@std.stei.itb.ac.id> Date: Sun, 22 Oct 2023 21:03:06 +0700 Subject: [PATCH] feat: average_rating --- src/controller/GymController.php | 144 +++++++++++++++------------- src/public/javascript/gym/rating.js | 21 +++- src/services/GymService.php | 31 ++++++ 3 files changed, 131 insertions(+), 65 deletions(-) diff --git a/src/controller/GymController.php b/src/controller/GymController.php index e20e3d1..97a43d6 100644 --- a/src/controller/GymController.php +++ b/src/controller/GymController.php @@ -208,90 +208,106 @@ class GymController extends BaseController } break; case 'PATCH': - $gymId = $arrQueryStringParams['gym_id']; - - if (!isset($gymId)) { - throw new Exception("Gym_id is not provided!"); - } + $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); + $oldGym = GymService::getInstance()->getById($gymId); - $pictureId = $videoId = null; + $pictureId = $videoId = null; - $pictureErr = ""; + $pictureErr = ""; - $pictureFile = $_FILES['gym-image']; - $pictureName = $pictureFile['name']; + $pictureFile = $_FILES['gym-image']; + $pictureName = $pictureFile['name']; - $expResult = explode('.', $pictureName); - $pictureFileExtension = strtolower(end($expResult)); + $expResult = explode('.', $pictureName); + $pictureFileExtension = strtolower(end($expResult)); - if ($pictureFileExtension != 'png') { - throw new Exception("Incorrect picture format"); - } + if ($pictureFileExtension != 'png') { + throw new Exception("Incorrect picture format"); + } - if ($pictureFile['size'] > 10000000) { - throw new Exception("Video file size too big"); - } + 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; + try { + $pictureId = MultimediaService::getInstance()->addPicture($pictureFile); + } catch (FileException $e) { + switch ($e->getErrorType()) { + default: + $pictureErr = "Mohon coba lagi"; + break; + } } - } - if ($pictureErr != "") { - throw new Exception($pictureErr); - } + if ($pictureErr != "") { + throw new Exception($pictureErr); + } - $videoErr = ""; + $videoErr = ""; - $videoFile = $_FILES['gym-video']; - $videoName = $videoFile['name']; + $videoFile = $_FILES['gym-video']; + $videoName = $videoFile['name']; - $expResult = explode('.', $videoName); - $videoFileExtension = strtolower(end($expResult)); + $expResult = explode('.', $videoName); + $videoFileExtension = strtolower(end($expResult)); - if ($videoFileExtension != 'mp4') { - throw new Exception("Incorrect video format"); - } + if ($videoFileExtension != 'mp4') { + throw new Exception("Incorrect video format"); + } - if ($videoFile['size'] > 10000000) { - throw new Exception("Video file size too big"); - } + 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; + try { + $videoId = MultimediaService::getInstance()->addVideo($videoFile); + } catch (FileException $e) { + switch ($e->getErrorType()) { + default: + $videoErr = "Mohon coba lagi"; + break; + } } - } - if ($videoErr != "") { - throw new Exception($videoErr); - } + 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!"); + 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; diff --git a/src/public/javascript/gym/rating.js b/src/public/javascript/gym/rating.js index e7c1f33..09e07d5 100644 --- a/src/public/javascript/gym/rating.js +++ b/src/public/javascript/gym/rating.js @@ -51,13 +51,30 @@ function getRating() { return parseInt(document.getElementById("drop-down-rate").value); } -// // take $ from php that calls this script + +function updateAvgRatingCall(gymId) { + const xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function () { + if (this.readyState === 4) { + if (this.status === 200 ){ + console.log(this.responseText); + } else { + const json = JSON.parse(this.responseText); + alert(json["error"]); + } + } + }; + xhr.open("PATCH", `/api/gym/`, true); + xhr.send(JSON.stringify({ gym_id: gymId, update_rating: 'true' })); +} + function newRatingCall(gymId) { const xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (this.readyState === 4) { if (this.status === 200 ){ console.log(this.responseText); + updateAvgRatingCall(gymId); window.location.reload(true); } else { const json = JSON.parse(this.responseText); @@ -77,6 +94,7 @@ function updateRatingCall(gymId) { if (this.readyState === 4) { if (this.status === 200 ){ console.log(this.responseText); + updateAvgRatingCall(gymId); window.location.reload(true); } else { const json = JSON.parse(this.responseText); @@ -95,6 +113,7 @@ function deleteRatingCall(gymId) { if (this.readyState === 4) { if (this.status === 200) { console.log(JSON.parse(this.responseText)); // console log is + updateAvgRatingCall(gymId); window.location.reload(true); } else { json = JSON.parse(this.responseText); diff --git a/src/services/GymService.php b/src/services/GymService.php index 98b6565..dc9f55f 100644 --- a/src/services/GymService.php +++ b/src/services/GymService.php @@ -269,4 +269,35 @@ class GymService extends BaseService ]); } + public function updateRating($gymId) + { + try{ + $pdo = new PDO("mysql:host=db;port=3306;dbname=gym_tracker", $_ENV['DB_USER'], $_ENV['DB_PASSWORD']); + + // check if the rating is not empty + $query = "SELECT * FROM ratings WHERE gym_id = :gym_id"; + $statement = $pdo->prepare($query); + $statement->bindParam(':gym_id', $gymId, PDO::PARAM_INT); + $statement->execute(); + $result_raw = $statement->fetchAll(PDO::FETCH_ASSOC); + if (empty($result_raw)) { + $query = "UPDATE gym SET average_rating = 0 WHERE gym_id = :gym_id"; + $statement = $pdo->prepare($query); + $statement->bindParam(':gym_id', $gymId, PDO::PARAM_INT); + $statement->execute(); + return; + } else { + $query = "UPDATE gym SET average_rating = (SELECT AVG(rating) FROM ratings WHERE gym_id = :gym_id) WHERE gym_id = :gym_id"; + + $statement = $pdo->prepare($query); + + $statement->execute([ + 'gym_id' => $gymId + ]); + } + } + catch (Exception $e) { + echo 'Caught exception: ', $e->getMessage(), "\n"; + } + } } \ No newline at end of file -- GitLab