Merge pull request #41051 from nextcloud/feat/dav/absence-db
feat(dav): implement personal absence settings backendpull/41195/head
commit
d0db4d6ecf
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Db;
|
||||
|
||||
use JsonSerializable;
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
|
||||
/**
|
||||
* @method string getUserId()
|
||||
* @method void setUserId(string $userId)
|
||||
* @method string getFirstDay()
|
||||
* @method void setFirstDay(string $firstDay)
|
||||
* @method string getLastDay()
|
||||
* @method void setLastDay(string $lastDay)
|
||||
* @method string getStatus()
|
||||
* @method void setStatus(string $status)
|
||||
* @method string getMessage()
|
||||
* @method void setMessage(string $message)
|
||||
*/
|
||||
class Absence extends Entity implements JsonSerializable {
|
||||
protected string $userId = '';
|
||||
protected string $firstDay = '';
|
||||
protected string $lastDay = '';
|
||||
protected string $status = '';
|
||||
protected string $message = '';
|
||||
|
||||
public function __construct() {
|
||||
$this->addType('userId', 'string');
|
||||
$this->addType('firstDay', 'string');
|
||||
$this->addType('lastDay', 'string');
|
||||
$this->addType('status', 'string');
|
||||
$this->addType('message', 'string');
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array {
|
||||
return [
|
||||
'userId' => $this->userId,
|
||||
'firstDay' => $this->firstDay,
|
||||
'lastDay' => $this->lastDay,
|
||||
'status' => $this->status,
|
||||
'message' => $this->message,
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Db;
|
||||
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
|
||||
use OCP\AppFramework\Db\QBMapper;
|
||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||
use OCP\IDBConnection;
|
||||
|
||||
/**
|
||||
* @template-extends QBMapper<Absence>
|
||||
*/
|
||||
class AbsenceMapper extends QBMapper {
|
||||
public function __construct(IDBConnection $db) {
|
||||
parent::__construct($db, 'dav_absence', Absence::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DoesNotExistException
|
||||
* @throws \OCP\DB\Exception
|
||||
*/
|
||||
public function findByUserId(string $userId): Absence {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->select('*')
|
||||
->from($this->getTableName())
|
||||
->where($qb->expr()->eq(
|
||||
'user_id',
|
||||
$qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR),
|
||||
IQueryBuilder::PARAM_STR),
|
||||
);
|
||||
try {
|
||||
return $this->findEntity($qb);
|
||||
} catch (MultipleObjectsReturnedException $e) {
|
||||
// Won't happen as there is a unique index on user_id
|
||||
throw new \RuntimeException(
|
||||
'The impossible has happened! The query returned multiple absence settings for one user.',
|
||||
0,
|
||||
$e,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \OCP\DB\Exception
|
||||
*/
|
||||
public function deleteByUserId(string $userId): void {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->delete($this->getTableName())
|
||||
->where($qb->expr()->eq(
|
||||
'user_id',
|
||||
$qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR),
|
||||
IQueryBuilder::PARAM_STR),
|
||||
);
|
||||
$qb->executeStatement();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Migration;
|
||||
|
||||
use Closure;
|
||||
use OCP\DB\ISchemaWrapper;
|
||||
use OCP\DB\Types;
|
||||
use OCP\Migration\IOutput;
|
||||
use OCP\Migration\SimpleMigrationStep;
|
||||
|
||||
class Version1029Date20231004091403 extends SimpleMigrationStep {
|
||||
/**
|
||||
* @param IOutput $output
|
||||
* @param Closure(): ISchemaWrapper $schemaClosure
|
||||
* @param array $options
|
||||
* @return null|ISchemaWrapper
|
||||
*/
|
||||
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
|
||||
/** @var ISchemaWrapper $schema */
|
||||
$schema = $schemaClosure();
|
||||
|
||||
if (!$schema->hasTable('dav_absence')) {
|
||||
$table = $schema->createTable('dav_absence');
|
||||
$table->addColumn('id', Types::INTEGER, [
|
||||
'autoincrement' => true,
|
||||
'notnull' => true,
|
||||
'length' => 4,
|
||||
]);
|
||||
$table->addColumn('user_id', Types::STRING, [
|
||||
'notnull' => true,
|
||||
'length' => 64,
|
||||
]);
|
||||
$table->addColumn('first_day', Types::STRING, [
|
||||
'length' => 10,
|
||||
'notnull' => true,
|
||||
]);
|
||||
$table->addColumn('last_day', Types::STRING, [
|
||||
'length' => 10,
|
||||
'notnull' => true,
|
||||
]);
|
||||
$table->addColumn('status', Types::STRING, [
|
||||
'length' => 100,
|
||||
'notnull' => true,
|
||||
]);
|
||||
$table->addColumn('message', Types::TEXT, [
|
||||
'notnull' => true,
|
||||
]);
|
||||
$table->addUniqueIndex(['user_id'], 'dav_absence_uid_idx');
|
||||
} else {
|
||||
$table = $schema->getTable('dav_absence');
|
||||
}
|
||||
|
||||
if ($table->getPrimaryKey() === null) {
|
||||
$table->setPrimaryKey(['id'], 'dav_absence_id_idx');
|
||||
}
|
||||
|
||||
return $schema;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\DAV\Service;
|
||||
|
||||
use OCA\DAV\Db\Absence;
|
||||
use OCA\DAV\Db\AbsenceMapper;
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
|
||||
class AbsenceService {
|
||||
public function __construct(
|
||||
private AbsenceMapper $absenceMapper,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \OCP\DB\Exception
|
||||
*/
|
||||
public function createOrUpdateAbsence(
|
||||
string $userId,
|
||||
string $firstDay,
|
||||
string $lastDay,
|
||||
string $status,
|
||||
string $message,
|
||||
): Absence {
|
||||
try {
|
||||
$absence = $this->absenceMapper->findByUserId($userId);
|
||||
} catch (DoesNotExistException) {
|
||||
$absence = new Absence();
|
||||
}
|
||||
|
||||
$absence->setUserId($userId);
|
||||
$absence->setFirstDay($firstDay);
|
||||
$absence->setLastDay($lastDay);
|
||||
$absence->setStatus($status);
|
||||
$absence->setMessage($message);
|
||||
|
||||
if ($absence->getId() === null) {
|
||||
return $this->absenceMapper->insert($absence);
|
||||
}
|
||||
return $this->absenceMapper->update($absence);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \OCP\DB\Exception
|
||||
*/
|
||||
public function clearAbsence(string $userId): void {
|
||||
$this->absenceMapper->deleteByUserId($userId);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue