66use App \Models \ComputerScienceResource ;
77use App \Models \ResourceEdits ;
88use App \Services \ResourceEditsService ;
9- use App \ Services \ UpvoteService ;
9+ use Illuminate \ Http \ Request ;
1010use Illuminate \Support \Facades \Auth ;
11- use Illuminate \Support \Facades \DB ;
1211use Illuminate \Support \Facades \Log ;
13- use Illuminate \Support \Facades \Storage ;
1412use Inertia \Inertia ;
15- use Str ;
1613use Throwable ;
1714
1815class ResourceEditsController extends Controller
1916{
2017 public function __construct (
21- protected ResourceEditsService $ resourceEditsService ,
22- protected UpvoteService $ upvoteService
18+ protected ResourceEditsService $ resourceEditsService
2319 ) {}
2420
2521 /**
@@ -41,38 +37,61 @@ public function create(string $slug)
4137 public function store (ComputerScienceResource $ computerScienceResource , StoreResourceEditRequest $ request )
4238 {
4339 $ validatedData = $ request ->validated ();
44- $ proposedChanges = $ validatedData ['proposed_changes ' ] ?? [];
45-
46- $ actualChanges = $ this ->resourceEditsService ->calculateChanges ($ computerScienceResource , $ proposedChanges );
47-
48- // Add image path to the actual changes
49- if (array_key_exists ('image_file ' , $ proposedChanges )) {
50- $ actualChanges ['image_path ' ] = null ;
51- if (isset ($ proposedChanges ['image_file ' ])) {
52- $ path = $ proposedChanges ['image_file ' ]->store ('resource-edits ' , 'public ' );
53- $ actualChanges ['image_path ' ] = $ path ;
54- }
55- unset($ actualChanges ['image_file ' ]);
56- }
5740
58- if (empty ($ actualChanges )) {
59- 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+ ]);
6059
6160 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. ' ]);
6271 }
72+ }
6373
64- $ resourceEdit = ResourceEdits::create ([
65- 'user_id ' => Auth::id (),
66- 'computer_science_resource_id ' => $ computerScienceResource ->id ,
67- 'edit_title ' => $ validatedData ['edit_title ' ],
68- 'edit_description ' => $ validatedData ['edit_description ' ],
69- 'proposed_changes ' => $ actualChanges ,
70- ]);
74+ public function index (Request $ request )
75+ {
76+ try {
77+ $ data = $ this ->resourceEditsService ->getIndexData ($ request );
7178
72- $ this ->upvoteService ->upvote ('edit ' , $ resourceEdit ->id );
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+ ]);
7387
74- return redirect ()->route ('resource_edits.show ' , ['slug ' => $ resourceEdit ->slug ])
75- ->with ('success ' , 'Edits Created! ' );
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+ }
7695 }
7796
7897 public function show (string $ slug )
@@ -87,57 +106,10 @@ public function show(string $slug)
87106 ]);
88107 }
89108
90- public function merge (ResourceEditsService $ editsService , ResourceEdits $ resourceEdits )
109+ public function merge (ResourceEdits $ resourceEdits )
91110 {
92- if (! $ editsService ->canMergeEdits ($ resourceEdits )) {
93- return redirect ()->back ()->with ('warning ' , 'Not enough approvals ' );
94- }
95-
96- DB ::beginTransaction ();
97111 try {
98- $ resource = ComputerScienceResource::findOrFail ($ resourceEdits ->computer_science_resource_id );
99-
100- // Go through each property in proposed_changes, and if it exists. then set the value
101- $ changes = $ resourceEdits ->proposed_changes ;
102- $ proposedFields = ['name ' , 'description ' , 'page_url ' , 'platforms ' , 'difficulties ' , 'pricing ' ];
103- foreach ($ proposedFields as $ field ) {
104- if (array_key_exists ($ field , $ changes )) {
105- $ resource ->$ field = $ changes [$ field ];
106- }
107- }
108-
109- if (array_key_exists ('image_path ' , $ changes )) {
110- if ($ resource ->image_path ) {
111- Storage::disk ('public ' )->delete ($ resource ->image_path );
112- }
113- $ destPath = null ;
114- if (isset ($ changes ['image_path ' ])) {
115- // Move the new file from 'resource-edits' to 'resource'
116- $ sourcePath = $ changes ['image_path ' ];
117- $ fileExtension = pathinfo ($ sourcePath , PATHINFO_EXTENSION );
118- $ newFileName = Str::random (40 ).'. ' .$ fileExtension ;
119- $ destPath = 'resource/ ' .$ newFileName ;
120-
121- // TODO: FIGURE OUT WHAT TO DO IN CASE OF EXCEPTION IN CODE FROM LATER STEPS
122- Storage::disk ('public ' )->move ($ sourcePath , $ destPath );
123- }
124- // Update image_path in DB
125- $ resource ->image_path = $ destPath ;
126- }
127-
128- $ resource ->save ();
129-
130- $ proposedTagFields = ['topics_tags ' , 'programming_languages_tags ' , 'general_tags ' ];
131- foreach ($ proposedTagFields as $ field ) {
132- if (array_key_exists ($ field , $ changes )) {
133- $ resource ->$ field = $ changes [$ field ];
134- }
135- }
136-
137- // Delete the edit since we successfully merged the changes
138- $ resourceEdits ->delete ();
139-
140- DB ::commit ();
112+ $ resource = $ this ->resourceEditsService ->mergeResourceEdit ($ resourceEdits );
141113
142114 Log::info ('Resource edit merged ' , [
143115 'resource_id ' => $ resource ->id ,
@@ -149,12 +121,20 @@ public function merge(ResourceEditsService $editsService, ResourceEdits $resourc
149121
150122 return redirect (route ('resources.show ' , ['slug ' => $ resource ->slug ]))
151123 ->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 ' );
152132 } catch (Throwable $ e ) {
153- DB ::rollBack ();
154133 Log::critical ('Failed to merge resource edits ' , [
155134 'error ' => $ e ->getMessage (),
156135 'trace ' => $ e ->getTraceAsString (),
157136 'resource_edit_id ' => $ resourceEdits ->id ,
137+ 'user_id ' => Auth::id (),
158138 ]);
159139
160140 return redirect ()->back ()->withErrors (['error ' => 'Failed to merge resource edits. Please try again. ' ]);
0 commit comments