66use App \Models \ComputerScienceResource ;
77use App \Models \ResourceEdits ;
88use App \Services \ResourceEditsService ;
9+ use Illuminate \Http \Request ;
910use Illuminate \Support \Facades \Auth ;
10- use Illuminate \Support \Facades \DB ;
1111use Illuminate \Support \Facades \Log ;
12- use Illuminate \Support \Facades \Storage ;
1312use Inertia \Inertia ;
14- use Str ;
1513use Throwable ;
1614
1715class ResourceEditsController extends Controller
@@ -39,36 +37,61 @@ public function create(string $slug)
3937 public function store (ComputerScienceResource $ computerScienceResource , StoreResourceEditRequest $ request )
4038 {
4139 $ validatedData = $ request ->validated ();
42- $ proposedChanges = $ validatedData ['proposed_changes ' ] ?? [];
43-
44- $ actualChanges = $ this ->resourceEditsService ->calculateChanges ($ computerScienceResource , $ proposedChanges );
45-
46- // Add image path to the actual changes
47- if (array_key_exists ('image_file ' , $ proposedChanges )) {
48- $ actualChanges ['image_path ' ] = null ;
49- if (isset ($ proposedChanges ['image_file ' ])) {
50- $ path = $ proposedChanges ['image_file ' ]->store ('resource-edits ' , 'public ' );
51- $ actualChanges ['image_path ' ] = $ path ;
52- }
53- unset($ actualChanges ['image_file ' ]);
54- }
5540
56- if (empty ($ actualChanges )) {
57- Log::warning ("Resource edit was submitted without any changes for resource ID: {$ computerScienceResource ->id }" );
41+ try {
42+ $ resourceEdit = $ this ->resourceEditsService ->createResourceEdit ($ computerScienceResource , $ validatedData );
43+
44+ Log::info ('Resource edit created ' , [
45+ 'resource_edit_id ' => $ resourceEdit ->id ,
46+ 'resource_id ' => $ computerScienceResource ->id ,
47+ 'user_id ' => Auth::id (),
48+ 'edit_title ' => $ resourceEdit ->edit_title ,
49+ ]);
50+
51+ return redirect ()->route ('resource_edits.show ' , ['slug ' => $ resourceEdit ->slug ])
52+ ->with ('success ' , 'Edits Created! ' );
53+ } catch (\InvalidArgumentException $ e ) {
54+ Log::warning ('Resource edit submitted with no changes ' , [
55+ 'resource_id ' => $ computerScienceResource ->id ,
56+ 'user_id ' => Auth::id (),
57+ 'error ' => $ e ->getMessage (),
58+ ]);
5859
5960 return redirect ()->back ()->with ('warning ' , 'Cannot submit an edit with no changes made. ' );
61+ } catch (Throwable $ e ) {
62+ Log::critical ('Failed to create resource edit ' , [
63+ 'error ' => $ e ->getMessage (),
64+ 'trace ' => $ e ->getTraceAsString (),
65+ 'resource_id ' => $ computerScienceResource ->id ,
66+ 'user_id ' => Auth::id (),
67+ 'data ' => $ validatedData ,
68+ ]);
69+
70+ return redirect ()->back ()->withErrors (['error ' => 'Failed to create resource edit. Please try again. ' ]);
6071 }
72+ }
6173
62- $ resourceEdit = ResourceEdits::create ([
63- 'user_id ' => Auth::id (),
64- 'computer_science_resource_id ' => $ computerScienceResource ->id ,
65- 'edit_title ' => $ validatedData ['edit_title ' ],
66- 'edit_description ' => $ validatedData ['edit_description ' ],
67- 'proposed_changes ' => $ actualChanges ,
68- ]);
74+ public function index (Request $ request )
75+ {
76+ try {
77+ $ data = $ this ->resourceEditsService ->getIndexData ($ request );
6978
70- return redirect ()->route ('resource_edits.show ' , ['slug ' => $ resourceEdit ->slug ])
71- ->with ('success ' , 'Edits Created! ' );
79+ return Inertia::render ('ResourceEdits/Index ' , $ data );
80+ } catch (Throwable $ e ) {
81+ Log::error ('Error loading resource edits index ' , [
82+ 'user_id ' => Auth::id (),
83+ 'query ' => $ request ->query (),
84+ 'error ' => $ e ->getMessage (),
85+ 'trace ' => $ e ->getTraceAsString (),
86+ ]);
87+
88+ // Return an empty page with an error flash so the UI can show a message
89+ session ()->flash ('error ' , 'Unable to load resource edits right now. ' );
90+
91+ return Inertia::render ('ResourceEdits/Index ' , [
92+ 'resource_edits ' => ResourceEdits::query ()->paginate (1 ),
93+ ]);
94+ }
7295 }
7396
7497 public function show (string $ slug )
@@ -83,57 +106,10 @@ public function show(string $slug)
83106 ]);
84107 }
85108
86- public function merge (ResourceEditsService $ editsService , ResourceEdits $ resourceEdits )
109+ public function merge (ResourceEdits $ resourceEdits )
87110 {
88- if (! $ editsService ->canMergeEdits ($ resourceEdits )) {
89- return redirect ()->back ()->with ('warning ' , 'Not enough approvals ' );
90- }
91-
92- DB ::beginTransaction ();
93111 try {
94- $ resource = ComputerScienceResource::findOrFail ($ resourceEdits ->computer_science_resource_id );
95-
96- // Go through each property in proposed_changes, and if it exists. then set the value
97- $ changes = $ resourceEdits ->proposed_changes ;
98- $ proposedFields = ['name ' , 'description ' , 'page_url ' , 'platforms ' , 'difficulties ' , 'pricing ' ];
99- foreach ($ proposedFields as $ field ) {
100- if (array_key_exists ($ field , $ changes )) {
101- $ resource ->$ field = $ changes [$ field ];
102- }
103- }
104-
105- if (array_key_exists ('image_path ' , $ changes )) {
106- if ($ resource ->image_path ) {
107- Storage::disk ('public ' )->delete ($ resource ->image_path );
108- }
109- $ destPath = null ;
110- if (isset ($ changes ['image_path ' ])) {
111- // Move the new file from 'resource-edits' to 'resource'
112- $ sourcePath = $ changes ['image_path ' ];
113- $ fileExtension = pathinfo ($ sourcePath , PATHINFO_EXTENSION );
114- $ newFileName = Str::random (40 ).'. ' .$ fileExtension ;
115- $ destPath = 'resource/ ' .$ newFileName ;
116-
117- // TODO: FIGURE OUT WHAT TO DO IN CASE OF EXCEPTION IN CODE FROM LATER STEPS
118- Storage::disk ('public ' )->move ($ sourcePath , $ destPath );
119- }
120- // Update image_path in DB
121- $ resource ->image_path = $ destPath ;
122- }
123-
124- $ resource ->save ();
125-
126- $ proposedTagFields = ['topics_tags ' , 'programming_languages_tags ' , 'general_tags ' ];
127- foreach ($ proposedTagFields as $ field ) {
128- if (array_key_exists ($ field , $ changes )) {
129- $ resource ->$ field = $ changes [$ field ];
130- }
131- }
132-
133- // Delete the edit since we successfully merged the changes
134- $ resourceEdits ->delete ();
135-
136- DB ::commit ();
112+ $ resource = $ this ->resourceEditsService ->mergeResourceEdit ($ resourceEdits );
137113
138114 Log::info ('Resource edit merged ' , [
139115 'resource_id ' => $ resource ->id ,
@@ -145,12 +121,20 @@ public function merge(ResourceEditsService $editsService, ResourceEdits $resourc
145121
146122 return redirect (route ('resources.show ' , ['slug ' => $ resource ->slug ]))
147123 ->with ('success ' , 'Successfully Merged Changes! ' );
124+ } catch (\LogicException $ e ) {
125+ Log::warning ('Insufficient approvals for resource edit merge ' , [
126+ 'resource_edit_id ' => $ resourceEdits ->id ,
127+ 'user_id ' => Auth::id (),
128+ 'error ' => $ e ->getMessage (),
129+ ]);
130+
131+ return redirect ()->back ()->with ('warning ' , 'Not enough approvals ' );
148132 } catch (Throwable $ e ) {
149- DB ::rollBack ();
150133 Log::critical ('Failed to merge resource edits ' , [
151134 'error ' => $ e ->getMessage (),
152135 'trace ' => $ e ->getTraceAsString (),
153136 'resource_edit_id ' => $ resourceEdits ->id ,
137+ 'user_id ' => Auth::id (),
154138 ]);
155139
156140 return redirect ()->back ()->withErrors (['error ' => 'Failed to merge resource edits. Please try again. ' ]);
0 commit comments