public function find(int $id): Reminder {
$qb = $this->db->getQueryBuilder();
- $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
+ $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
public function findDueForUser(IUser $user, int $fileId): Reminder {
$qb = $this->db->getQueryBuilder();
- $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
+ $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
public function findAll() {
$qb = $this->db->getQueryBuilder();
- $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
+ $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
->from($this->getTableName())
->orderBy('due_date', 'ASC');
public function findAllForUser(IUser $user) {
$qb = $this->db->getQueryBuilder();
- $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
+ $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR)))
->orderBy('due_date', 'ASC');
public function findOverdue() {
$qb = $this->db->getQueryBuilder();
- $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
+ $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
->from($this->getTableName())
->where($qb->expr()->lt('due_date', $qb->createFunction('NOW()')))
->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)))
public function findNotified(?int $limit = null) {
$qb = $this->db->getQueryBuilder();
- $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
+ $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified')
->from($this->getTableName())
->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)))
->orderBy('due_date', 'ASC')
);
}
- public function create(IUser $user, int $fileId, DateTime $dueDate): void {
- $reminder = new Reminder();
- $reminder->setUserId($user->getUID());
- $reminder->setFileId($fileId);
- $reminder->setDueDate($dueDate);
- $reminder->setCreatedAt(new DateTime('now', new DateTimeZone('UTC')));
- $this->reminderMapper->insert($reminder);
+ public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): void {
+ $now = new DateTime('now', new DateTimeZone('UTC'));
+ try {
+ $reminder = $this->reminderMapper->findDueForUser($user, $fileId);
+ $reminder->setDueDate($dueDate);
+ $reminder->setUpdatedAt($now);
+ $this->reminderMapper->update($reminder);
+ } catch (DoesNotExistException $e) {
+ // Create new reminder if no reminder is found
+ $reminder = new Reminder();
+ $reminder->setUserId($user->getUID());
+ $reminder->setFileId($fileId);
+ $reminder->setDueDate($dueDate);
+ $reminder->setUpdatedAt($now);
+ $reminder->setCreatedAt($now);
+ $this->reminderMapper->insert($reminder);
+ }
}
/**