From 48c820e86dba4ea1dd526a36dd5111a5ec825715 Mon Sep 17 00:00:00 2001 From: Riedler Date: Mon, 19 Aug 2024 20:23:50 +0200 Subject: [PATCH] refactor: minor improvements --- lbplanner/classes/model/reservation.php | 60 ++++++++++++++----- lbplanner/services/slots/book_reservation.php | 2 +- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/lbplanner/classes/model/reservation.php b/lbplanner/classes/model/reservation.php index f23dc56..82865c6 100644 --- a/lbplanner/classes/model/reservation.php +++ b/lbplanner/classes/model/reservation.php @@ -87,6 +87,29 @@ public function __construct(int $id, int $slotid, DateTimeImmutable $date, int $ $this->slot = null; } + /** + * Mark the object as freshly created and sets the new ID + * @param int $id the new ID after insertint into the DB + * @param slot $slot the cached slot object + */ + public function set_fresh(int $id, ?slot $slot) { + assert($this->id === 0); + assert($id !== 0); + $this->id = $id; + if (!is_null($slot)) { + $this->set_slot($slot); + } + } + + /** + * sets the cached slot object (mainly for deduplicating DB requests) + * @param slot $slot the cached slot object + */ + public function set_slot(slot $slot) { + assert($this->slotid === $slot->id); + $this->slot = $slot; + } + /** * Returns the associated slot. * @@ -100,22 +123,6 @@ public function get_slot(): slot { return $this->slot; } - /** - * Prepares data for the DB endpoint. - * - * @return object a representation of this reservation and its data - */ - public function prepare_for_db(): object { - $obj = new \stdClass(); - - $obj->slotid = $this->slotid; - $obj->date = $this->date; - $obj->userid = $this->userid; - $obj->reserverid = $this->reserverid; - - return $obj; - } - /** * Calculates the exact time and date this reservation is supposed to start * @@ -144,6 +151,27 @@ public function get_datetime_end(): DateTimeImmutable { return $this->datetime_end; } + /** + * Prepares data for the DB endpoint. + * doesn't set ID if it's 0 + * + * @return object a representation of this reservation and its data + */ + public function prepare_for_db(): object { + $obj = new \stdClass(); + + $obj->slotid = $this->slotid; + $obj->date = $this->date; + $obj->userid = $this->userid; + $obj->reserverid = $this->reserverid; + + if ($this->id !== 0) { + $obj->id = $this->id; + } + + return $obj; + } + /** * Prepares data for the API endpoint. * diff --git a/lbplanner/services/slots/book_reservation.php b/lbplanner/services/slots/book_reservation.php index ca0a649..af538fa 100644 --- a/lbplanner/services/slots/book_reservation.php +++ b/lbplanner/services/slots/book_reservation.php @@ -128,7 +128,7 @@ public static function book_reservation(int $slotid, string $date, int $userid): $reservation = new reservation(0, $slotid, $dateobj, $userid, $USER->id); $id = $DB->insert_record(slot_helper::TABLE_RESERVATIONS, $reservation->prepare_for_db()); - $reservation->id = $id; + $reservation->set_fresh($id, $slot); // TODO: if userid!=USER->id → send notif to the user that the supervisor booked a reservation for them