diff --git a/database/migrations/structure/20210510_2000_challenge.sql b/database/migrations/structure/20210510_2000_challenge.sql index 0b9a657..810d849 100644 --- a/database/migrations/structure/20210510_2000_challenge.sql +++ b/database/migrations/structure/20210510_2000_challenge.sql @@ -48,6 +48,6 @@ CREATE TABLE `guesses` ( PRIMARY KEY (`id`), KEY `user_id` (`user_id`), KEY `place_in_challenge_id` (`place_in_challenge_id`), - CONSTRAINT `guesses_user_id` FOREIGN KEY (`user_id`) REFERENCES `places` (`id`), + CONSTRAINT `guesses_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`), CONSTRAINT `guesses_place_in_challenge_id` FOREIGN KEY (`place_in_challenge_id`) REFERENCES `place_in_challenge` (`id`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; \ No newline at end of file diff --git a/src/Controller/GameFlowController.php b/src/Controller/GameFlowController.php index 2e33820..71ea558 100644 --- a/src/Controller/GameFlowController.php +++ b/src/Controller/GameFlowController.php @@ -151,7 +151,7 @@ class GameFlowController if (!isset($currentPlace) || $withHistory) { $withRelations = [User::class, PlaceInChallenge::class, Place::class]; - foreach ($this->guessRepository->getAllInChallenge($challenge, $withRelations) as $guess) { + foreach ($this->guessRepository->getAllInChallenge($challenge, $withRelations) as $guess) { $round = $guess->getPlaceInChallenge()->getRound(); if ($guess->getUser()->getId() === $userId) { diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 36397d8..2860d97 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -9,7 +9,10 @@ use MapGuesser\Interfaces\Response\IRedirect; use MapGuesser\OAuth\GoogleOAuth; use MapGuesser\PersistentData\PersistentDataManager; use MapGuesser\PersistentData\Model\User; +use MapGuesser\PersistentData\Model\UserInChallenge; +use MapGuesser\Repository\GuessRepository; use MapGuesser\Repository\UserConfirmationRepository; +use MapGuesser\Repository\UserInChallengeRepository; use MapGuesser\Repository\UserPasswordResetterRepository; use MapGuesser\Repository\UserPlayedPlaceRepository; use MapGuesser\Response\HtmlContent; @@ -29,6 +32,10 @@ class UserController implements ISecured private UserPlayedPlaceRepository $userPlayedPlaceRepository; + private UserInChallengeRepository $userInChallengeRepository; + + private GuessRepository $guessRepository; + public function __construct(IRequest $request) { $this->request = $request; @@ -36,6 +43,8 @@ class UserController implements ISecured $this->userConfirmationRepository = new UserConfirmationRepository(); $this->userPasswordResetterRepository = new UserPasswordResetterRepository(); $this->userPlayedPlaceRepository = new UserPlayedPlaceRepository(); + $this->userInChallengeRepository = new UserInChallengeRepository(); + $this->guessRepository = new GuessRepository(); } public function authorize(): bool @@ -209,6 +218,14 @@ class UserController implements ISecured $this->pdm->deleteFromDb($userPlayedPlace); } + foreach ($this->userInChallengeRepository->getAllByUser($user) as $userInChallenge) { + $this->pdm->deleteFromDb($userInChallenge); + } + + foreach ($this->guessRepository->getAllByUser($user) as $guess) { + $this->pdm->deleteFromDb($guess); + } + $this->pdm->deleteFromDb($user); \Container::$dbConnection->commit(); diff --git a/src/PersistentData/PersistentDataManager.php b/src/PersistentData/PersistentDataManager.php index 273eefc..b0274e4 100644 --- a/src/PersistentData/PersistentDataManager.php +++ b/src/PersistentData/PersistentDataManager.php @@ -60,7 +60,7 @@ class PersistentDataManager if (strpos($key, '__') == false) { $method = 'set' . str_replace('_', '', ucwords($key, '_')); - if (method_exists($model, $method)) { + if (method_exists($model, $method) && isset($value)) { $model->$method($value); } @@ -70,7 +70,7 @@ class PersistentDataManager $method = 'set' . str_replace('_', '', ucwords($key, '_')); - if (method_exists($model, $method)) { + if (method_exists($model, $method) && isset($value)) { $model->$method($value); } diff --git a/src/Repository/GuessRepository.php b/src/Repository/GuessRepository.php index 7c9b4df..eeca0a0 100644 --- a/src/Repository/GuessRepository.php +++ b/src/Repository/GuessRepository.php @@ -19,7 +19,15 @@ class GuessRepository $this->pdm = new PersistentDataManager(); } - public function getAllByUserAndChallenge(User $user, Challenge $challenge) : Generator + public function getAllByUser(User $user): Generator + { + $select = new Select(\Container::$dbConnection); + $select->where('user_id', '=', $user->getId()); + + yield from $this->pdm->selectMultipleFromDb($select, Guess::class); + } + + public function getAllByUserAndChallenge(User $user, Challenge $challenge): Generator { $select = new Select(\Container::$dbConnection); $select->innerJoin('place_in_challenge', ['place_in_challenge', 'id'], '=', ['guesses', 'place_in_challenge_id']); @@ -29,7 +37,7 @@ class GuessRepository yield from $this->pdm->selectMultipleFromDb($select, Guess::class); } - public function getByUserAndPlaceInChallenge(User $user, Challenge $challenge, Place $place) : ?Guess + public function getByUserAndPlaceInChallenge(User $user, Challenge $challenge, Place $place): ?Guess { $select = new Select(\Container::$dbConnection); $select->innerJoin('place_in_challenge', ['place_in_challenge', 'id'], '=', ['guesses', 'place_in_challenge_id']); @@ -57,7 +65,7 @@ class GuessRepository $necessaryRelations = [PlaceInChallenge::class]; $withRelations = array_unique(array_merge($withRelations, $necessaryRelations)); } - + $select = new Select(\Container::$dbConnection); $select->where('challenge_id', '=', $challenge->getId()); $select->orderBy('round');