From 003ed30ffeba675289caf03f6c4eae27a1005673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fukan=20=C3=96ks=C3=BCz?= Date: Mon, 9 Nov 2020 04:40:51 +0300 Subject: [PATCH] Infinite iterative folder/document creation controllers/models defined --- .../Http/Controllers/DocumentController.php | 29 ---------- server/app/Http/Livewire/Document/Create.php | 45 +++++++++++++--- server/app/Http/Livewire/Document/Edit.php | 8 +++ server/app/Http/Livewire/Document/Show.php | 24 +++++++-- server/app/Http/Livewire/Folder/Create.php | 43 ++++++++++++--- .../app/Http/Livewire/Folder/ListContents.php | 13 +++++ server/app/Http/Livewire/Folder/Show.php | 22 +++++--- .../Middleware/CheckIfUserOwnsDocument.php | 9 ++++ server/app/Models/Document.php | 2 +- server/app/Models/Folder.php | 2 +- .../resources/views/document/show.blade.php | 8 --- .../views/livewire/dashboard.blade.php | 8 +-- .../views/livewire/document/create.blade.php | 12 +---- .../views/livewire/document/edit.blade.php | 9 ++-- .../views/livewire/document/show.blade.php | 6 +-- .../views/livewire/folder/create.blade.php | 9 ---- .../livewire/folder/list-contents.blade.php | 53 ++++++++++++++++++- .../views/livewire/folder/show.blade.php | 4 +- server/routes/web.php | 13 ++++- 19 files changed, 223 insertions(+), 96 deletions(-) delete mode 100644 server/app/Http/Controllers/DocumentController.php delete mode 100644 server/resources/views/document/show.blade.php diff --git a/server/app/Http/Controllers/DocumentController.php b/server/app/Http/Controllers/DocumentController.php deleted file mode 100644 index 4b82162..0000000 --- a/server/app/Http/Controllers/DocumentController.php +++ /dev/null @@ -1,29 +0,0 @@ -document()->orderBy('updated_at', 'DESC')->get(); - - return view("dashboard"); - } - - public function show($documentId) - { - $document = Document::findOrFail($documentId); - - return view('document.show', ['document' => $document]); - } - - public function createNewDocument() - { - - } -} diff --git a/server/app/Http/Livewire/Document/Create.php b/server/app/Http/Livewire/Document/Create.php index 23f80d6..d35b234 100644 --- a/server/app/Http/Livewire/Document/Create.php +++ b/server/app/Http/Livewire/Document/Create.php @@ -4,12 +4,15 @@ use Livewire\Component; use App\Models\Document; +use App\Models\Folder; class Create extends Component { public $isCreating = false; public $name = ""; public $message = ""; + public $emitTo; + public $folderId; public function confirmCreating() { @@ -19,21 +22,51 @@ public function confirmCreating() public function createNewDocument() { try { - $document = Document::create([ - 'name' => $this->name - ]); + if($this->folderId == null) + { + $document = Document::create([ + 'name' => $this->name, + ]); + } else { + $document = Document::create([ + 'name' => $this->name, + 'folder_id' => (int)$this->folderId + ]); + } $document->user()->attach(auth()->user()); } catch (\Throwable $e) { - $this->emitTo('dashboard', 'flashMessage', "Oluşturma işlemi başarısız oldu!"); + $this->emitTo($this->emitTo, 'flashMessage', "Oluşturma işlemi başarısız oldu!"); } - $this->emitTo('dashboard', 'flashMessage', $this->name . " isimli doküman başarıyla eklendi!"); + $this->emitTo($this->emitTo, 'flashMessage', $this->name . " isimli doküman başarıyla eklendi!"); - $this->emitTo('document.show', 'newDocumentCreated'); + if ($this->folderId == null) + { + $documents = Document::where('folder_id', null)->orderBy('updated_at', 'DESC')->get(); + } else { + $documents = Folder::findOrFail($this->folderId)->document()->orderBy('updated_at', 'DESC')->get(); + } + + $this->emitTo('document.show', 'newDocumentCreated', $documents); $this->isCreating = false; $this->name = ""; } + public function mount($emitTo, $folderId) + { + $this->folderId = $folderId; + + $this->emitTo = $emitTo; + + if(!$emitTo) { + $this->emitTo = 'dashboard'; + } + + if(!$folderId) { + $this->folderId = null; + } + } + public function render() { return view('livewire.document.create'); diff --git a/server/app/Http/Livewire/Document/Edit.php b/server/app/Http/Livewire/Document/Edit.php index fd48c15..4dd8bda 100644 --- a/server/app/Http/Livewire/Document/Edit.php +++ b/server/app/Http/Livewire/Document/Edit.php @@ -3,9 +3,17 @@ namespace App\Http\Livewire\Document; use Livewire\Component; +use App\Models\Document; class Edit extends Component { + public $document; + + public function mount($documentId) + { + $this->document = Document::findOrFail($documentId); + } + public function render() { return view('livewire.document.edit'); diff --git a/server/app/Http/Livewire/Document/Show.php b/server/app/Http/Livewire/Document/Show.php index d9f4237..93d6805 100644 --- a/server/app/Http/Livewire/Document/Show.php +++ b/server/app/Http/Livewire/Document/Show.php @@ -10,14 +10,30 @@ class Show extends Component protected $listeners = ['newDocumentCreated']; - public function newDocumentCreated() + public function newDocumentCreated($documents) { - $this->documents = auth()->user()->document()->orderBy('updated_at', 'DESC')->get(); + if (!$documents) + { + $this->documents = auth()->user()->document() + ->orderBy('updated_at', 'DESC') + ->where('folder_id', null) + ->get(); + } else { + $this->documents = $documents; + } } - public function mount() + public function mount($documents) { - $this->documents = auth()->user()->document()->orderBy('updated_at', 'DESC')->get(); + if (!$documents) + { + $this->documents = auth()->user()->document() + ->orderBy('updated_at', 'DESC') + ->where('folder_id', null) + ->get(); + } else { + $this->documents = $documents; + } } public function render() diff --git a/server/app/Http/Livewire/Folder/Create.php b/server/app/Http/Livewire/Folder/Create.php index 6134cd3..0a8eb2f 100644 --- a/server/app/Http/Livewire/Folder/Create.php +++ b/server/app/Http/Livewire/Folder/Create.php @@ -10,6 +10,8 @@ class Create extends Component public $isCreatingFolder = false; public $name = ""; public $message = ""; + public $emitTo; + public $folderId; public function confirmCreatingFolder() { @@ -19,21 +21,50 @@ public function confirmCreatingFolder() public function createNewFolder() { try { - $folder = Folder::create([ - 'name' => $this->name - ]); + if ($this->folderId == null) { + $folder = Folder::create([ + 'name' => $this->name + ]); + } else { + $folder = Folder::create([ + 'name' => $this->name, + 'parent_folder_id' => (int)$this->folderId + ]); + } + $folder->user()->attach(auth()->user()); } catch (\Throwable $e) { - $this->emitTo('dashboard', 'flashMessage', "Oluşturma işlemi başarısız oldu!"); + $this->emitTo($this->emitTo, 'flashMessage', "Oluşturma işlemi başarısız oldu!"); } - $this->emitTo('dashboard', 'flashMessage', $this->name . " isimli klasör başarıyla eklendi!"); + $this->emitTo($this->emitTo, 'flashMessage', $this->name . " isimli klasör başarıyla eklendi!"); - $this->emitTo('folder.show', 'newFolderCreated'); + if ($this->folderId == null) { + $folders = Folder::where('parent_folder_id', null)->orderBy('updated_at', 'DESC')->get(); + } else { + $folders = Folder::where('parent_folder_id', $this->folderId)->orderBy('updated_at', 'DESC')->get(); + } + + $this->emitTo('folder.show', 'newFolderCreated', $folders, $this->folderId); $this->isCreatingFolder = false; $this->name = ""; } + public function mount($emitTo, $folderId) + { + $this->folderId = $folderId; + + $this->emitTo = $emitTo; + + if(!$emitTo) { + $this->emitTo = 'dashboard'; + } + + if(!$folderId) { + $this->folderId = null; + } + } + public function render() { return view('livewire.folder.create'); diff --git a/server/app/Http/Livewire/Folder/ListContents.php b/server/app/Http/Livewire/Folder/ListContents.php index b2c549c..f422932 100644 --- a/server/app/Http/Livewire/Folder/ListContents.php +++ b/server/app/Http/Livewire/Folder/ListContents.php @@ -2,10 +2,23 @@ namespace App\Http\Livewire\Folder; +use App\Models\Folder; use Livewire\Component; class ListContents extends Component { + public $subfolders; + public $documents; + public $folderId; + public $message; + + public function mount($folderId) + { + $this->folderId = $folderId; + $this->documents = Folder::findOrFail($folderId)->document()->orderBy('updated_at', 'DESC')->get(); + $this->subfolders = Folder::where('parent_folder_id', $folderId)->orderBy('updated_at', 'DESC')->get(); + } + public function render() { return view('livewire.folder.list-contents'); diff --git a/server/app/Http/Livewire/Folder/Show.php b/server/app/Http/Livewire/Folder/Show.php index de5c8d9..985bfe1 100644 --- a/server/app/Http/Livewire/Folder/Show.php +++ b/server/app/Http/Livewire/Folder/Show.php @@ -9,17 +9,27 @@ class Show extends Component public $folders; protected $listeners = ['newFolderCreated']; - - public function newFolderCreated() + + public function newFolderCreated($folders, $folderId) { - $this->folders = auth()->user()->folder()->orderBy('updated_at', 'DESC')->get(); + if(!$folders) + { + $this->folders = auth()->user()->folder()->where('parent_folder_id', null)->orderBy('updated_at', 'DESC')->get(); + } else { + $this->folders = auth()->user()->folder()->where('parent_folder_id', $folderId)->orderBy('updated_at', 'DESC')->get(); + } } - public function mount() + public function mount($folders, $folderId) { - $this->folders = auth()->user()->folder()->orderBy('updated_at', 'DESC')->get(); + if(!$folders) + { + $this->folders = auth()->user()->folder()->where('parent_folder_id', null)->orderBy('updated_at', 'DESC')->get(); + } else { + $this->folders = auth()->user()->folder()->where('parent_folder_id', $folderId)->orderBy('updated_at', 'DESC')->get(); + } } - + public function render() { return view('livewire.folder.show'); diff --git a/server/app/Http/Middleware/CheckIfUserOwnsDocument.php b/server/app/Http/Middleware/CheckIfUserOwnsDocument.php index ea90a9d..4398c89 100644 --- a/server/app/Http/Middleware/CheckIfUserOwnsDocument.php +++ b/server/app/Http/Middleware/CheckIfUserOwnsDocument.php @@ -26,6 +26,15 @@ public function handle(Request $request, Closure $next) } } + $folderId = $request->route('folderId'); + if ($folderId) + { + if (Auth::user()->folder()->find($folderId) === null) + { + return redirect(route('dashboard')); + } + } + return $next($request); } } diff --git a/server/app/Models/Document.php b/server/app/Models/Document.php index c6f00ee..7b0de9d 100644 --- a/server/app/Models/Document.php +++ b/server/app/Models/Document.php @@ -10,7 +10,7 @@ class Document extends Model use HasFactory; protected $fillable = [ - 'name', 'content' + 'name', 'content', 'folder_id' ]; public function user() diff --git a/server/app/Models/Folder.php b/server/app/Models/Folder.php index a83b42d..52b5e15 100644 --- a/server/app/Models/Folder.php +++ b/server/app/Models/Folder.php @@ -9,7 +9,7 @@ class Folder extends Model { use HasFactory; - protected $fillable = ['name']; + protected $fillable = ['name', 'parent_folder_id']; public function user() { diff --git a/server/resources/views/document/show.blade.php b/server/resources/views/document/show.blade.php deleted file mode 100644 index 6d03611..0000000 --- a/server/resources/views/document/show.blade.php +++ /dev/null @@ -1,8 +0,0 @@ - -
-
-

{{ $document->name }}

-

{{ $document->content }}

-
-
-
diff --git a/server/resources/views/livewire/dashboard.blade.php b/server/resources/views/livewire/dashboard.blade.php index 6b72226..1443d7d 100644 --- a/server/resources/views/livewire/dashboard.blade.php +++ b/server/resources/views/livewire/dashboard.blade.php @@ -41,10 +41,10 @@ - - + + - - + + diff --git a/server/resources/views/livewire/document/create.blade.php b/server/resources/views/livewire/document/create.blade.php index 3d1ab26..b3313d6 100644 --- a/server/resources/views/livewire/document/create.blade.php +++ b/server/resources/views/livewire/document/create.blade.php @@ -10,18 +10,8 @@ - @if (!empty($message)) - - @endif Oluşturmak istediğiniz dokümanın ismini yazınız. -
@@ -33,7 +23,7 @@ {{ __('Close') }} - + {{ __('Create') }} diff --git a/server/resources/views/livewire/document/edit.blade.php b/server/resources/views/livewire/document/edit.blade.php index ad58cc8..ce6bc78 100644 --- a/server/resources/views/livewire/document/edit.blade.php +++ b/server/resources/views/livewire/document/edit.blade.php @@ -1,3 +1,6 @@ -
- {{-- Stop trying to control. --}} -
+
+
+ {{ $document->name }} + {{ $document->content }} +
+
\ No newline at end of file diff --git a/server/resources/views/livewire/document/show.blade.php b/server/resources/views/livewire/document/show.blade.php index 6210afd..4e959cf 100644 --- a/server/resources/views/livewire/document/show.blade.php +++ b/server/resources/views/livewire/document/show.blade.php @@ -16,7 +16,7 @@ class="inline-flex items-center px-4 py-2 border border-transparent text-center
@foreach ($documents as $document)
- + -
{{ $document->name }}
-
{{ $document->updated_at->isoFormat('LLL') }} +
{{ $document["name"] }}
+
{{ \Carbon\Carbon::parse($document["updated_at"])->isoFormat('LLL') }}
diff --git a/server/resources/views/livewire/folder/create.blade.php b/server/resources/views/livewire/folder/create.blade.php index c695458..4a18d68 100644 --- a/server/resources/views/livewire/folder/create.blade.php +++ b/server/resources/views/livewire/folder/create.blade.php @@ -10,15 +10,6 @@ - @if (!empty($message)) - - @endif Oluşturmak istediğiniz klasörün ismini yazınız. diff --git a/server/resources/views/livewire/folder/list-contents.blade.php b/server/resources/views/livewire/folder/list-contents.blade.php index a40248d..16e3541 100644 --- a/server/resources/views/livewire/folder/list-contents.blade.php +++ b/server/resources/views/livewire/folder/list-contents.blade.php @@ -1,3 +1,52 @@ -
- {{-- Knowing others is intelligence; knowing yourself is true wisdom. --}} +
+
+
+
+ @if ($message) + + @endif +
+
+ +
+ + + + + +
+
diff --git a/server/resources/views/livewire/folder/show.blade.php b/server/resources/views/livewire/folder/show.blade.php index 81c6625..3d6dc4e 100644 --- a/server/resources/views/livewire/folder/show.blade.php +++ b/server/resources/views/livewire/folder/show.blade.php @@ -16,9 +16,9 @@ class="inline-flex items-center px-4 py-2 border border-transparent text-center
@foreach ($folders as $folder) @endforeach diff --git a/server/routes/web.php b/server/routes/web.php index d89b6e8..6405b0e 100644 --- a/server/routes/web.php +++ b/server/routes/web.php @@ -19,5 +19,16 @@ Route::middleware(['auth:sanctum', 'verified', 'document'])->group(function () { Route::get('/dashboard', \App\Http\Livewire\Dashboard::class)->name('dashboard'); - Route::get('/document/{documentId}', '\App\Http\Controllers\DocumentController@show')->name('showDocument'); + + Route::get('/document/{documentId}', \App\Http\Livewire\Document\Edit::class) + ->name('showDocument') + ->where([ + 'documentId' => '[0-9]+' + ]); + + Route::get('/folder/{folderId}', \App\Http\Livewire\Folder\ListContents::class) + ->name('listFolderContents') + ->where([ + 'folderId' => '[0-9]+' + ]); });