From 807b4d024f5937d8a57febf78777509c2691bf18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Vigh?= Date: Wed, 5 May 2021 19:16:54 +0200 Subject: [PATCH] extended database with UserPlayedPlace table for tracking when and what place a User played in game --- .../20210503_1040_user_played_place.sql | 12 +++ src/PersistentData/Model/UserPlayedPlace.php | 99 +++++++++++++++++++ src/Repository/UserPlayedPlaceRepository.php | 52 ++++++++++ 3 files changed, 163 insertions(+) create mode 100644 database/migrations/structure/20210503_1040_user_played_place.sql create mode 100644 src/PersistentData/Model/UserPlayedPlace.php create mode 100644 src/Repository/UserPlayedPlaceRepository.php diff --git a/database/migrations/structure/20210503_1040_user_played_place.sql b/database/migrations/structure/20210503_1040_user_played_place.sql new file mode 100644 index 0000000..afe60b3 --- /dev/null +++ b/database/migrations/structure/20210503_1040_user_played_place.sql @@ -0,0 +1,12 @@ +CREATE TABLE `user_played_place` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `user_id` int(10) unsigned NOT NULL, + `place_id` int(10) unsigned NOT NULL, + `last_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + `occurrences` int(10) NOT NULL DEFAULT 1, + PRIMARY KEY(`id`), + KEY `user_id` (`user_id`), + KEY `place_id` (`place_id`), + CONSTRAINT `user_played_place_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`), + CONSTRAINT `user_played_place_place_id` FOREIGN KEY (`place_id`) REFERENCES `places` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; diff --git a/src/PersistentData/Model/UserPlayedPlace.php b/src/PersistentData/Model/UserPlayedPlace.php new file mode 100644 index 0000000..d612476 --- /dev/null +++ b/src/PersistentData/Model/UserPlayedPlace.php @@ -0,0 +1,99 @@ + User::class, 'place' => Place::class]; + + private ?User $user = null; + + private ?int $userId = null; + + private ?Place $place = null; + + private ?int $placeId = null; + + private DateTime $lastTime; + + private int $occurrences = 1; + + public function setUser(User $user): void + { + $this->user = $user; + } + + public function setUserId(int $userId): void + { + $this->userId = $userId; + } + + public function setPlace(Place $place): void + { + $this->place = $place; + } + + public function setPlaceId(int $placeId): void + { + $this->placeId = $placeId; + } + + public function setLastTimeDate(DateTime $lastTime): void + { + $this->lastTime = $lastTime; + } + + public function setLastTime(string $lastTime): void + { + $this->lastTime = new DateTime($lastTime); + } + + public function setOccurrences(int $occurrences): void + { + $this->occurrences = $occurrences; + } + + public function incrementOccurrences(): void + { + $this->occurrences++; + } + + public function getUser(): ?User + { + return $this->user; + } + + public function getUserId(): ?int + { + return $this->userId; + } + + public function getPlace(): ?Place + { + return $this->place; + } + + public function getPlaceId(): ?int + { + return $this->placeId; + } + + public function getLastTimeDate(): DateTime + { + return $this->lastTime; + } + + public function getLastTime(): string + { + return $this->lastTime->format('Y-m-d H:i:s'); + } + + public function getOccurrences(): int + { + return $this->occurrences; + } +} diff --git a/src/Repository/UserPlayedPlaceRepository.php b/src/Repository/UserPlayedPlaceRepository.php new file mode 100644 index 0000000..0e497b7 --- /dev/null +++ b/src/Repository/UserPlayedPlaceRepository.php @@ -0,0 +1,52 @@ +pdm = new PersistentDataManager(); + } + + public function getByUser(User $user): Generator + { + $select = new Select(\Container::$dbConnection); + $select->where('user_id', '=', $user->getId()); + + yield from $this->pdm->selectMultipleFromDb($select, UserPlayedPlace::class); + } + + public function getByPlace(Place $place): Generator + { + $select = new Select(\Container::$dbConnection); + $select->where('place_id', '=', $place->getId()); + + yield from $this->pdm->selectMultipleFromDb($select, UserPlayedPlace::class); + } + + public function getAllByUser(User $user) : Generator + { + $select = new Select(\Container::$dbConnection); + $select->where('user_id', '=', $user->getId()); + + yield from $this->pdm->selectMultipleFromDb($select, UserPlayedPlace::class, true); + } + + public function getByUserIdAndPlaceId(int $userId, int $placeId) : ?UserPlayedPlace + { + $select = new Select(\Container::$dbConnection); + $select->where('user_id', '=', $userId); + $select->where('place_id', '=', $placeId); + + return $this->pdm->selectFromDb($select, UserPlayedPlace::class); + } +}