File tree Expand file tree Collapse file tree 4 files changed +32
-16
lines changed Expand file tree Collapse file tree 4 files changed +32
-16
lines changed Original file line number Diff line number Diff line change @@ -53,5 +53,3 @@ LOG_STACK=single
53
53
SESSION_ENCRYPT = false
54
54
SESSION_PATH = /
55
55
SESSION_DOMAIN = null
56
-
57
- APP_MAX_THREAD_COUNT = 5
Original file line number Diff line number Diff line change @@ -83,30 +83,26 @@ public function show(Thread $thread)
83
83
84
84
public function create ()
85
85
{
86
- $ tags = Tag::all ();
87
- $ selectedTags = old ('tags ' ) ?: [];
88
-
89
- $ threadCount = Auth::user ()->threadsCountToday ();
90
- if ($ threadCount >= 5 ) {
86
+ if (Auth::user ()->hasTooManyThreadsToday ()) {
91
87
$ this ->error ('You can only post a maximum of 5 threads per day. ' );
88
+
92
89
return redirect ()->route ('forum ' );
93
90
}
94
91
92
+ $ tags = Tag::all ();
93
+ $ selectedTags = old ('tags ' ) ?: [];
94
+
95
95
return view ('forum.threads.create ' , ['tags ' => $ tags , 'selectedTags ' => $ selectedTags ]);
96
96
}
97
97
98
98
public function store (ThreadRequest $ request ): RedirectResponse
99
99
{
100
-
101
- $ threadCount = Auth::user ()->threadsCountToday ();
102
-
103
- // Check if the user has reached the limit
104
- if ($ threadCount >= 5 ) {
100
+ if (Auth::user ()->hasTooManyThreadsToday ()) {
105
101
$ this ->error ('You can only post a maximum of 5 threads per day. ' );
102
+
106
103
return redirect ()->route ('forum ' );
107
104
}
108
105
109
-
110
106
$ this ->dispatchSync (CreateThread::fromRequest ($ request , $ uuid = Str::uuid ()));
111
107
112
108
$ thread = Thread::findByUuidOrFail ($ uuid );
Original file line number Diff line number Diff line change @@ -197,14 +197,20 @@ public function countThreads(): int
197
197
return $ this ->threadsRelation ()->count ();
198
198
}
199
199
200
-
201
- public function threadsCountToday (): int
200
+ public function countThreadsFromToday (): int
202
201
{
202
+ $ today = Carbon::today ();
203
+
203
204
return $ this ->threadsRelation ()
204
- ->whereDate ('created_at ' , Carbon:: today () )
205
+ ->whereBetween ('created_at ' , [ $ today, $ today -> copy ()-> endOfDay ()] )
205
206
->count ();
206
207
}
207
208
209
+ public function hasTooManyThreadsToday (): bool
210
+ {
211
+ return $ this ->countThreadsFromToday () >= 5 ;
212
+ }
213
+
208
214
/**
209
215
* @return \Illuminate\Database\Eloquent\Collection
210
216
*/
Original file line number Diff line number Diff line change 83
83
->assertSessionHas ('success ' , 'Thread successfully created! ' );
84
84
});
85
85
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
+
86
102
test ('users can edit a thread ' , function () {
87
103
$ user = $ this ->createUser ();
88
104
$ tag = Tag::factory ()->create (['name ' => 'Test Tag ' ]);
You can’t perform that action at this time.
0 commit comments