22
33namespace App \Http \Controllers ;
44
5+ use App \Exceptions \Resources \ResourceAlreadyCreatedException ;
6+ use App \Exceptions \Resources \ResourceInvalidTabException ;
57use App \Http \Requests \StoreResourceRequest ;
68use App \Models \ComputerScienceResource ;
79use App \Models \NewsPost ;
8- use App \Models \ResourceEdits ;
9- use App \Models \ResourceReview ;
10- use App \Services \CommentService ;
1110use App \Services \ComputerScienceResourceFilter ;
12- use App \Services \ResourceReviewService ;
13- use App \Services \SortingManagers \GeneralVotesSortingManager ;
14- use App \Services \SortingManagers \ResourceSortingManager ;
15- use App \Services \UpvoteService ;
11+ use App \Services \ComputerScienceResourceService ;
1612use Illuminate \Http \Request ;
1713use Illuminate \Support \Facades \Auth ;
18- use Illuminate \Support \Facades \DB ;
1914use Illuminate \Support \Facades \Log ;
20- use Illuminate \Support \Facades \Storage ;
2115use Inertia \Inertia ;
2216use Throwable ;
2317
2418class ComputerScienceResourceController extends Controller
2519{
2620 public function __construct (
27- protected CommentService $ commentService ,
28- protected GeneralVotesSortingManager $ generalVotesSortingManager ,
29- protected ResourceReviewService $ reviewService ,
30- protected ResourceSortingManager $ resourceSortingManager ,
31- protected UpvoteService $ upvoteService ,
21+ protected ComputerScienceResourceService $ resourceService ,
3222 ) {}
3323
3424 /**
@@ -67,86 +57,27 @@ public function create()
6757 public function store (StoreResourceRequest $ request )
6858 {
6959 $ validatedData = $ request ->validated ();
70-
71- DB ::beginTransaction ();
7260 try {
73- // Store the image onto storage
74- $ path = null ;
75- if (array_key_exists ('image_file ' , $ validatedData ) && $ imageFile = $ validatedData ['image_file ' ]) {
76- $ path = $ imageFile ->store ('resource ' , 'public ' );
77- if (! $ path ) {
78- Log::error ('Failed to store image file ' , [
79- 'user_id ' => Auth::id (),
80- 'file_info ' => $ imageFile ,
81- ]);
82-
83- $ fileName = $ imageFile ->getClientOriginalName ();
84- throw new \RuntimeException (
85- "Could not save the image file ' {$ fileName }' for user ID " .Auth::id ().'. '
86- );
87- }
88- }
89-
90- $ resource = ComputerScienceResource::create ([
61+ $ resource = $ this ->resourceService ->createResource ($ validatedData );
62+ session ()->flash ('success ' , 'Created Resource! ' );
63+ return response ()->json ($ resource );
64+ }
65+ catch (ResourceAlreadyCreatedException $ e )
66+ {
67+ Log::warning ('Resource already exists ' , [
9168 'user_id ' => Auth::id (),
92- 'name ' => $ validatedData ['name ' ],
93- 'image_path ' => $ path ,
94- 'description ' => $ validatedData ['description ' ],
95- 'page_url ' => $ validatedData ['page_url ' ],
96- 'platforms ' => $ validatedData ['platforms ' ],
97- 'difficulty ' => $ validatedData ['difficulty ' ],
98- 'pricing ' => $ validatedData ['pricing ' ],
69+ 'resource_id ' => $ e ->resource ->id ?? null ,
70+ 'name ' => $ e ->resource ->name ?? null ,
9971 ]);
100-
101- // Add topics as tags
102- $ resource ->topic_tags = $ validatedData ['topic_tags ' ];
103-
104- // Add programming languages as tags (if provided)
105- if (isset ($ validatedData ['programming_language_tags ' ])) {
106- $ resource ->programming_language_tags = $ validatedData ['programming_language_tags ' ];
107- }
108-
109- // Add general tags (if provided)
110- if (isset ($ validatedData ['general_tags ' ])) {
111- $ resource ->general_tags = $ validatedData ['general_tags ' ];
112- }
113-
114- DB ::commit ();
115-
116- $ this ->upvoteService ->upvote ('resource ' , $ resource ->id );
117-
118- Log::info ('Resource created ' , [
119- 'resource_id ' => $ resource ->id ,
72+ session ()->flash ('warning ' , 'Resource Already Exists! ' );
73+ return response ()->json ($ e ->resource );
74+ }
75+ catch (Throwable $ e ) {
76+ Log::error ('Error creating resource ' , [
12077 'user_id ' => Auth::id (),
121- 'name ' => $ resource ->name ,
122- 'slug ' => $ resource ->slug ,
123- 'platforms ' => $ resource ->platforms ,
124- ]);
125-
126- session ()->flash ('success ' , 'Created Resource! ' );
127-
128- return response ()->json ($ resource );
129- } catch (Throwable $ e ) {
130- DB ::rollBack ();
131- // Attempt to remove the uploaded image if it was stored
132- if (isset ($ path ) && $ path ) {
133- try {
134- Storage::disk ('public ' )->delete ($ path );
135- } catch (Throwable $ removeEx ) {
136- Log::warning ('Failed to remove image after exception ' , [
137- 'user_id ' => Auth::id (),
138- 'image_path ' => $ path ,
139- 'error ' => $ removeEx ->getMessage (),
140- ]);
141- }
142- }
143- Log::critical ('Failed to create resource ' , [
14478 'error ' => $ e ->getMessage (),
14579 'trace ' => $ e ->getTraceAsString (),
146- 'user_id ' => Auth::id (),
147- 'data ' => $ validatedData ,
14880 ]);
149-
15081 return response ()->json ([], 500 );
15182 }
15283 }
@@ -156,62 +87,22 @@ public function store(StoreResourceRequest $request)
15687 */
15788 public function show (Request $ request , string $ slug , string $ tab = 'reviews ' )
15889 {
159- $ computerScienceResource = ComputerScienceResource::where ('slug ' , $ slug )->firstOrFail ();
160- // Get the review summaries
161- $ computerScienceResource ->load ('reviewSummary ' );
162- $ computerScienceResource ->load ('user ' );
163-
164- $ validTabs = ['reviews ' , 'discussion ' , 'edits ' ];
90+ try {
91+ $ result = $ this ->resourceService ->getShowResourceData ($ request , $ slug , $ tab );
92+ return Inertia::render ('Resources/Show ' , $ result );
93+ } catch (ResourceInvalidTabException $ e ) {
94+ Log::warning ('Invalid resource tab requested ' , [
95+ 'user_id ' => Auth::id (),
96+ 'slug ' => $ slug ,
97+ 'requested_tab ' => $ tab ,
98+ 'error ' => $ e ->getMessage (),
99+ ]);
165100
166- if (! in_array ($ tab , $ validTabs )) {
167- // Redirect to default if invalid
168101 return redirect ()->route ('resources.show ' , [
169- 'slug ' => $ computerScienceResource -> slug ,
102+ 'slug ' => $ slug ,
170103 'tab ' => 'reviews ' ,
171- ]);
104+ ])-> with ( ' warning ' , ' Invalid tab requested, redirected to reviews. ' ) ;
172105 }
173-
174- // return the resource and tab
175- $ data = [
176- 'tab ' => $ tab ,
177- 'resource ' => $ computerScienceResource ,
178- ];
179-
180- $ sortBy = $ request ->query ('sort_by ' , 'top ' );
181- // Load only the necessary tab data
182- if ($ tab === 'reviews ' ) {
183- $ userReview = null ;
184- if ($ userId = Auth::id ()) {
185- $ userReview = ResourceReview::whereBelongsTo ($ computerScienceResource )
186- ->firstWhere ('user_id ' , $ userId );
187- }
188-
189- $ data ['userReview ' ] = $ userReview ;
190-
191- $ data ['reviews ' ] = Inertia::defer (
192- function () use ($ computerScienceResource , $ sortBy , $ request ) {
193- $ query = ResourceReview::whereBelongsTo ($ computerScienceResource );
194- $ query = $ this ->generalVotesSortingManager ->applySort ($ query , $ sortBy , ResourceReview::class);
195-
196- return $ query ->with ('user ' )->paginate (10 )->appends ($ request ->query ());
197- }
198- );
199- } elseif ($ tab === 'edits ' ) {
200- $ data ['resourceEdits ' ] = Inertia::defer (
201- function () use ($ computerScienceResource , $ sortBy , $ request ) {
202- $ query = ResourceEdits::whereBelongsTo ($ computerScienceResource );
203- $ query = $ this ->generalVotesSortingManager ->applySort ($ query , $ sortBy , ResourceEdits::class);
204-
205- return $ query ->with ('user ' )->paginate (10 )->appends ($ request ->query ());
206- }
207- );
208- } elseif ($ tab === 'discussion ' ) {
209- $ data ['discussion ' ] = Inertia::defer (
210- fn () => $ this ->commentService ->getPaginatedComments ('resource ' , $ computerScienceResource ->id , 0 , 150 , $ sortBy )
211- );
212- }
213-
214- return Inertia::render ('Resources/Show ' , $ data );
215106 }
216107
217108 /**
0 commit comments