Skip to content

Commit

Permalink
Migrate note private status to visibility status (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kovah committed Jun 23, 2022
1 parent dba872f commit a92b513
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Models/NoteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function store(NoteStoreRequest $request): RedirectResponse
abort(403);
}

$data = $request->except(['_token']);
$data = $request->validated();
NoteRepository::create($data);

flash(trans('note.added_successfully'), 'success');
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/Models/NoteStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function rules(): array
'note' => [
'required',
],
'is_private' => [
'visibility' => [
'sometimes',
'boolean',
'integer',
],
];
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/Models/NoteUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public function rules(): array
'note' => [
'required',
],
'is_private' => [
'visibility' => [
'sometimes',
'boolean',
'integer',
],
];
}
Expand Down
7 changes: 4 additions & 3 deletions app/Models/LinkList.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
* @property string|null $deleted_at
* @property-read Collection|Link[] $links
* @property-read User $user
* @method static Builder|Tag byUser(int $user_id = null)
* @method static Builder|Tag privateOnly()
* @method static Builder|Tag publicOnly()
* @method static Builder|LinkList byUser(int $user_id = null)
* @method static Builder|LinkList privateOnly()
* @method static Builder|LinkList internalOnly()
* @method static Builder|LinkList publicOnly()
*/
class LinkList extends Model implements Auditable
{
Expand Down
16 changes: 10 additions & 6 deletions app/Models/Note.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace App\Models;

use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;

/**
Expand All @@ -18,30 +18,34 @@
* @property int $user_id
* @property int $link_id
* @property string $note
* @property int $is_private
* @property int $visibility
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property string|null $deleted_at
* @property-read Link $link
* @property-read User $user
* @method static Builder|Link byUser($user_id = null)
* @method static Builder|Note byUser($user_id = null)
* @method static Builder|Note privateOnly()
* @method static Builder|Note internalOnly()
* @method static Builder|Note publicOnly()
*/
class Note extends Model
{
use SoftDeletes;
use HasFactory;
use ScopesVisibility;
use SoftDeletes;

public $fillable = [
'user_id',
'link_id',
'note',
'is_private',
'visibility',
];

protected $casts = [
'user_id' => 'integer',
'link_id' => 'integer',
'is_private' => 'boolean',
'visibility' => 'integer',
];

/*
Expand Down
2 changes: 0 additions & 2 deletions app/Repositories/NoteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public static function create(array $data): Note
*/
public static function update(Note $note, array $data): Note
{
$data['is_private'] ??= false;

$note->update($data);

return $note;
Expand Down
2 changes: 1 addition & 1 deletion app/View/Components/Forms/VisibilityToggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class VisibilityToggle extends Component
{
public function __construct(private ?int $existingValue = null)
public function __construct(private ?int $existingValue = null, public string $inputClasses = '', public string $labelClasses = '')
{
}

Expand Down
22 changes: 22 additions & 0 deletions database/migrations/2022_06_23_112431_migrate_user_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Enums\ModelAttribute;
use App\Models\LinkList;
use App\Models\Note;
use App\Models\Tag;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
Expand All @@ -18,6 +19,7 @@ public function up()
$this->migrateLinkVisibility();
$this->migrateListVisibility();
$this->migrateTagVisibility();
$this->migrateNoteVisibility();
}

protected function migrateLinkVisibility(): void
Expand Down Expand Up @@ -81,4 +83,24 @@ protected function migrateTagVisibility(): void
$table->dropColumn(['is_private']);
});
}

protected function migrateNoteVisibility(): void
{
Schema::table('notes', function (Blueprint $table) {
$table->integer('visibility')->default(ModelAttribute::VISIBILITY_PRIVATE)->after('is_private');
});

Note::withTrashed()->get()->each(function ($note) {
$note->visibility = match ((bool)$note->is_private) {
true => ModelAttribute::VISIBILITY_PRIVATE,
false => $this->guestAccessEnabled
? ModelAttribute::VISIBILITY_PUBLIC : ModelAttribute::VISIBILITY_INTERNAL,
};
$note->saveQuietly();
});

Schema::table('notes', function (Blueprint $table) {
$table->dropColumn(['is_private']);
});
}
}
2 changes: 2 additions & 0 deletions lang/en_US/note.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
'update' => 'Update Note',
'delete' => 'Delete Note',

