mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-01-17 13:18:21 +01:00
Migrate note private status to visibility status (#165)
This commit is contained in:
parent
dba872faad
commit
a92b513cc0
@ -28,7 +28,7 @@ class NoteController extends Controller
|
||||
abort(403);
|
||||
}
|
||||
|
||||
$data = $request->except(['_token']);
|
||||
$data = $request->validated();
|
||||
NoteRepository::create($data);
|
||||
|
||||
flash(trans('note.added_successfully'), 'success');
|
||||
|
@ -30,9 +30,9 @@ class NoteStoreRequest extends FormRequest
|
||||
'note' => [
|
||||
'required',
|
||||
],
|
||||
'is_private' => [
|
||||
'visibility' => [
|
||||
'sometimes',
|
||||
'boolean',
|
||||
'integer',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -27,9 +27,9 @@ class NoteUpdateRequest extends FormRequest
|
||||
'note' => [
|
||||
'required',
|
||||
],
|
||||
'is_private' => [
|
||||
'visibility' => [
|
||||
'sometimes',
|
||||
'boolean',
|
||||
'integer',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -30,9 +30,10 @@ use OwenIt\Auditing\Contracts\Auditable;
|
||||
* @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
|
||||
{
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
@ -18,30 +18,34 @@ use Illuminate\Support\Str;
|
||||
* @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',
|
||||
];
|
||||
|
||||
/*
|
||||
|
@ -26,8 +26,6 @@ class NoteRepository
|
||||
*/
|
||||
public static function update(Note $note, array $data): Note
|
||||
{
|
||||
$data['is_private'] ??= false;
|
||||
|
||||
$note->update($data);
|
||||
|
||||
return $note;
|
||||
|
@ -8,7 +8,7 @@ use Illuminate\View\Component;
|
||||
|
||||
class VisibilityToggle extends Component
|
||||
{
|
||||
public function __construct(private ?int $existingValue = null)
|
||||
public function __construct(private ?int $existingValue = null, public string $inputClasses = '', public string $labelClasses = '')
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
@ -18,6 +19,7 @@ class MigrateUserData extends Migration
|
||||
$this->migrateLinkVisibility();
|
||||
$this->migrateListVisibility();
|
||||
$this->migrateTagVisibility();
|
||||
$this->migrateNoteVisibility();
|
||||
}
|
||||
|
||||
protected function migrateLinkVisibility(): void
|
||||
@ -81,4 +83,24 @@ class MigrateUserData extends Migration
|
||||
$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']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ return [
|
||||
'update' => 'Update Note',
|
||||
'delete' => 'Delete Note',
|
||||
|
||||
'public' => 'Public Note',
|
||||
'internal' => 'Internal Note',
|
||||
'private' => 'Private Note',
|
||||
|
||||
'note_content' => 'Note Content',
|
||||
|
@ -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>
|
||||
|
@ -27,13 +27,9 @@
|
||||
|
||||
<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')
|
||||
|
@ -23,13 +23,8 @@
|
||||
|
||||
<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')
|
||||
|
@ -4,6 +4,7 @@ namespace Tests\Migrations;
|
||||
|
||||
use App\Models\Link;
|
||||
use App\Models\LinkList;
|
||||
use App\Models\Note;
|
||||
use App\Models\Tag;
|
||||
use App\Settings\SystemSettings;
|
||||
use Tests\TestCase;
|
||||
@ -195,4 +196,68 @@ class UserDataMigrationTest extends TestCase
|
||||
'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
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user