From 67114211ec3ae79bedc635aa0da9c610bf028fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 1 May 2023 19:19:39 +0200 Subject: [PATCH] RVRNEXT-6 add model and repository for transactions --- src/PersistentData/Model/Transaction.php | 160 +++++++++++++++++++++++ src/Repository/TransactionRepository.php | 44 +++++++ 2 files changed, 204 insertions(+) create mode 100644 src/PersistentData/Model/Transaction.php create mode 100644 src/Repository/TransactionRepository.php diff --git a/src/PersistentData/Model/Transaction.php b/src/PersistentData/Model/Transaction.php new file mode 100644 index 0000000..a4a9b48 --- /dev/null +++ b/src/PersistentData/Model/Transaction.php @@ -0,0 +1,160 @@ + Community::class, + 'currency' => Currency::class, + 'payer_user' => User::class, + 'payee_user' => User::class + ]; + + private ?Community $community = null; + + private int $communityId; + + private ?Currency $currency = null; + + private int $currencyId; + + private ?User $payerUser = null; + + private int $payerUserId; + + private ?User $payeeUser = null; + + private ?int $payeeUserId = null; + + private string $description = ''; + + private float $sum = 0.0; + + private DateTime $time; + + public function setCommunity(Community $community): void + { + $this->community = $community; + } + + public function setCommunityId(int $communityId): void + { + $this->communityId = $communityId; + } + + public function setCurrency(Currency $currency): void + { + $this->currency = $currency; + } + + public function setCurrencyId(int $currencyId): void + { + $this->currencyId = $currencyId; + } + + public function setPayerUser(User $payerUser): void + { + $this->payerUser = $payerUser; + } + + public function setPayerUserId(int $payerUserId): void + { + $this->payerUserId = $payerUserId; + } + + public function setPayeeUser(?User $payeeUser): void + { + $this->payeeUser = $payeeUser; + } + + public function setPayeeUserId(?int $payeeUserId): void + { + $this->payeeUserId = $payeeUserId; + } + + public function setDescription(string $description): void + { + $this->description = $description; + } + + public function setSum(float $sum): void + { + $this->sum = $sum; + } + + public function setTimeDate(DateTime $time): void + { + $this->time = $time; + } + + public function setTime(string $time): void + { + $this->time = new DateTime($time); + } + + public function getCommunity(): ?Community + { + return $this->community; + } + + public function getCommunityId(): int + { + return $this->communityId; + } + + public function getCurrency(): ?Currency + { + return $this->currency; + } + + public function getCurrencyId(): int + { + return $this->currencyId; + } + + public function getPayerUser(): ?User + { + return $this->payerUser; + } + + public function getPayerUserId(): int + { + return $this->payerUserId; + } + + public function getPayeeUser(): ?User + { + return $this->payeeUser; + } + + public function getPayeeUserId(): ?int + { + return $this->payeeUserId; + } + + public function getDescription(): string + { + return $this->description; + } + + public function getSum(): float + { + return $this->sum; + } + + public function getTimeDate(): DateTime + { + return $this->time; + } + + public function getTime(): string + { + return $this->time->format('Y-m-d H:i:s'); + } +} diff --git a/src/Repository/TransactionRepository.php b/src/Repository/TransactionRepository.php new file mode 100644 index 0000000..4ef85bc --- /dev/null +++ b/src/Repository/TransactionRepository.php @@ -0,0 +1,44 @@ +selectFromDbById($id, Transaction::class); + } + + public function getAllByCommunity(Community $community, bool $useRelations = false, array $withRelations = []): Generator + { + $select = $this->selectAllByCommunity($community); + + yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations); + } + + public function countAllByCommunity(Community $community): int + { + return $this->selectAllByCommunity($community)->count(); + } + + public function getPagedByCommunity(Community $community, int $start, int $limit, bool $useRelations = false, array $withRelations = []): Generator + { + $select = new Select(Container::$dbConnection); + $select->where('community_id', '=', $community->getId()); + $select->orderBy('time', 'DESC'); + $select->limit($limit, $start); + + yield from Container::$persistentDataManager->selectMultipleFromDb($select, Transaction::class, $useRelations, $withRelations); + } + + private function selectAllByCommunity(Community $community) + { + $select = new Select(Container::$dbConnection, Transaction::getTable()); + $select->where('community_id', '=', $community->getId()); + return $select; + } +}