Skip to content

Commit b226d9e

Browse files
committed
nits
1 parent 608c14f commit b226d9e

3 files changed

Lines changed: 6 additions & 26 deletions

File tree

app/Http/Controllers/ResourceEditsController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function store(ComputerScienceResource $computerScienceResource, StoreRes
4444

4545
$actualChanges = $this->calculateChanges($computerScienceResource, $proposedChanges);
4646

47+
// Add image path to the actual changes
4748
if (array_key_exists('image_file', $proposedChanges)) {
4849
$actualChanges['image_path'] = null;
4950
if (isset($proposedChanges['image_file'])) {
@@ -135,6 +136,8 @@ public function merge(ResourceEditsService $editsService, ResourceEdits $resourc
135136
$newFileName = Str::random(40).'.'.$fileExtension;
136137
$destPath = 'resource/'.$newFileName;
137138

139+
140+
// TODO: FIGURE OUT WHAT TO DO IN CASE OF EXCEPTION IN CODE FROM LATER STEPS
138141
Storage::disk('public')->move($sourcePath, $destPath);
139142
}
140143

@@ -145,11 +148,9 @@ public function merge(ResourceEditsService $editsService, ResourceEdits $resourc
145148
$resource->save();
146149

147150
$proposedTagFields = ['topic_tags', 'programming_language_tags', 'general_tags'];
148-
$allTags = [];
149151
foreach ($proposedTagFields as $field) {
150152
if (array_key_exists($field, $changes)) {
151153
$resource->$field = $changes[$field];
152-
$allTags[] = $changes[$field];
153154
}
154155
}
155156

app/Services/ComputerScienceResourceService.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,8 @@ public function createResource(array $validatedData): ComputerScienceResource
4141
// Store the image onto storage
4242
$path = null;
4343
if (array_key_exists('image_file', $validatedData) && $imageFile = $validatedData['image_file']) {
44+
// TODO: FIGURE OUT WHAT TO DO IN CASE OF EXCEPTION IN CODE FROM LATER STEPS
4445
$path = $imageFile->store('resource', 'public');
45-
if (! $path) {
46-
Log::error('Failed to store image file', [
47-
'user_id' => Auth::id(),
48-
'file_info' => $imageFile,
49-
]);
50-
51-
$fileName = $imageFile->getClientOriginalName();
52-
throw new \RuntimeException(
53-
"Could not save the image file '{$fileName}' for user ID ".Auth::id().'.'
54-
);
55-
}
5646
}
5747

5848
$resource = ComputerScienceResource::create([
@@ -94,18 +84,7 @@ public function createResource(array $validatedData): ComputerScienceResource
9484
return $resource;
9585
} catch (Throwable $e) {
9686
DB::rollBack();
97-
// Attempt to remove the uploaded image if it was stored
98-
if (isset($path) && $path) {
99-
try {
100-
Storage::disk('public')->delete($path);
101-
} catch (Throwable $removeEx) {
102-
Log::warning('Failed to remove image after exception', [
103-
'user_id' => Auth::id(),
104-
'image_path' => $path,
105-
'error' => $removeEx->getMessage(),
106-
]);
107-
}
108-
}
87+
10988
Log::critical('Failed to create resource', [
11089
'error' => $e->getMessage(),
11190
'trace' => $e->getTraceAsString(),

tests/Feature/ComputerScienceResourceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function test_image_is_removed_if_resource_creation_fails()
127127
$response->assertStatus(422); // Validation error
128128

129129
// The image should not exist in storage
130-
Storage::disk('public')->assertMissing('resource/'.$formData['image_file']->hashName());
130+
$this->assertEmpty(Storage::disk('public')->allFiles('resource'), "Failed asserting that no image files exist in the 'resource' directory after failed resource creation.");
131131
}
132132

133133
public function test_model_removes_image_upon_deletion()

0 commit comments

Comments
 (0)