Refactor code

This commit is contained in:
brufdev
2025-02-13 13:42:13 +00:00
parent 1122ce3538
commit 3d95517f47
7 changed files with 33 additions and 41 deletions

View File

@@ -12,11 +12,11 @@ final readonly class CreateVaultNode
{
/**
* @param array{
* parent_id?: int|null,
* is_file: bool,
* name: string,
* extension?: string|null,
* content?: string|null
* parent_id?: int|null,
* is_file: bool,
* name: string,
* extension?: string|null,
* content?: string|null
* } $attributes
*/
public function handle(Vault $vault, array $attributes): VaultNode

View File

@@ -11,23 +11,23 @@ final readonly class UpdateVaultNode
{
/**
* @param array{
* parent_id?: int|null,
* is_file: bool,
* name: string,
* extension?: string|null,
* content?: string|null
* parent_id: int|null,
* is_file: bool,
* name: string,
* extension: string|null,
* content: string|null
* } $attributes
*/
public function handle(VaultNode $node, array $attributes): void
{
$relativeOriginalPath = new GetPathFromVaultNode()->handle($node);
$originalPath = new GetPathFromVaultNode()->handle($node);
// Save node to database
$node->update($attributes);
// Save node to disk
if ($node->is_file) {
Storage::disk('local')->put($relativeOriginalPath, $attributes['content'] ?? '');
Storage::disk('local')->put($originalPath, $attributes['content'] ?? '');
}
if (!$node->wasChanged('name')) {
@@ -35,10 +35,7 @@ final readonly class UpdateVaultNode
}
// Rename node on disk
$relativePath = new GetPathFromVaultNode()->handle($node);
Storage::disk('local')->move(
$relativeOriginalPath,
$relativePath,
);
$path = new GetPathFromVaultNode()->handle($node);
Storage::disk('local')->move($originalPath, $path);
}
}

View File

@@ -64,7 +64,5 @@ it('logouts the user', function (): void {
->test(UserMenu::class)
->call('logout');
/** @var User $currentUser */
$currentUser = auth()->user();
expect($currentUser)->toBeNull();
expect(auth()->user())->toBeNull();
});

View File

@@ -50,5 +50,6 @@ it('adds a node', function (): void {
->set('form.name', fake()->words(3, true))
->call('add')
->assertSet('show', false);
expect($vault->nodes()->count())->toBe(1);
});

View File

@@ -30,9 +30,8 @@ it('creates a vault', function (): void {
->call('create');
expect($user->vaults()->count())->toBe(1);
$relativePath = new GetPathFromUser()->handle($user);
$absolutePath = Storage::disk('local')->path($relativePath . $vaultName);
expect($absolutePath)->toBeDirectory();
$path = new GetPathFromUser()->handle($user) . $vaultName;
expect(Storage::disk('local')->path($path))->toBeDirectory();
});
it('exports a vault', function (): void {
@@ -112,7 +111,6 @@ it('deletes a vault', function (): void {
->assertDispatched('toast');
expect($user->vaults()->count())->toBe(0);
$relativePath = new GetPathFromVault()->handle($vault);
$absolutePath = Storage::disk('local')->path($relativePath);
expect($absolutePath)->not->toBeDirectory();
$path = new GetPathFromVault()->handle($vault);
expect(Storage::disk('local')->path($path))->not->toBeDirectory();
});

View File

@@ -21,7 +21,6 @@ it('updates the vault', function (): void {
->call('update');
expect($user->vaults()->first()->name)->toBe($newName);
$relativePath = new GetPathFromUser()->handle($user);
$absolutePath = Storage::disk('local')->path($relativePath . $newName);
expect($absolutePath)->toBeDirectory();
$path = new GetPathFromUser()->handle($user) . $newName;
expect(Storage::disk('local')->path($path))->toBeDirectory();
});

View File

@@ -60,7 +60,7 @@ it('does not open a folder', function (): void {
->assertSet('selectedFile', null);
});
it('resets edit mode when opening a that is not a note', function (): void {
it('resets edit mode when opening a file that is not a note', function (): void {
$user = User::factory()->create()->first();
$vault = new CreateVault()->handle($user, [
'name' => fake()->words(3, true),
@@ -145,17 +145,17 @@ it('refreshes an open file', function (): void {
'name' => fake()->words(3, true),
'extension' => 'md',
]);
$relativeUrl = new GetUrlFromVaultNode()->handle($node);
$url = new GetUrlFromVaultNode()->handle($node);
$name = $node->name;
$newName = fake()->words(4, true);
Livewire::actingAs($user)
->withQueryParams(['file' => $node->id])
->test(Show::class, ['vault' => $vault])
->assertSet('selectedFileUrl', $relativeUrl)
->assertSet('selectedFileUrl', $url)
->set('nodeForm.name', $newName)
->call('refreshFile', $node->refresh())
->assertSet('selectedFileUrl', str_replace($name, $newName, $relativeUrl));
->assertSet('selectedFileUrl', str_replace($name, $newName, $url));
});
it('does not refresh a file that is not open', function (): void {
@@ -349,8 +349,8 @@ it('updates the node', function (): void {
->set('nodeForm.content', $newContent);
expect($vault->nodes()->first()->content)->toBe($newContent);
$relativePath = new GetPathFromVaultNode()->handle($node);
expect(Storage::disk('local')->get($relativePath))->toBe($newContent);
$path = new GetPathFromVaultNode()->handle($node);
expect(Storage::disk('local')->get($path))->toBe($newContent);
});
it('process the links when updating a node', function (): void {
@@ -418,9 +418,8 @@ it('updates the vault', function (): void {
->call('editVault');
expect($user->vaults()->first()->name)->toBe($newName);
$relativePath = new GetPathFromUser()->handle($user);
$absolutePath = Storage::disk('local')->path($relativePath . $newName);
expect($absolutePath)->toBeDirectory();
$path = new GetPathFromUser()->handle($user) . $newName;
expect(Storage::disk('local')->path($path))->toBeDirectory();
});
it('deletes a node', function (): void {
@@ -447,9 +446,8 @@ it('deletes a node', function (): void {
->assertDispatched('toast');
expect($vault->nodes()->count())->toBe(0);
$relativePath = new GetPathFromVaultNode()->handle($folderNode);
$absolutePath = Storage::disk('local')->path($relativePath);
expect($absolutePath)->not->toBeDirectory();
$path = new GetPathFromVaultNode()->handle($folderNode);
expect(Storage::disk('local')->path($path))->not->toBeDirectory();
});
it('closes an open file when it is deleted', function (): void {
@@ -500,6 +498,7 @@ it('deletes the links and backlinks when deleting a node', function (): void {
->test(Show::class, ['vault' => $vault])
->call('deleteNode', $firstNode)
->assertDispatched('toast');
expect($firstNode->links()->count())->toBe(0);
expect($secondNode->links()->count())->toBe(0);
});