diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index b41c4f49..2bd9a06f 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -19,13 +19,13 @@ public function show(Post $post) $post->with('comments'); return view('post.show', [ - 'post' => $post, + '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 * @@ -70,6 +71,7 @@ public function list(Request $request) 'posts' => $posts, ])->fragmentsIf(!$request->isMethodSafe()); - return $test; + return $test; } + } diff --git a/app/Http/Controllers/ProfileCommentsController.php b/app/Http/Controllers/ProfileCommentsController.php new file mode 100644 index 00000000..a23b8f82 --- /dev/null +++ b/app/Http/Controllers/ProfileCommentsController.php @@ -0,0 +1,117 @@ + $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..ad28d7bf 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Post; use App\Models\User; use Illuminate\Http\Request; @@ -15,7 +16,7 @@ class ProfileController extends Controller */ 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') @@ -25,28 +26,31 @@ public function show(User $user, Request $request) 'user' => $user, 'isMyAccount' => $isMyAccount, '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'), @@ -54,4 +58,38 @@ public function update(Request $request) ])->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..92060af2 --- /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,46 +28,34 @@ На проекте с 23 мая 2022

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

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

- @if ($isMyAccount) -
-
-
- Напишите первую статью, чтобы привлечь читателей -
- -
-
- @else -
-
-
- Здесь еще нет публикаций -
-
-
- @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..0f008b75 --- /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'); + }); + /* |--------------------------------------------------------------------------