Skip to content

Commit

Permalink
refactor: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
RiedleroD committed Sep 30, 2024
1 parent e8ac93f commit 48c820e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
60 changes: 44 additions & 16 deletions lbplanner/classes/model/reservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
*
Expand Down Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion lbplanner/services/slots/book_reservation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 48c820e

Please sign in to comment.