...
This commit is contained in:
parent
1db373a03d
commit
9bcbd3e194
118 changed files with 1116 additions and 155 deletions
57
motorradig/src/Models/Booking.php
Normal file
57
motorradig/src/Models/Booking.php
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Model;
|
||||
|
||||
class Booking extends Model
|
||||
{
|
||||
protected static string $table = 'bookings';
|
||||
|
||||
public static function countForTour(int $tourId): int
|
||||
{
|
||||
return (int) db()->fetchValue('SELECT COUNT(*) FROM `bookings` WHERE tour_id = ?', [$tourId]);
|
||||
}
|
||||
|
||||
public static function forUser(int $tourId, int $userId): ?static
|
||||
{
|
||||
$row = db()->fetchOne('SELECT * FROM `bookings` WHERE tour_id = ? AND user_id = ?', [$tourId, $userId]);
|
||||
|
||||
return $row === null ? null : static::fromRow($row);
|
||||
}
|
||||
|
||||
public static function forTour(int $tourId): array
|
||||
{
|
||||
$rows = db()->fetchAll('SELECT * FROM `bookings` WHERE tour_id = ?', [$tourId]);
|
||||
|
||||
return array_map(fn(array $row) => static::fromRow($row), $rows);
|
||||
}
|
||||
|
||||
public static function deleteForUser(int $tourId, int $userId): void
|
||||
{
|
||||
db()->execute('DELETE FROM `bookings` WHERE tour_id = ? AND user_id = ?', [$tourId, $userId]);
|
||||
}
|
||||
|
||||
/** Bucht einen Raum oder aktualisiert die Raumbuchung eines Benutzers. */
|
||||
public static function joinRoom(int $tourId, int $userId, int $roomId): void
|
||||
{
|
||||
$booking = static::forUser($tourId, $userId);
|
||||
|
||||
if ($booking !== null) {
|
||||
$booking->room_id = $roomId;
|
||||
$booking->save();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static::create([
|
||||
'tour_id' => $tourId,
|
||||
'user_id' => $userId,
|
||||
'room_id' => $roomId,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue