Skip to content

Commit 648aa42

Browse files
committed
wip
1 parent 77cb89f commit 648aa42

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

.env.example

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,3 @@ LOG_STACK=single
5353
SESSION_ENCRYPT=false
5454
SESSION_PATH=/
5555
SESSION_DOMAIN=null
56-
57-
APP_MAX_THREAD_COUNT = 5

app/Http/Controllers/Forum/ThreadsController.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,26 @@ public function show(Thread $thread)
8383

8484
public function create()
8585
{
86-
$tags = Tag::all();
87-
$selectedTags = old('tags') ?: [];
88-
89-
$threadCount = Auth::user()->threadsCountToday();
90-
if ($threadCount >= 5) {
86+
if (Auth::user()->hasTooManyThreadsToday()) {
9187
$this->error('You can only post a maximum of 5 threads per day.');
88+
9289
return redirect()->route('forum');
9390
}
9491

92+
$tags = Tag::all();
93+
$selectedTags = old('tags') ?: [];
94+
9595
return view('forum.threads.create', ['tags' => $tags, 'selectedTags' => $selectedTags]);
9696
}
9797

9898
public function store(ThreadRequest $request): RedirectResponse
9999
{
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()) {
105101
$this->error('You can only post a maximum of 5 threads per day.');
102+
106103
return redirect()->route('forum');
107104
}
108105

109-
110106
$this->dispatchSync(CreateThread::fromRequest($request, $uuid = Str::uuid()));
111107

112108
$thread = Thread::findByUuidOrFail($uuid);

app/Models/User.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,20 @@ public function countThreads(): int
197197
return $this->threadsRelation()->count();
198198
}
199199

200-
201-
public function threadsCountToday(): int
200+
public function countThreadsFromToday(): int
202201
{
202+
$today = Carbon::today();
203+
203204
return $this->threadsRelation()
204-
->whereDate('created_at', Carbon::today())
205+
->whereBetween('created_at', [$today, $today->copy()->endOfDay()])
205206
->count();
206207
}
207208

209+
public function hasTooManyThreadsToday(): bool
210+
{
211+
return $this->countThreadsFromToday() >= 5;
212+
}
213+
208214
/**
209215
* @return \Illuminate\Database\Eloquent\Collection
210216
*/

tests/Feature/ForumTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@
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+
86102
test('users can edit a thread', function () {
87103
$user = $this->createUser();
88104
$tag = Tag::factory()->create(['name' => 'Test Tag']);

0 commit comments

Comments
 (0)