@@ -81,30 +81,27 @@ 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 ()
8585 {
8686 $ tags = Tag::all ();
8787 $ selectedTags = old ('tags ' ) ?: [];
8888
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+
8995 return view ('forum.threads.create ' , ['tags ' => $ tags , 'selectedTags ' => $ selectedTags ]);
9096 }
9197
9298 public function store (ThreadRequest $ request ): RedirectResponse
9399 {
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 ();
105102
106103 // Check if the user has reached the limit
107- if ($ threadCount >= getenv ( ' APP_MAX_THREAD_COUNT ' ) ) {
104+ if ($ threadCount >= 5 ) {
108105 $ this ->error ('You can only post a maximum of 5 threads per day. ' );
109106 return redirect ()->route ('forum ' );
110107 }
0 commit comments