44
55use App \Models \Post ;
66use Illuminate \Http \Request ;
7+ use Illuminate \Support \Facades \Auth ;
78use Illuminate \View \View ;
89use Illuminate \Support \Facades \Response ;
10+ use Illuminate \Database \Eloquent \Builder ;
911
1012class PostController extends Controller
1113{
14+ public $ action = "append " ;
15+
1216 /**
1317 * @param \App\Models\Post $post
1418 *
@@ -19,13 +23,13 @@ public function show(Post $post)
1923 $ post ->with ('comments ' );
2024
2125 return view ('post.show ' , [
22- 'post ' => $ post ,
26+ 'post ' => $ post ,
2327 ]);
2428 }
2529
2630 public function edit (Post $ post ): View
2731 {
28- //нужно будет проверять аавтора, если пост существует
32+ $ this -> authorize ( ' isOwner ' , $ post );
2933
3034 $ title = $ post ->exists ? 'Редактирование ' : 'Новая статья ' ;
3135 return view ('post.edit ' , [
@@ -36,7 +40,7 @@ public function edit(Post $post): View
3640
3741 public function update (Request $ request , Post $ post )
3842 {
39- //нужно проверять аавтора, если пост существует
43+ $ this -> authorize ( ' isOwner ' , $ post );
4044
4145 $ request ->validate ([
4246 'title ' => 'required|string ' ,
@@ -54,6 +58,19 @@ public function update(Request $request, Post $post)
5458 return redirect ()->route ('post.edit ' , $ post );//пока сюда
5559 }
5660
61+ public function delete (Request $ request , Post $ post )
62+ {
63+ $ this ->authorize ('isOwner ' , $ post );
64+
65+ $ post ->delete ();
66+ $ this ->action = "replace " ;
67+
68+ //сюда поставить уведомление
69+
70+ return $ this ->list ($ request );//пока сюда
71+ }
72+
73+
5774 /**
5875 * @param \Illuminate\Http\Request $request
5976 *
@@ -63,13 +80,15 @@ public function list(Request $request)
6380 {
6481 $ posts = Post::with (['user ' ])
6582 ->withCount ('comments ' )
83+ ->when ($ request ->has ('user_id ' ), fn (Builder $ query ) => $ query ->where ('user_id ' , $ request ->get ('user_id ' )))
6684 ->orderBy ('id ' , 'desc ' )
67- ->simplePaginate ( 5 );
85+ ->cursorPaginate ( 3 );
6886
69- $ test = view ('post.list ' , [
70- 'posts ' => $ posts ,
87+ return view ('particles.posts.list ' , [
88+ 'posts ' => $ posts ,
89+ 'isMyProfile ' => $ request ->has ('user_id ' ) && $ request ->user ()?->id == $ request ->get ('user_id ' ),
90+ 'action ' => $ this ->action
7191 ])->fragmentsIf (!$ request ->isMethodSafe ());
72-
73- return $ test ;
7492 }
93+
7594}
0 commit comments