'public' => 'Public Note',
'internal' => 'Internal Note',
'private' => 'Private Note',

'note_content' => 'Note Content',
Expand Down
4 changes: 2 additions & 2 deletions resources/views/components/forms/visibility-toggle.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div {{ $attributes }}>
<label class="form-label" for="visibility">@lang('linkace.visibility')</label>
<select id="visibility" name="visibility" class="form-select{{ $errors->has('visibility') ? ' is-invalid' : '' }}">
<label class="form-label {{ $labelClasses }}" for="visibility">@lang('linkace.visibility')</label>
<select id="visibility" name="visibility" class="form-select {{ $inputClasses }}{{ $errors->has('visibility') ? ' is-invalid' : '' }}">
<option value="{{ $public }}" {{ $publicSelected ? 'selected' : '' }}>
@lang('attributes.visibility.' . $public)
</option>
Expand Down
10 changes: 3 additions & 7 deletions resources/views/models/notes/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@ class="form-control{{ $errors->has('note') ? ' is-invalid' : '' }}"

<div class="d-flex align-items-center">

<div class="form-check ms-auto me-3">
<input class="form-check-input" type="checkbox" id="is_private" name="is_private" value="1"
@if($note->is_private) checked @endif>
<label class="form-check-label" for="is_private">
<small>@lang('note.private')</small>
</label>
</div>
<x-forms.visibility-toggle :existing-value="$note->visibility"
class="ms-auto me-3 d-flex align-items-center" input-classes="form-select-sm"
label-classes="mb-0 me-2 small"/>

<button type="submit" class="btn btn-sm btn-primary">
<x-icon.save class="me-2"/> @lang('note.edit')
Expand Down
9 changes: 2 additions & 7 deletions resources/views/models/notes/partials/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ class="form-control{{ $errors->has('note') ? ' is-invalid' : '' }}"

<div class="d-flex align-items-center">

<div class="form-check ms-auto me-3">
<input class="form-check-input" type="checkbox" id="is_private" name="is_private" value="1"
@if($link->is_private || usersettings('notes_private_default')) checked @endif>
<label class="form-check-label" for="is_private">
<small>@lang('note.private')</small>
</label>
</div>
<x-forms.visibility-toggle class="ms-auto me-3 d-flex align-items-center" input-classes="form-select-sm"
label-classes="mb-0 me-2 small"/>

<button type="submit" class="btn btn-sm btn-primary">
<x-icon.save class="me-2"/> @lang('note.add')
Expand Down
65 changes: 65 additions & 0 deletions tests/Migrations/UserDataMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Link;
use App\Models\LinkList;
use App\Models\Note;
use App\Models\Tag;
use App\Settings\SystemSettings;
use Tests\TestCase;
Expand Down Expand Up @@ -195,4 +196,68 @@ public function testTagVisibilityMigrationWithEnabledGuestMode(): void
'visibility' => 1, // is public
]);
}

public function testNoteVisibilityMigration(): void
{
$this->migrateUpTo('2022_06_23_112431_migrate_user_data.php');

Note::unguard();
Note::create([
'user_id' => 1,
'link_id' => 1,
'note' => 'A private note',
'is_private' => true,
]);

Note::create([
'user_id' => 1,
'link_id' => 1,
'note' => 'A public note',
'is_private' => false,
]);

$this->artisan('migrate');

$this->assertDatabaseHas('notes', [
'note' => 'A private note',
'visibility' => 3, // is private
]);
$this->assertDatabaseHas('notes', [
'note' => 'A public note',
'visibility' => 2, // is internal
]);
}

public function testNoteVisibilityMigrationWithEnabledGuestMode(): void
{
$this->migrateUpTo('2022_06_23_112431_migrate_user_data.php');

SystemSettings::fake(['guest_access_enabled' => true]);

Note::unguard();
Note::create([
'user_id' => 1,
'link_id' => 1,
'note' => 'A private note',
'is_private' => true,
]);

Note::create([
'user_id' => 1,
'link_id' => 1,
'note' => 'A public note',
'is_private' => false,
]);

$this->artisan('migrate');

$this->assertDatabaseHas('notes', [
'note' => 'A private note',
'visibility' => 3, // is private
]);
$this->assertDatabaseHas('notes', [
'note' => 'A public note',
'visibility' => 1, // is public
]);
}
}

0 comments on commit a92b513

Please sign in to comment.