diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-11-29 11:03:15 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-11-29 11:03:15 +0100 |
commit | fa9096c0ab521aae45cab6c48a54290d14a221b9 (patch) | |
tree | 61466b792c2714848cfea00594456ac06c259c94 /src/Model/Event/SendUnits.php | |
parent | 3afcaef927391db23fe23c6c8c26b8960e8dae32 (diff) |
event tables
Diffstat (limited to 'src/Model/Event/SendUnits.php')
-rw-r--r-- | src/Model/Event/SendUnits.php | 91 |
1 files changed, 61 insertions, 30 deletions
diff --git a/src/Model/Event/SendUnits.php b/src/Model/Event/SendUnits.php index bf81031..ea94442 100644 --- a/src/Model/Event/SendUnits.php +++ b/src/Model/Event/SendUnits.php @@ -3,45 +3,52 @@ namespace App\Model\Event; use App\DB; -use App\Model\Event; -class SendUnits extends Event +class SendUnits extends BaseEvent { + public string $type; + + public int $amount; + public string $unit; + + public int $source; + public int $destination; + + public ?int $home = null; + public ?int $residence = null; + + public bool $isCanceled = false; + /** * @return void */ public function __invoke(): void { - $payload = json_decode($this->payload, true); - - if (isset($payload['cancel'])) { - if ($payload['type'] === 'SendBack') { - $payload['source'] = $payload['cancel']['home']; - $payload['destination'] = $payload['cancel']['residence']; + if ($this->isCanceled) { + if ($this->type === 'SendBack') { + $this->source = $this->home; + $this->destination = $this->residence; - $this->borrow($payload); + $this->borrow(); } } else { - if ($payload['type'] === 'Recall' || $payload['type'] === 'SendBack') { - $this->return($payload); + if ($this->type === 'Recall' || $this->type === 'SendBack') { + $this->return(); } - else if ($payload['type'] === 'Borrow') { - $this->borrow($payload); + else if ($this->type === 'Borrow') { + $this->borrow(); } - else if ($payload['type'] === 'Gift') { - $this->gift($payload); + else if ($this->type === 'Gift') { + $this->gift(); } } } - /** - * @param array $payload - */ - private function return(array $payload): void + private function return(): void { DB::query( <<<SQL @@ -50,14 +57,11 @@ class SendUnits extends Event on conflict (type, home_village_id, residence_village_id) do update set amount = village_units.amount+:amount SQL, - ['amount' => $payload['amount'], 'type' => $payload['unit'], 'id' => $payload['destination']] + ['amount' => $this->amount, 'type' => $this->unit, 'id' => $this->destination] ); } - /** - * @param array $payload - */ - private function borrow(array $payload): void + private function borrow(): void { DB::query( <<<SQL @@ -66,14 +70,11 @@ class SendUnits extends Event on conflict (type, home_village_id, residence_village_id) do update set amount = village_units.amount+:amount SQL, - ['amount' => $payload['amount'], 'type' => $payload['unit'], 'home' => $payload['source'], 'residence' => $payload['destination']] + ['amount' => $this->amount, 'type' => $this->unit, 'home' => $this->source, 'residence' => $this->destination] ); } - /** - * @param array $payload - */ - private function gift(array $payload): void + private function gift(): void { DB::query( <<<SQL @@ -82,7 +83,37 @@ class SendUnits extends Event on conflict (type, home_village_id, residence_village_id) do update set amount = village_units.amount+:amount SQL, - ['amount' => $payload['amount'], 'type' => $payload['unit'], 'home' => $payload['destination'], 'residence' => $payload['destination']] + ['amount' => $this->amount, 'type' => $this->unit, 'home' => $this->destination, 'residence' => $this->residence] ); } + + public function dbInsert(): void + { + DB::query( + 'insert into events (time, village_id) VALUES (:time, :village_id)', + ['time' => $this->event->time->format('c'), 'village_id' => $this->event->villageId] + ); + + DB::query( + <<<SQL + insert into events_send_units (event_id, type, amount, unit, source, destination, home, residence, is_canceled) + VALUES (:event_id, :type, :amount, :unit, :source, :destination, :home, :residence, :is_canceled) + SQL, + [ + 'event_id' => DB::$connection->lastInsertId(), + 'type' => $this->type, + 'amount' => $this->amount, + 'unit' => $this->unit, + 'source' => $this->source, 'destination' => $this->destination, + 'home' => $this->home, 'residence' => $this->residence, + 'is_canceled' => $this->isCanceled ?: 0, // @see https://www.php.net/manual/de/pdostatement.execute.php#126013 + ] + ); + } + + public function dbDelete(): void + { + DB::query('delete from events where id=:id', ['id' => $this->eventId]); + DB::query('delete from events_send_units where id=:id', ['id' => $this->id]); + } } |