-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathPostController.php
164 lines (136 loc) · 4.63 KB
/
PostController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace App\Http\Controllers;
use App\Models\Meet;
use App\Models\Post;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Orchid\Support\Facades\Toast;
class PostController extends Controller
{
/**
* @return \Illuminate\Contracts\View\View|\HotwiredLaravel\\Http\MultiplePendingTurboStreamResponse|\HotwiredLaravel\\Http\PendingTurboStreamResponse|null
*/
public function feed()
{
$popular = Post::withCount('likers')
->withCount('comments')
->orderBy('likers_count', 'desc')
->orderBy('created_at', 'desc')
->simplePaginate(5); // CursorPaginate не работает с сортировкой по полю из withCount
if (request()->isMethod('GET')) {
$posts = Post::with(['author'])
->withCount('comments')
->withCount('likers')
->orderBy('id', 'desc')
->cursorPaginate(3);
$most = Meet::approved()
->whereDate('start_date', '>=', now())
->orderBy('start_date', 'asc')
->first();
return view('post.index', [
'popular' => $popular,
'posts' => $posts,
'most' => $most,
]);
}
return turbo_stream([
turbo_stream()->append('popular-list', view('post._popular_list', [
'popular' => $popular,
])),
turbo_stream()->replace('popular-more', view('post._popular_pagination', [
'popular' => $popular,
])),
]);
}
/**
* @param \App\Models\Post $post
*
* @return \Illuminate\Contracts\View\View
*/
public function show(Post $post)
{
$post->load(['comments', 'author'])
->loadCount('comments')
->loadCount('likers');
return view('post.show', [
'post' => $post,
]);
}
/**
* @param \Illuminate\Http\Request $request
*
* @return \HotwiredLaravel\\Http\MultiplePendingTurboStreamResponse|\HotwiredLaravel\\Http\PendingTurboStreamResponse
*/
public function preview(Request $request)
{
return turbo_stream()->replace('post-preview', view('post.preview', [
'title' => $request->input('title', 'Заголовок'),
'content' => $request->input('content', 'Текст публикации'),
]));
}
public function edit(Request $request, Post $post)
{
$this->authorize('update', $post);
return view('post.edit', [
'post' => $post,
]);
}
/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\Post $post
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, Post $post)
{
$this->authorize('update', $post);
$request->validate([
'title' => 'required|string',
'content' => 'required|string',
]);
$post->fill([
'title' => $request->get('title'),
'content' => $request->get('content'),
'user_id' => $request->user()->id,
])->save();
Toast::success('Изменения успешно сохранены.')->disableAutoHide();
return redirect()->route('post.show', $post);
}
/**
* @param \Illuminate\Http\Request $request
* @param \App\Models\Post $post
*
* @return \Illuminate\Http\RedirectResponse
*/
public function delete(Request $request, Post $post)
{
$this->authorize('delete', $post);
$post->delete();
Toast::success('Публикация удалена.')->disableAutoHide();
return redirect()->route('feed');
}
/**
* @param \Illuminate\Http\Request $request
*
* @return string
*/
public function list(Request $request)
{
$posts = Post::with(['author'])
->withCount('comments')
->withCount('likers')
->when($request->has('user_id'), fn (Builder $query) => $query->where('user_id', $request->get('user_id')))
->orderBy('id', 'desc')
->cursorPaginate(3);
$request->user()?->attachLikeStatus($posts);
return turbo_stream([
turbo_stream()->removeAll('.post-placeholder'),
turbo_stream()->append('posts-frame', view('post.list', [
'posts' => $posts,
])),
turbo_stream()->replace('post-more', view('post.pagination', [
'posts' => $posts,
])),
]);
}
}