File tree Expand file tree Collapse file tree 3 files changed +44
-1
lines changed Expand file tree Collapse file tree 3 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -81,8 +81,14 @@ public function show(Thread $thread)
8181 return view ('forum.threads.show ' , compact ('thread ' , 'moderators ' ));
8282 }
8383
84- public function create (): View
84+ public function create (): RedirectResponse | View
8585 {
86+ if (Auth::user ()->hasTooManyThreadsToday ()) {
87+ $ this ->error ('You can only post a maximum of 5 threads per day. ' );
88+
89+ return redirect ()->route ('forum ' );
90+ }
91+
8692 $ tags = Tag::all ();
8793 $ selectedTags = old ('tags ' ) ?: [];
8894
@@ -91,6 +97,12 @@ public function create(): View
9197
9298 public function store (ThreadRequest $ request ): RedirectResponse
9399 {
100+ if (Auth::user ()->hasTooManyThreadsToday ()) {
101+ $ this ->error ('You can only post a maximum of 5 threads per day. ' );
102+
103+ return redirect ()->route ('forum ' );
104+ }
105+
94106 $ this ->dispatchSync (CreateThread::fromRequest ($ request , $ uuid = Str::uuid ()));
95107
96108 $ thread = Thread::findByUuidOrFail ($ uuid );
Original file line number Diff line number Diff line change 55use App \Concerns \HasTimestamps ;
66use App \Concerns \PreparesSearch ;
77use App \Enums \NotificationType ;
8+ use Carbon \Carbon ;
89use Illuminate \Contracts \Auth \MustVerifyEmail ;
910use Illuminate \Database \Eloquent \Builder ;
1011use Illuminate \Database \Eloquent \Factories \HasFactory ;
@@ -196,6 +197,20 @@ public function countThreads(): int
196197 return $ this ->threadsRelation ()->count ();
197198 }
198199
200+ public function countThreadsFromToday (): int
201+ {
202+ $ today = Carbon::today ();
203+
204+ return $ this ->threadsRelation ()
205+ ->whereBetween ('created_at ' , [$ today , $ today ->copy ()->endOfDay ()])
206+ ->count ();
207+ }
208+
209+ public function hasTooManyThreadsToday (): bool
210+ {
211+ return $ this ->countThreadsFromToday () >= 5 ;
212+ }
213+
199214 /**
200215 * @return \Illuminate\Database\Eloquent\Collection
201216 */
Original file line number Diff line number Diff line change 8383 ->assertSessionHas ('success ' , 'Thread successfully created! ' );
8484});
8585
86+ test ('users cannot create more than 5 threads per day ' , function () {
87+ $ tag = Tag::factory ()->create (['name ' => 'Test Tag ' ]);
88+
89+ $ user = $ this ->login ();
90+
91+ Thread::factory ()->count (5 )->create (['author_id ' => $ user ->id (), 'created_at ' => now ()]);
92+
93+ $ this ->post ('/forum/create-thread ' , [
94+ 'subject ' => 'How to work with Eloquent? ' ,
95+ 'body ' => 'This text explains how to work with Eloquent. ' ,
96+ 'tags ' => [$ tag ->id ()],
97+ ])
98+ ->assertRedirect ('/forum ' )
99+ ->assertSessionHas ('error ' , 'You can only post a maximum of 5 threads per day. ' );
100+ })->only ();
101+
86102test ('users can edit a thread ' , function () {
87103 $ user = $ this ->createUser ();
88104 $ tag = Tag::factory ()->create (['name ' => 'Test Tag ' ]);
You can’t perform that action at this time.
0 commit comments