From 05b61a4ac674d5f7b5ef50404a631f14c9cea2cf Mon Sep 17 00:00:00 2001 From: SadElephant Date: Tue, 7 Nov 2023 18:39:58 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D1=82=D0=B0=D1=82=D1=8C=D0=B8=20=D0=B8?= =?UTF-8?q?=20=D0=BA=D0=BE=D0=BC=D0=BC=D0=B5=D0=BD=D1=82=D0=B0=D1=80=D0=B8?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80=D0=BE=D1=84=D0=B8?= =?UTF-8?q?=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/PostController.php | 6 +- .../Controllers/ProfileCommentsController.php | 115 ++++++++++++++++++ app/Http/Controllers/ProfileController.php | 59 +++++++-- app/Policies/OwnerPolicy.php | 41 +++++++ app/Policies/PostPolicy.php | 9 ++ .../views/pages/profile/profile.blade.php | 28 +++-- .../views/pages/profile/tabs/base.blade.php | 28 +++++ .../pages/profile/tabs/comments-tab.blade.php | 43 +++++++ .../tabs/particles/comments/comment.blade.php | 79 ++++++++++++ .../pages/profile/tabs/posts-tab.blade.php | 27 ++++ .../views/particles/posts/list.blade.php | 71 +++++++++++ resources/views/post/list.blade.php | 64 +--------- routes/web.php | 27 ++++ 13 files changed, 513 insertions(+), 84 deletions(-) create mode 100644 app/Http/Controllers/ProfileCommentsController.php create mode 100644 app/Policies/OwnerPolicy.php create mode 100644 app/Policies/PostPolicy.php create mode 100644 resources/views/pages/profile/tabs/base.blade.php create mode 100644 resources/views/pages/profile/tabs/comments-tab.blade.php create mode 100644 resources/views/pages/profile/tabs/particles/comments/comment.blade.php create mode 100644 resources/views/pages/profile/tabs/posts-tab.blade.php create mode 100644 resources/views/particles/posts/list.blade.php diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index b41c4f49..6aca551c 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -25,7 +25,7 @@ public function show(Post $post) public function edit(Post $post): View { - //нужно будет проверять аавтора, если пост существует + $this->authorize('isOwner',$post); $title = $post->exists ? 'Редактирование' : 'Новая статья'; return view('post.edit', [ @@ -36,7 +36,7 @@ public function edit(Post $post): View public function update(Request $request, Post $post) { - //нужно проверять аавтора, если пост существует + $this->authorize('isOwner',$post); $request->validate([ 'title' => 'required|string', @@ -54,6 +54,7 @@ public function update(Request $request, Post $post) return redirect()->route('post.edit', $post);//пока сюда } + /** * @param \Illuminate\Http\Request $request * @@ -72,4 +73,5 @@ public function list(Request $request) return $test; } + } diff --git a/app/Http/Controllers/ProfileCommentsController.php b/app/Http/Controllers/ProfileCommentsController.php new file mode 100644 index 00000000..b8985712 --- /dev/null +++ b/app/Http/Controllers/ProfileCommentsController.php @@ -0,0 +1,115 @@ + $user->comments, + 'user' => $user, + 'active' => 'comments' + ])) + ->fragmentIf(!request()->isMethod('GET'), 'comments'); + } + + /** + * @param \App\Models\Comment $comment + * + * @return string + */ + public function showReply(Comment $comment) + { + return $this->show($comment->commenter, [ + 'reply' => $comment->getKey(), + ]); + } + + /** + * @param \App\Models\Comment $comment + * + * @return string + */ + public function showEdit(Comment $comment) + { + return $this->show($comment->commenter, [ + 'edit' => $comment->getKey(), + ]); + } + + /** + * @param \Illuminate\Http\Request $request + * @param \App\Models\Comment $comment + * + * @return string + */ + public function reply(Request $request, Comment $comment) + { + $this->authorize('reply', $comment); + + $request->validate([ + 'message' => 'required|string', + ]); + + $reply = new Comment([ + 'comment' => $request->input('message'), + 'approved' => true, + ]); + + $reply->commenter()->associate($request->user()); + $reply->commentable()->associate($comment->commentable); + $reply->parent()->associate($comment); + $reply->save(); + + return $this->show($comment->commenter); + } + + /** + * @param \Illuminate\Http\Request $request + * @param \App\Models\Comment $comment + * + * @return string + */ + public function update(Request $request, Comment $comment) + { + $this->authorize('update', $comment); + + $request->validate([ + 'message' => 'required|string', + ]); + + $comment->update([ + 'comment' => $request->message, + ]); + + return $this->show($comment->commenter); + } + + /** + * @param \App\Models\Comment $comment + * + * @return string + */ + public function delete(Comment $comment) + { + $this->authorize('delete', $comment); + + $comment->children()->exists() + ? $comment->delete() + : $comment->forceDelete(); + + return $this->show($comment->commenter); + } + +} diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index 788bd7c8..d38ccd39 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -2,56 +2,91 @@ namespace App\Http\Controllers; +use App\Models\Post; use App\Models\User; use Illuminate\Http\Request; class ProfileController extends Controller { /** - * @param \App\Models\User $user + * @param \App\Models\User $user * @param \Illuminate\Http\Request $request * * @return \Illuminate\Contracts\View\View */ public function show(User $user, Request $request) { - $isMyAccount = $user->id === $request->user()->id; + $isMyAccount = $user->id === $request->user()?->id; $posts = $user->load('posts') ->orderBy('id', 'desc') ->simplePaginate(5); return view('pages.profile.profile', [ - 'user' => $user, + 'user' => $user, 'isMyAccount' => $isMyAccount, - 'posts' => $posts, + 'posts' => $posts, + 'active' => 'posts' ]); } - public function edit(Request $request){ + public function edit(Request $request) + { return view('pages.profile.edit', [ - 'user' => $request->user(), + 'user' => $request->user(), ]); } public function update(Request $request) { - $request->validate([ - 'name' => 'required|string', - 'about'=> 'required|string' - ], + $request->validate( + [ + 'name' => 'required|string', + 'about' => 'sometimes|string' + ], [], [ - 'name' => 'Имя', + 'name' => 'Имя', 'about' => 'О себе' ]); $request->user()->fill([ - 'name' => $request->input('name'), + 'name' => $request->input('name'), 'about' => $request->input('about') ])->save(); return $this->edit($request)->fragment('profile'); } + + /** + * @param \Illuminate\Http\Request $request + * + * @return string + */ + public function postTab(Request $request,User $user) + { + $posts = $user->posts() + ->withCount('comments') + ->orderBy('id', 'desc') + ->simplePaginate(3); + + $test = view('pages.profile.tabs.posts-tab', [ + 'posts' => $posts, + 'user' => $user, + 'isMyAccount' => $user->id === $request->user()?->id, + 'active' => 'posts' + ])->fragmentsIf(!$request->isMethodSafe()); + + return $test; + } + + public function commentsTab(User $user, array $data = []) + { + return view('pages.profile.tabs.comments-tab', array_merge($data, [ + 'user' => $user, + 'active' => 'comments' + ])) + ->fragmentIf(!request()->isMethod('GET'), 'comments'); + } } diff --git a/app/Policies/OwnerPolicy.php b/app/Policies/OwnerPolicy.php new file mode 100644 index 00000000..fa87863c --- /dev/null +++ b/app/Policies/OwnerPolicy.php @@ -0,0 +1,41 @@ +id === $model->user_id) || is_null($model->user_id) + ? Response::allow() + : Response::denyAsNotFound(); + } + + /** + * Determine whether the user can view the model. + * + * @param User $user + * @param Model $model + * + * @return bool + */ + public function isOwner(User $user, Model $model):bool + { + return is_null($model->user_id) || ($user->id === $model->user_id); + } +} diff --git a/app/Policies/PostPolicy.php b/app/Policies/PostPolicy.php new file mode 100644 index 00000000..c52e8a0d --- /dev/null +++ b/app/Policies/PostPolicy.php @@ -0,0 +1,9 @@ +
+
@@ -19,6 +20,7 @@
+

{{ $user->name }} @@ -26,27 +28,34 @@ На проекте с 23 мая 2022

-
+
+ @if ($isMyAccount) + + + Выйти из профиля + Редактировать + @else + + + + - - - Выйти из профиля - @endif
-

Тут будут награды пользователя:

+ {{--

Тут будут награды пользователя:

--}} + - @if ($isMyAccount) + {{-- @if ($isMyAccount)
@@ -66,6 +75,11 @@
@endif + --}} +
+ + + @endsection diff --git a/resources/views/pages/profile/tabs/base.blade.php b/resources/views/pages/profile/tabs/base.blade.php new file mode 100644 index 00000000..4ddc3b2d --- /dev/null +++ b/resources/views/pages/profile/tabs/base.blade.php @@ -0,0 +1,28 @@ + + + + +
+ @yield('tab') +
+
diff --git a/resources/views/pages/profile/tabs/comments-tab.blade.php b/resources/views/pages/profile/tabs/comments-tab.blade.php new file mode 100644 index 00000000..94dc3586 --- /dev/null +++ b/resources/views/pages/profile/tabs/comments-tab.blade.php @@ -0,0 +1,43 @@ +@extends('pages.profile.tabs.base') + +@section('tab') + @if($comments->isEmpty()) + @if ($isMyAccount) +
+
+
+ Вы не написали ни одного комментария +
+ +
+
+ @else +
+
+
+ Этот пользователь не оставил ни одного комментария +
+
+
+ @endif + @else + + + + +
+
+ @foreach($comments as $comment) + @include('pages.profile.tabs.particles.comments.comment', [ + 'comment' => $comment, + 'edit' => $edit ?? null, + ]) + @endforeach +
+
+
+
+ @endif +@endsection diff --git a/resources/views/pages/profile/tabs/particles/comments/comment.blade.php b/resources/views/pages/profile/tabs/particles/comments/comment.blade.php new file mode 100644 index 00000000..aa2b5015 --- /dev/null +++ b/resources/views/pages/profile/tabs/particles/comments/comment.blade.php @@ -0,0 +1,79 @@ +
+ +
+
+ + {{ $comment->commenter->name }} + +
+ +
+
+
{{ $comment->commenter->name }}
+ +
+ + + + + @can('update', $comment) + · + Редактировать + @endcan + + @can('delete', $comment) + · + Удалить + + @endcan +
+ + {{-- + + --}} +
+ + + @if($edit === $comment->getKey()) +
+ @method('PUT') + +
+ +
+
+ @else +

{!! $comment->prettyComment() !!}

+ @endif + + +
+
+ +
diff --git a/resources/views/pages/profile/tabs/posts-tab.blade.php b/resources/views/pages/profile/tabs/posts-tab.blade.php new file mode 100644 index 00000000..bfb5b75f --- /dev/null +++ b/resources/views/pages/profile/tabs/posts-tab.blade.php @@ -0,0 +1,27 @@ + +@extends('pages.profile.tabs.base') + +@section('tab') + @if($posts->isEmpty()) +
+
+ @if ($isMyAccount) +
+ Напишите первую статью, чтобы привлечь читателей +
+ + + @else + +
+ Здесь еще нет публикаций +
+ @endif +
+
+ @else + @include('particles.posts.list',['isProfile'=>true]) + @endif +@endsection diff --git a/resources/views/particles/posts/list.blade.php b/resources/views/particles/posts/list.blade.php new file mode 100644 index 00000000..7660dbf4 --- /dev/null +++ b/resources/views/particles/posts/list.blade.php @@ -0,0 +1,71 @@ + + @foreach ($posts as $post) +
+ +
+
+
+ + {{ $post->user->title }} + +
+ +
+
+ {{ $post->user->name }} +
+

Разработчик laravel.su

+
+
+ +
+ +
+ + +

{{ $post->title }}

+ +

{{ $post->preview() }}

+
+ +
+ + + + ~56 + + + + + {{ $post->comments_count }} + + + {{ $post->created_at->diffForHumans() }} +
+
+ @endforeach +
+ + + @if ($posts->hasMorePages()) +
+ +
+ @endif +
diff --git a/resources/views/post/list.blade.php b/resources/views/post/list.blade.php index 675223ae..9544667a 100644 --- a/resources/views/post/list.blade.php +++ b/resources/views/post/list.blade.php @@ -36,69 +36,7 @@
- - @foreach ($posts as $post) -
- -
-
-
- - {{ $post->user->title }} - -
- -
-
- {{ $post->user->name }} -
-

Разработчик laravel.su

-
-
- -
- -
- - -

{{ $post->title }}

- -

{{ $post->preview() }}

-
- -
- - - - ~56 - - - - - {{ $post->comments_count }} - - - {{ $post->created_at->diffForHumans() }} -
-
- @endforeach -
- - - @if ($posts->hasMorePages()) -
- -
- @endif -
+ @include('particles.posts.list',['isMyAccount'=>false])
@endsection diff --git a/routes/web.php b/routes/web.php index e8501bdc..a5c763d7 100644 --- a/routes/web.php +++ b/routes/web.php @@ -4,6 +4,7 @@ use App\Http\Controllers\AuthController; use App\Http\Controllers\DocsController; use App\Http\Controllers\CommentsController; +use App\Http\Controllers\ProfileCommentsController; use Illuminate\Support\Facades\Route; use App\Http\Middleware\TurboStream; use App\Docs; @@ -53,6 +54,7 @@ ->group(function () { Route::get('/posts/edit/{post?}', [PostController::class, 'edit'])->name('post.edit'); Route::post('/posts/edit/{post?}', [PostController::class, 'update'])->name('post.update'); + // Route::delete('/posts/edit/{post}', [PostController::class, 'delete'])->name('post.delete'); }); @@ -144,6 +146,31 @@ Route::get('/profile/{user:nickname}', [\App\Http\Controllers\ProfileController::class, 'show']) ->name('profile'); +Route::get('/profile/{user:nickname}/posts',[\App\Http\Controllers\ProfileController::class,'postTab']) + ->name('profile.posts'); + +Route::post('/profile/{user:nickname}/posts',[\App\Http\Controllers\ProfileController::class,'postTab']) + ->middleware(\App\Http\Middleware\TurboStream::class); + + +Route::get('/profile/{user:nickname}/comments',[\App\Http\Controllers\ProfileCommentsController::class,'show']) + ->name('profile.comments'); + + +Route::middleware(['auth', TurboStream::class]) + ->prefix('/profile/comments') + ->group(function () { + + + Route::put('/{comment}', [ProfileCommentsController::class, 'update']) + ->name('profile.comments.update'); + + Route::delete('/{comment}', [ProfileCommentsController::class, 'delete']) + ->name('profile.comments.delete'); + + Route::post('/{comment}/edit', [ProfileCommentsController::class, 'showEdit'])->name('profile.comments.show.edit'); + }); + /* |--------------------------------------------------------------------------