Skip to content

Commit

Permalink
get rid of boolean table columns
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Sep 23, 2024
1 parent 2480853 commit 036bf2b
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 69 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<id>gpxpod</id>
<name>GpxPod</name>
<summary> </summary><description> </description>
<version>6.0.0</version>
<version>7.0.0</version>
<licence>agpl</licence>
<author>Julien Veyssier (@julien-nc)</author>
<namespace>GpxPod</namespace>
Expand Down
40 changes: 20 additions & 20 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public function index(): TemplateResponse {
'path' => $dir['path'],
'isOpen' => $dir['isOpen'],
'sortOrder' => $dir['sortOrder'],
'sortAsc' => $dir['sortAsc'],
'recursive' => $dir['recursive'],
'sortAscending' => $dir['sortAscending'],
'displayRecursive' => $dir['displayRecursive'],
'tracks' => [],
'pictures' => [],
'loading' => false,
Expand Down Expand Up @@ -292,8 +292,8 @@ private function getPublicTemplate(IShare $share, ?string $password, ?string $pa
'path' => $this->l10n->t('Public link'),
'isOpen' => true,
'sortOrder' => 0,
'sortAsc' => true,
'recursive' => false,
'sortAscending' => true,
'displayRecursive' => false,
'tracks' => [
'0' => $this->getPublicTrack($share, $shareNode),
],
Expand All @@ -315,8 +315,8 @@ private function getPublicTemplate(IShare $share, ?string $password, ?string $pa
'path' => $shareNode->getName(),
'isOpen' => true,
'sortOrder' => 0,
'sortAsc' => true,
'recursive' => false,
'sortAscending' => true,
'displayRecursive' => false,
'tracks' => $this->getPublicDirectoryTracks($share, $shareNode),
'pictures' => [],
'loading' => false,
Expand All @@ -338,8 +338,8 @@ private function getPublicTemplate(IShare $share, ?string $password, ?string $pa
'path' => $this->l10n->t('Public link'),
'isOpen' => true,
'sortOrder' => 0,
'sortAsc' => true,
'recursive' => false,
'sortAscending' => true,
'displayRecursive' => false,
'tracks' => [
'0' => $this->getPublicTrack($share, $targetNode),
],
Expand All @@ -359,8 +359,8 @@ private function getPublicTemplate(IShare $share, ?string $password, ?string $pa
'path' => $shareNode->getName() . '/' . ltrim($path, '/'),
'isOpen' => true,
'sortOrder' => 0,
'sortAsc' => true,
'recursive' => false,
'sortAscending' => true,
'displayRecursive' => false,
'tracks' => $this->getPublicDirectoryTracks($share, $targetNode),
'pictures' => [],
'loading' => false,
Expand Down Expand Up @@ -675,17 +675,17 @@ private function getDefaultSettings(array $settings): array {
* @param int $id
* @param bool $isOpen
* @param int|null $sortOrder
* @param bool|null $sortAsc
* @param bool|null $recursive
* @param bool|null $sortAscending
* @param bool|null $displayRecursive
* @return DataResponse
* @throws \OCP\DB\Exception
*/
#[NoAdminRequired]
public function updateDirectory(
int $id, ?bool $isOpen = null, ?int $sortOrder = null,
?bool $sortAsc = null, ?bool $recursive = null,
?bool $sortAscending = null, ?bool $displayRecursive = null,
): DataResponse {
$this->directoryMapper->updateDirectory($id, $this->userId, null, $isOpen, $sortOrder, $sortAsc, $recursive);
$this->directoryMapper->updateDirectory($id, $this->userId, null, $isOpen, $sortOrder, $sortAscending, $displayRecursive);
return new DataResponse();
}

Expand Down Expand Up @@ -717,16 +717,16 @@ public function updateDirectoryTracks(int $id, ?bool $isEnabled = null): DataRes
* no CSRF because this can be called from the files app
*
* @param string $path
* @param bool $recursive
* @param bool $displayRecursive
* @return DataResponse
* @throws NoUserException
* @throws NotFoundException
* @throws NotPermittedException
*/
#[NoAdminRequired]
#[NoCSRFRequired]
public function addDirectory(string $path, bool $recursive = false): DataResponse {
if ($recursive) {
public function addDirectory(string $path, bool $displayRecursive = false): DataResponse {
if ($displayRecursive) {
return $this->addDirectoryRecursive($path);
}
$userFolder = $this->root->getUserFolder($this->userId);
Expand Down Expand Up @@ -1004,7 +1004,7 @@ public function getTrackMarkersJson(int $id, string $directoryPath, bool $proces
return new DataResponse(['error' => 'This directory is not a directory'], Http::STATUS_BAD_REQUEST);
}

$recursive = $dbDir->getRecursive();
$displayRecursive = $dbDir->getDisplayRecursive() === 1;
$optionValues = $this->processService->getSharedMountedOptionValue($this->userId);
$sharedAllowed = $optionValues['sharedAllowed'];
$mountedAllowed = $optionValues['mountedAllowed'];
Expand All @@ -1022,7 +1022,7 @@ public function getTrackMarkersJson(int $id, string $directoryPath, bool $proces
$filesByExtension[$ext] = [];
}

if ($recursive) {
if ($displayRecursive) {
$extensions = array_keys(ConversionService::fileExtToGpsbabelFormat);
$files = $this->processService->searchFilesWithExt($userFolder->get($directoryPath), $sharedAllowed, $mountedAllowed, $extensions);
foreach ($files as $file) {
Expand All @@ -1048,7 +1048,7 @@ public function getTrackMarkersJson(int $id, string $directoryPath, bool $proces
$this->conversionService->convertFiles($userFolder, $directoryPath, $this->userId, $filesByExtension);

// PROCESS gpx files and fill DB
$this->processService->processGpxFiles($this->userId, $dbDir->getId(), $sharedAllowed, $mountedAllowed, $processAll, $recursive);
$this->processService->processGpxFiles($this->userId, $dbDir->getId(), $sharedAllowed, $mountedAllowed, $processAll, $displayRecursive);

// build tracks array
$dbTracks = $this->trackMapper->getDirectoryTracksOfUser($this->userId, $dbDir->getId());
Expand Down
20 changes: 10 additions & 10 deletions lib/Db/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@
* @method void setIsOpen(int $isOpen)
* @method int getSortOrder()
* @method void setSortOrder(int $sortOrder)
* @method bool|null getSortAsc()
* @method void setSortAsc(bool|null $sortAsc)
* @method bool|null getRecursive()
* @method void setRecursive(bool|null $recursive)
* @method int getSortAscending()
* @method void setSortAscending(int $sortAscending)
* @method int getDisplayRecursive()
* @method void setDisplayRecursive(int $displayRecursive)
*/
class Directory extends Entity implements \JsonSerializable {

protected string $user = '';
protected string $path = '';
protected int $isOpen = 0;
protected int $sortOrder = 0;
protected ?bool $sortAsc = null;
protected ?bool $recursive = false;
protected int $sortAscending = 1;
protected int $displayRecursive = 0;

public function __construct() {
$this->addType('user', 'string');
$this->addType('path', 'string');
$this->addType('is_open', 'integer');
$this->addType('sort_order', 'integer');
$this->addType('sort_asc', 'boolean');
$this->addType('recursive', 'boolean');
$this->addType('sort_ascending', 'boolean');
$this->addType('display_recursive', 'boolean');
}

#[\ReturnTypeWillChange]
Expand All @@ -67,8 +67,8 @@ public function jsonSerialize() {
'path' => $this->getPath(),
'isOpen' => $this->getIsOpen() === 1,
'sortOrder' => $this->getSortOrder(),
'sortAsc' => $this->getSortAsc(),
'recursive' => $this->getRecursive(),
'sortAscending' => $this->getSortAscending() === 1,
'displayRecursive' => $this->getDisplayRecursive() === 1,
];
}
}
30 changes: 16 additions & 14 deletions lib/Db/DirectoryMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ public function deleteAndCleanup(int $id, string $userId): int {
* @param string $user
* @param bool $isOpen
* @param int $sortOrder
* @param bool $sortAsc
* @param bool $recursive
* @param bool $sortAscending
* @param bool $displayRecursive
* @return Directory
* @throws Exception
*/
public function createDirectory(string $path, string $user, bool $isOpen = false, int $sortOrder = 0,
bool $sortAsc = true, bool $recursive = false): Directory {
public function createDirectory(
string $path, string $user, bool $isOpen = false, int $sortOrder = 0,
bool $sortAscending = true, bool $displayRecursive = false
): Directory {
try {
// do not create if one with same path/userId already exists
$dir = $this->getDirectoryOfUserByPath($path, $user);
Expand All @@ -178,8 +180,8 @@ public function createDirectory(string $path, string $user, bool $isOpen = false
$dir->setUser($user);
$dir->setIsOpen($isOpen ? 1 : 0);
$dir->setSortOrder($sortOrder);
$dir->setSortAsc($sortAsc);
$dir->setRecursive($recursive);
$dir->setSortAscending($sortAscending ? 1 : 0);
$dir->setDisplayRecursive($displayRecursive ? 1 : 0);
/** @var Directory $directory */
$createdDirectory = $this->insert($dir);
return $createdDirectory;
Expand All @@ -191,17 +193,17 @@ public function createDirectory(string $path, string $user, bool $isOpen = false
* @param string|null $path
* @param bool|null $isOpen
* @param int|null $sortOrder
* @param bool|null $sortAsc
* @param bool|null $recursive
* @param bool|null $sortAscending
* @param bool|null $displayRecursive
* @return Directory|null
* @throws Exception
*/
public function updateDirectory(
int $id, string $userId,
?string $path = null, ?bool $isOpen = null, ?int $sortOrder = null,
?bool $sortAsc = null, ?bool $recursive = null,
?bool $sortAscending = null, ?bool $displayRecursive = null,
): ?Directory {
if ($path === null && $isOpen === null && $sortOrder === null && $sortAsc === null && $recursive === null) {
if ($path === null && $isOpen === null && $sortOrder === null && $sortAscending === null && $displayRecursive === null) {
return null;
}
try {
Expand All @@ -218,11 +220,11 @@ public function updateDirectory(
if ($sortOrder !== null) {
$dir->setSortOrder($sortOrder);
}
if ($sortAsc !== null) {
$dir->setSortAsc($sortAsc);
if ($sortAscending !== null) {
$dir->setSortAscending($sortAscending ? 1 : 0);
}
if ($recursive !== null) {
$dir->setRecursive($recursive);
if ($displayRecursive !== null) {
$dir->setDisplayRecursive($displayRecursive ? 1 : 0);
}
/** @var Directory $directory */
$updatedDirectory = $this->update($dir);
Expand Down
71 changes: 71 additions & 0 deletions lib/Migration/Version070000Date20240923181142.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace OCA\GpxPod\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

class Version070000Date20240923181142 extends SimpleMigrationStep {

public function __construct() {
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
* @return null|ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
$schemaChanged = false;

$table = $schema->getTable('gpxpod_directories');
if (!$table->hasColumn('sort_ascending')) {
$table->addColumn('sort_ascending', Types::SMALLINT, [
'notnull' => true,
'default' => 1,
]);
$schemaChanged = true;
}
if (!$table->hasColumn('display_recursive')) {
$table->addColumn('display_recursive', Types::SMALLINT, [
'notnull' => true,
'default' => 0,
]);
$schemaChanged = true;
}
if ($table->hasColumn('sort_asc')) {
$table->dropColumn('sort_asc');
$schemaChanged = true;
}
if ($table->hasColumn('recursive')) {
$table->dropColumn('recursive');
$schemaChanged = true;
}

return $schemaChanged ? $schema : null;
}

/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}
}
14 changes: 7 additions & 7 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ export default {
onDirectoryAddRecursive(path) {
const req = {
path,
recursive: true,
displayRecursive: true,
}
const url = generateUrl('/apps/gpxpod/directories')
axios.post(url, req).then((response) => {
Expand Down Expand Up @@ -575,20 +575,20 @@ export default {
this.loadDirectory(dirId, true, true)
},
onDirectoryRecursiveChanged(dirId) {
this.state.directories[dirId].recursive = !this.state.directories[dirId].recursive
this.updateDirectory(dirId, { recursive: this.state.directories[dirId].recursive })
this.state.directories[dirId].displayRecursive = !this.state.directories[dirId].displayRecursive
this.updateDirectory(dirId, { displayRecursive: this.state.directories[dirId].displayRecursive })
.then(() => {
this.loadDirectory(dirId, true, true)
})
},
onDirectorySortChanged({ dirId, sortOrder, sortAsc }) {
onDirectorySortChanged({ dirId, sortOrder, sortAscending }) {
if (sortOrder !== undefined) {
this.state.directories[dirId].sortOrder = sortOrder
this.updateDirectory(dirId, { sortOrder })
}
if (sortAsc !== undefined) {
this.state.directories[dirId].sortAsc = sortAsc
this.updateDirectory(dirId, { sortAsc })
if (sortAscending !== undefined) {
this.state.directories[dirId].sortAscending = sortAscending
this.updateDirectory(dirId, { sortAscending })
}
},
updateDirectory(dirId, values) {
Expand Down
Loading

0 comments on commit 036bf2b

Please sign in to comment.