-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dav): implement personal absence settings backend #41051
Merged
+315
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved
Hide resolved
|
||
$table->setPrimaryKey(['id'], 'dav_absence_id_idx'); | ||
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved
Hide resolved
|
||
} | ||
|
||
return $schema; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: 1.28.1 would have been nice instead, to keep in sync with the NC version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
semantic > syntactic versioning 😉