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 @@ +
Тут будут награды пользователя:
- @if ($isMyAccount) -Разработчик laravel.su
+Разработчик laravel.su
-
{{ $comment->commenter->name }}
+ ++- Action
+ - Another action
+ - Something else here
+
+{!! $comment->prettyComment() !!}
+ @endif + +