Skip to content

Commit 48c820e

Browse files
committed
refactor: minor improvements
1 parent e8ac93f commit 48c820e

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

lbplanner/classes/model/reservation.php

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,29 @@ public function __construct(int $id, int $slotid, DateTimeImmutable $date, int $
8787
$this->slot = null;
8888
}
8989

90+
/**
91+
* Mark the object as freshly created and sets the new ID
92+
* @param int $id the new ID after insertint into the DB
93+
* @param slot $slot the cached slot object
94+
*/
95+
public function set_fresh(int $id, ?slot $slot) {
96+
assert($this->id === 0);
97+
assert($id !== 0);
98+
$this->id = $id;
99+
if (!is_null($slot)) {
100+
$this->set_slot($slot);
101+
}
102+
}
103+
104+
/**
105+
* sets the cached slot object (mainly for deduplicating DB requests)
106+
* @param slot $slot the cached slot object
107+
*/
108+
public function set_slot(slot $slot) {
109+
assert($this->slotid === $slot->id);
110+
$this->slot = $slot;
111+
}
112+
90113
/**
91114
* Returns the associated slot.
92115
*
@@ -100,22 +123,6 @@ public function get_slot(): slot {
100123
return $this->slot;
101124
}
102125

103-
/**
104-
* Prepares data for the DB endpoint.
105-
*
106-
* @return object a representation of this reservation and its data
107-
*/
108-
public function prepare_for_db(): object {
109-
$obj = new \stdClass();
110-
111-
$obj->slotid = $this->slotid;
112-
$obj->date = $this->date;
113-
$obj->userid = $this->userid;
114-
$obj->reserverid = $this->reserverid;
115-
116-
return $obj;
117-
}
118-
119126
/**
120127
* Calculates the exact time and date this reservation is supposed to start
121128
*
@@ -144,6 +151,27 @@ public function get_datetime_end(): DateTimeImmutable {
144151
return $this->datetime_end;
145152
}
146153

154+
/**
155+
* Prepares data for the DB endpoint.
156+
* doesn't set ID if it's 0
157+
*
158+
* @return object a representation of this reservation and its data
159+
*/
160+
public function prepare_for_db(): object {
161+
$obj = new \stdClass();
162+
163+
$obj->slotid = $this->slotid;
164+
$obj->date = $this->date;
165+
$obj->userid = $this->userid;
166+
$obj->reserverid = $this->reserverid;
167+
168+
if ($this->id !== 0) {
169+
$obj->id = $this->id;
170+
}
171+
172+
return $obj;
173+
}
174+
147175
/**
148176
* Prepares data for the API endpoint.
149177
*

lbplanner/services/slots/book_reservation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static function book_reservation(int $slotid, string $date, int $userid):
128128
$reservation = new reservation(0, $slotid, $dateobj, $userid, $USER->id);
129129

130130
$id = $DB->insert_record(slot_helper::TABLE_RESERVATIONS, $reservation->prepare_for_db());
131-
$reservation->id = $id;
131+
$reservation->set_fresh($id, $slot);
132132

133133
// TODO: if userid!=USER->id → send notif to the user that the supervisor booked a reservation for them
134134

0 commit comments

Comments
 (0)