Files
Reservair/includes/Repository/RsvTimetableRepository.php
T

85 lines
2.4 KiB
PHP
Raw Normal View History

2026-06-11 19:03:29 +02:00
<?php
use Reservair\Database\Db;
class RsvTimetableRepository {
private string $table;
public function __construct() {
$this->table = Db::prefix() . 'rsv_timetable';
}
public function get_all(?int $limit = null, int $skip = 0): array {
if ($limit === null) {
return Db::get_results("SELECT * FROM {$this->table} ORDER BY id");
}
return Db::get_results(
"SELECT * FROM {$this->table} ORDER BY id LIMIT %d OFFSET %d",
[$limit, $skip]
);
}
public function count_all(): int {
return (int) Db::get_var("SELECT COUNT(*) FROM {$this->table}");
}
public function get(int $id): ?RsvTimetable {
$row = Db::get_row(
"SELECT * FROM {$this->table} WHERE id = %d",
[$id],
ARRAY_A
);
if ($row === null) {
return null;
}
return new RsvTimetable($row);
}
public function create(RsvTimetable $timetable): int {
return Db::insert($this->table, [
'name' => $timetable->name,
'block_size' => $timetable->block_size,
'maintainer_email' => $timetable->maintainer_email,
]);
}
public function update(int $id, RsvTimetable $timetable): int {
return Db::update(
$this->table,
[
'name' => $timetable->name,
'block_size' => $timetable->block_size,
'maintainer_email' => $timetable->maintainer_email,
2026-06-16 19:33:55 +02:00
'google_calendar_id' => $timetable->google_calendar_id,
2026-06-11 19:03:29 +02:00
],
['id' => $id]
);
}
public function get_all_maintainer_emails(): array {
return Db::get_col(
"SELECT DISTINCT maintainer_email FROM {$this->table}
WHERE maintainer_email IS NOT NULL AND maintainer_email != ''"
);
}
public function get_maintainer_email(int $id): ?string {
return Db::get_var(
"SELECT maintainer_email FROM {$this->table} WHERE id = %d",
[$id]
) ?: null;
}
public function set_google_calendar_id(int $id, ?string $calendar_id): void {
Db::update(
$this->table,
['google_calendar_id' => $calendar_id],
['id' => $id]
);
}
public function delete(int $id): int {
return Db::delete($this->table, ['id' => $id]);
}
}