@@ -81,30 +81,27 @@ public function show(Thread $thread)
81
81
return view ('forum.threads.show ' , compact ('thread ' , 'moderators ' ));
82
82
}
83
83
84
- public function create (): View
84
+ public function create ()
85
85
{
86
86
$ tags = Tag::all ();
87
87
$ selectedTags = old ('tags ' ) ?: [];
88
88
89
+ $ threadCount = Auth::user ()->threadsCountToday ();
90
+ if ($ threadCount >= 5 ) {
91
+ $ this ->error ('You can only post a maximum of 5 threads per day. ' );
92
+ return redirect ()->route ('forum ' );
93
+ }
94
+
89
95
return view ('forum.threads.create ' , ['tags ' => $ tags , 'selectedTags ' => $ selectedTags ]);
90
96
}
91
97
92
98
public function store (ThreadRequest $ request ): RedirectResponse
93
99
{
94
- $ userId = Auth::id (); // Get the authenticated user's ID
95
- $ midnight = now ()->endOfDay ();
96
- $ remainingSeconds = $ midnight ->diffInSeconds (now ());
97
-
98
- // Count the threads posted by the user today
99
- $ cacheKey = "user_threads_count_ {$ userId }" ;
100
- $ threadCount = Cache::remember ($ cacheKey , $ remainingSeconds , function () use ($ userId ) {
101
- return Thread::where ('author_id ' , $ userId )
102
- ->where ('created_at ' , '>= ' , now ()->startOfDay ())
103
- ->count ();
104
- });
100
+
101
+ $ threadCount = Auth::user ()->threadsCountToday ();
105
102
106
103
// Check if the user has reached the limit
107
- if ($ threadCount >= getenv ( ' APP_MAX_THREAD_COUNT ' ) ) {
104
+ if ($ threadCount >= 5 ) {
108
105
$ this ->error ('You can only post a maximum of 5 threads per day. ' );
109
106
return redirect ()->route ('forum ' );
110
107
}
0 commit comments