Support deletion of attachments
MODIFY
app/Http/Controllers/AttachmentController.php
app/Http/Controllers/AttachmentController.php
@@ -34,4 +34,21 @@ class AttachmentController extends Controller
return redirect('/' . $transactionType . 's/' . $transactionId);
}
+
+ public function delete(Request $request, string $id)
+ {
+ $attachment = $this->attachmentRepository->getById($id);
+
+ if (!$attachment) {
+ abort(404);
+ }
+
+ // Memorize some details before we delete it
+ $transactionType = $attachment->transaction_type;
+ $transactionId = $attachment->transaction_id;
+
+ $this->attachmentRepository->delete($id);
+
+ return redirect('/' . $transactionType . 's/' . $transactionId);
+ }
}

MODIFY
app/Repositories/AttachmentRepository.php
app/Repositories/AttachmentRepository.php
@@ -7,6 +7,11 @@ use Exception;
class AttachmentRepository
{
+ public function getById(int $id): ?Attachment
+ {
+ return Attachment::find($id);
+ }
+
public function create(string $transactionType, int $transactionId, string $filePath): Attachment
{
if ($transactionType !== 'earning' && $transactionType !== 'spending') {
@@ -19,4 +24,15 @@ class AttachmentRepository
'file_path' => $filePath
]);
}
+
+ public function delete(int $id): void
+ {
+ $attachment = $this->getById($id);
+
+ if (!$attachment) {
+ throw new Exception('Could not find attachment with ID ' . $id);
+ }
+
+ $attachment->delete();
+ }
}

MODIFY
resources/views/partials/attachments.blade.php
resources/views/partials/attachments.blade.php
<div class="box__section text-center">No attachments for this transaction</div>
@endif
@foreach ($payload->attachments as $attachment)
- <div class="box__section">
+ <div class="box__section row">
<img src="{{ $attachment->file_b64 }}" style="max-width: 100%; max-height: 200px; border-radius: 5px; vertical-align: top;" />
+ <div class="ml-2">
+ <form method="POST" action="/attachments/{{ $attachment->id }}/delete">
+ {{ csrf_field() }}
+ <button class="button link">
+ <i class="fas fa-trash-alt"></i>
+ </button>
+ </form>
+ </div>
</div>
@endforeach
</div>

MODIFY
routes/web.php
routes/web.php
@@ -17,6 +17,7 @@ Route::group(['middleware' => ['auth']], function () {
Route::get('/dashboard', 'DashboardController')->name('dashboard');
Route::post('/attachments', 'AttachmentController@store');
+ Route::post('/attachments/{id}/delete', 'AttachmentController@delete');
Route::get('/transactions/create', 'TransactionController@create')->name('transactions.create');

Support deletion of attachments