Add protected reminder management
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
class Reminder
|
||||
{
|
||||
public static function findForUser(int $id, int $userId): ?array
|
||||
{
|
||||
$statement = Database::connection()->prepare(
|
||||
'SELECT r.*, h.title AS habit_title
|
||||
FROM reminders r
|
||||
INNER JOIN habits h ON h.id = r.habit_id
|
||||
WHERE r.id = :id AND h.user_id = :user_id
|
||||
LIMIT 1'
|
||||
);
|
||||
$statement->execute(['id' => $id, 'user_id' => $userId]);
|
||||
|
||||
$reminder = $statement->fetch();
|
||||
return $reminder ?: null;
|
||||
}
|
||||
|
||||
public static function allForUser(int $userId): array
|
||||
{
|
||||
$statement = Database::connection()->prepare(
|
||||
'SELECT r.*, h.title AS habit_title
|
||||
FROM reminders r
|
||||
INNER JOIN habits h ON h.id = r.habit_id
|
||||
WHERE h.user_id = :user_id
|
||||
ORDER BY h.title ASC, r.is_active DESC, r.weekday ASC, r.reminder_time ASC'
|
||||
);
|
||||
$statement->execute(['user_id' => $userId]);
|
||||
|
||||
return $statement->fetchAll();
|
||||
}
|
||||
|
||||
public static function createForHabit(int $habitId, int $userId, array $data): bool
|
||||
{
|
||||
$statement = Database::connection()->prepare(
|
||||
'INSERT INTO reminders (habit_id, reminder_time, weekday, is_active)
|
||||
SELECT id, :reminder_time, :weekday, :is_active
|
||||
FROM habits
|
||||
WHERE id = :habit_id AND user_id = :user_id'
|
||||
);
|
||||
$statement->execute([
|
||||
'habit_id' => $habitId,
|
||||
'user_id' => $userId,
|
||||
'reminder_time' => $data['reminder_time'],
|
||||
'weekday' => $data['weekday'],
|
||||
'is_active' => $data['is_active'],
|
||||
]);
|
||||
|
||||
return $statement->rowCount() === 1;
|
||||
}
|
||||
|
||||
public static function update(int $id, int $userId, array $data): bool
|
||||
{
|
||||
$statement = Database::connection()->prepare(
|
||||
'UPDATE reminders r
|
||||
INNER JOIN habits h ON h.id = r.habit_id
|
||||
SET r.reminder_time = :reminder_time,
|
||||
r.weekday = :weekday,
|
||||
r.is_active = :is_active
|
||||
WHERE r.id = :id AND h.user_id = :user_id'
|
||||
);
|
||||
|
||||
return $statement->execute([
|
||||
'id' => $id,
|
||||
'user_id' => $userId,
|
||||
'reminder_time' => $data['reminder_time'],
|
||||
'weekday' => $data['weekday'],
|
||||
'is_active' => $data['is_active'],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function delete(int $id, int $userId): bool
|
||||
{
|
||||
$statement = Database::connection()->prepare(
|
||||
'DELETE r
|
||||
FROM reminders r
|
||||
INNER JOIN habits h ON h.id = r.habit_id
|
||||
WHERE r.id = :id AND h.user_id = :user_id'
|
||||
);
|
||||
|
||||
return $statement->execute(['id' => $id, 'user_id' => $userId]);
|
||||
}
|
||||
|
||||
public static function toggle(int $id, int $userId): bool
|
||||
{
|
||||
$statement = Database::connection()->prepare(
|
||||
'UPDATE reminders r
|
||||
INNER JOIN habits h ON h.id = r.habit_id
|
||||
SET r.is_active = IF(r.is_active = 1, 0, 1)
|
||||
WHERE r.id = :id AND h.user_id = :user_id'
|
||||
);
|
||||
|
||||
return $statement->execute(['id' => $id, 'user_id' => $userId]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user