58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
|
|
<?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(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|