Skip to content

Commit

Permalink
optimized queries to db + refactored orders by for ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
Lung committed Dec 9, 2023
1 parent 9d804ed commit 9892041
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 258 deletions.
8 changes: 4 additions & 4 deletions src/BankPayment/BankPaymentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace kissj\BankPayment;

use kissj\Event\Event;
use kissj\Orm\Order;
use kissj\Orm\Repository;

/**
* @method BankPayment[] findAll()
* @method BankPayment[] findBy(mixed[] $criteria, mixed[] $orderBy = [])
* @method BankPayment[] findBy(mixed[] $criteria, Order[] $orders = [])
* @method BankPayment|null findOneBy(mixed[] $criteria, mixed[] $orderBy = [])
* @method BankPayment getOneBy(mixed[] $criteria)
*/
Expand All @@ -23,7 +23,7 @@ public function getAllBankPaymentsOrdered(Event $event): array
{
return $this->findBy(
['event' => $event],
['id' => false],
[new Order('id', Order::DIRECTION_DESC)],
);
}

Expand All @@ -39,7 +39,7 @@ public function getBankPaymentsOrderedWithStatus(Event $event, string $status):
'event' => $event,
'status' => $status,
],
['id' => false],
[new Order('id', Order::DIRECTION_DESC)],
);
}

Expand Down
1 change: 0 additions & 1 deletion src/Event/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use kissj\Orm\Repository;

/**
* @method Event[] findAll()
* @method Event[] findBy(mixed[] $criteria)
* @method Event|null findOneBy(mixed[] $criteria)
* @method Event get(int $id)
Expand Down
2 changes: 1 addition & 1 deletion src/Export/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function paidContactDataToCSV(Event $event, User $adminUser): array
[UserStatus::Paid],
$event,
$adminUser,
new Order(Order::FILED_UPDATED_AT),
[new Order(Order::COLUMN_UPDATED_AT)],
);

$rows = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Orm/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Order
public const DIRECTION_ASC = 'ASC';
public const DIRECTION_DESC = 'DESC';

public const FILED_UPDATED_AT = 'updatedAt';
public const COLUMN_UPDATED_AT = 'updated_at';

public function __construct(
private readonly string $field,
Expand Down
82 changes: 14 additions & 68 deletions src/Orm/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function initEvents(): void

/**
* @param array<string,Entity|Relation|bool|int|float|string> $criteria
* @return bool
*/
public function isExisting(array $criteria): bool
{
Expand All @@ -41,12 +40,10 @@ public function get(int $id): Entity

/**
* @param array<string,Entity|Relation|bool|int|float|string> $criteria
* @param array<string,bool> $orderBy
* @return Entity
*/
public function getOneBy(array $criteria, array $orderBy = []): Entity
public function getOneBy(array $criteria): Entity
{
$entity = $this->findOneBy($criteria, $orderBy);
$entity = $this->findOneBy($criteria);
if ($entity === null) {
throw new \RuntimeException('Entity was not found.');
}
Expand All @@ -56,15 +53,15 @@ public function getOneBy(array $criteria, array $orderBy = []): Entity

/**
* @param array<string,Entity|Relation|bool|int|float|string> $criteria
* @param array<string,bool> $orderBy
* @param Order[] $orders
* @return Entity[]
*/
public function findBy(array $criteria, array $orderBy = []): array
public function findBy(array $criteria, array $orders = []): array
{
$qb = $this->createFluent();

$this->addConditions($qb, $criteria);
$this->addOrderBy($qb, $orderBy);
$this->addOrdersBy($qb, $orders);

// this little boi dumps sql query
// $qb->getConnection()->test($qb->_export(null, ['%ofs %lmt', null, null]));
Expand All @@ -79,40 +76,14 @@ public function findBy(array $criteria, array $orderBy = []): array
return $entities;
}

/**
* @param array<array<string,Entity|Relation|bool|int|float|string>> $criterias
* @param array<string,bool> $orderBy
* @return Entity[]
*/
public function findByMultiple(array $criterias, array $orderBy = []): array
{
$qb = $this->createFluent();

foreach ($criterias as $criterium) {
$this->addConditions($qb, $criterium);
}
$this->addOrderBy($qb, $orderBy);

$rows = $qb->fetchAll();
$entities = [];

foreach ($rows as $row) {
$entities[] = $this->createEntity($row);
}

return $entities;
}

/**
* @param array<string,Entity|Relation|bool|int|float|string> $criteria
* @param array<string,bool> $orderBy
* @return Entity|null
*/
public function findOneBy(array $criteria, array $orderBy = []): ?Entity
public function findOneBy(array $criteria): ?Entity
{
$qb = $this->createFluent();
$this->addConditions($qb, $criteria);
$this->addOrderBy($qb, $orderBy);

/** @var ?Row $row */
$row = $qb->fetch();

Expand All @@ -125,7 +96,6 @@ public function findOneBy(array $criteria, array $orderBy = []): ?Entity

/**
* @param array<string,Entity|Relation|bool|int|float|string> $criteria
* @return int
*/
public function countBy(array $criteria): int
{
Expand All @@ -138,9 +108,7 @@ public function countBy(array $criteria): int
}

/**
* @param Fluent $qb
* @param array<string,Entity|Relation|bool|int|float|string> $criteria
* @return void
*/
protected function addConditions(Fluent $qb, array $criteria): void
{
Expand Down Expand Up @@ -170,39 +138,17 @@ protected function addConditions(Fluent $qb, array $criteria): void
}

/**
* @param array<string,Entity|Relation|bool|int|float|string> $criteria
* @return int
*/
public function findIdBy(array $criteria): int
{
$qb = $this->createFluent();
$this->addConditions($qb, $criteria);
/** @var int $id */
$id = $qb->fetchSingle();

return $id;
}

/**
* @return Entity[]
* @param Order[] $orders
*/
public function findAll(): array
protected function addOrdersBy(Fluent $qb, array $orders): void
{
return $this->createEntities($this->createFluent()->fetchAll());
}
foreach($orders as $order) {
$qb->orderBy($order->getField());

/**
* @param Fluent $qb
* @param array<string,bool> $orderBy
* @return void
*/
protected function addOrderBy(Fluent $qb, array $orderBy): void
{
foreach ($orderBy as $order => $asc) {
if ($asc) {
$qb->orderBy($order)->asc();
if ($order->isOrderAsc()) {
$qb->asc();
} else {
$qb->orderBy($order)->desc();
$qb->desc();
}
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/Participant/Admin/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,43 +66,43 @@ public function showStats(
Event $event,
User $user,
): Response {
$orderByUpdatedAtDesc = new Order(Order::FILED_UPDATED_AT, Order::DIRECTION_DESC);
$orderByUpdatedAtDesc = new Order(Order::COLUMN_UPDATED_AT, Order::DIRECTION_DESC);

return $this->view->render($response, 'admin/stats-admin.twig', [
'paidPatrolLeaders' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::PatrolLeader],
[UserStatus::Paid],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
),
'paidTroopLeaders' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::TroopLeader],
[UserStatus::Paid],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
),
'paidTroopParticipants' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::TroopParticipant],
[UserStatus::Paid],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
),
'paidIsts' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::Ist],
[UserStatus::Paid],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
),
'paidGuests' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::Guest],
[UserStatus::Paid],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
),
'caIst' => $event->getEventType()->getContentArbiterIst(),
'caPl' => $event->getEventType()->getContentArbiterPatrolLeader(),
Expand All @@ -118,47 +118,47 @@ public function showOpen(
Event $event,
User $user,
): Response {
$orderByUpdatedAtDesc = new Order(Order::FILED_UPDATED_AT, Order::DIRECTION_DESC);
$orderByUpdatedAtDesc = new Order(Order::COLUMN_UPDATED_AT, Order::DIRECTION_DESC);

return $this->view->render($response, 'admin/open-admin.twig', [
'openPatrolLeaders' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::PatrolLeader],
[UserStatus::Open],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
true,
),
'openTroopLeaders' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::TroopLeader],
[UserStatus::Open],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
true,
),
'openTroopParticipants' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::TroopParticipant],
[UserStatus::Open],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
true,
),
'openIsts' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::Ist],
[UserStatus::Open],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
true,
),
'openGuests' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::Guest],
[UserStatus::Open],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
true,
),
'caIst' => $event->getEventType()->getContentArbiterIst(),
Expand All @@ -175,43 +175,43 @@ public function showApproving(
Event $event,
User $user,
): Response {
$orderByUpdatedAtDesc = new Order(Order::FILED_UPDATED_AT, Order::DIRECTION_DESC);
$orderByUpdatedAtDesc = new Order(Order::COLUMN_UPDATED_AT, Order::DIRECTION_DESC);

return $this->view->render($response, 'admin/approve-admin.twig', [
'closedPatrolLeaders' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::PatrolLeader],
[UserStatus::Closed],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
),
'closedTroopLeaders' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::TroopLeader],
[UserStatus::Closed],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
),
'closedTroopParticipants' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::TroopParticipant],
[UserStatus::Closed],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
),
'closedIsts' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::Ist],
[UserStatus::Closed],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
),
'closedGuests' => $this->participantRepository->getAllParticipantsWithStatus(
[ParticipantRole::Guest],
[UserStatus::Closed],
$event,
$user,
$orderByUpdatedAtDesc,
[$orderByUpdatedAtDesc],
),
'caIst' => $event->getEventType()->getContentArbiterIst(),
'caPl' => $event->getEventType()->getContentArbiterPatrolLeader(),
Expand Down
15 changes: 0 additions & 15 deletions src/Participant/Guest/GuestRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,4 @@
*/
class GuestRepository extends Repository
{
/**
* @param Event $event
* @return Guest[]
*/
public function findAllWithEvent(Event $event): array
{
$guests = [];
foreach ($this->findAll() as $participant) {
if ($participant instanceof Guest && $participant->getUserButNotNull()->event->id === $event->id) {
$guests[] = $participant;
}
}

return $guests;
}
}
Loading

0 comments on commit 9892041

Please sign in to comment.