Skip to content

Commit

Permalink
Vsl30 del 435 bulk delete attachments (#39)
Browse files Browse the repository at this point in the history
* Bulk delete of attachments

* Bulk delete of attachments

* Apply fixes from StyleCI

* Bulk delete of attachments

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
DevFajdetic and StyleCIBot authored Aug 2, 2024
1 parent 40f5763 commit e435256
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
Route::prefix(config('asseco-attachments.routes.prefix'))
->middleware(config('asseco-attachments.routes.middleware'))
->group(function () {
Route::delete('attachments/bulk-delete', [AttachmentController::class, 'bulkDelete'])->name('attachments.bulkDelete');

Route::apiResource('attachments', AttachmentController::class);

Route::get('attachments/{attachment}/download', [AttachmentController::class, 'download'])->name('attachments.download');
Expand Down
40 changes: 39 additions & 1 deletion src/App/Http/Controllers/AttachmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Asseco\Attachments\App\Contracts\Attachment as AttachmentContract;
use Asseco\Attachments\App\Http\Requests\AttachmentRequest;
use Asseco\Attachments\App\Http\Requests\AttachmentUpdateRequest;
use Asseco\Attachments\App\Http\Requests\DeleteAttachmentsRequest;
use Asseco\Attachments\App\Models\Attachment;
use Asseco\Attachments\App\Service\CachedUploads;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class AttachmentController extends Controller
Expand Down Expand Up @@ -67,7 +69,7 @@ public function show(Attachment $attachment): JsonResponse
* Update the specified resource.
*
* @param Attachment $attachment
* @param AttachmentRequest $request
* @param AttachmentUpdateRequest $request
* @return JsonResponse
*/
public function update(Attachment $attachment, AttachmentUpdateRequest $request): JsonResponse
Expand Down Expand Up @@ -97,4 +99,40 @@ public function download(Attachment $attachment)
{
return Storage::download($attachment->path, $attachment->name);
}

/**
* Remove multiple resources from storage.
*
* @param DeleteAttachmentsRequest $request
* @return JsonResponse
*/
public function bulkDelete(DeleteAttachmentsRequest $request): JsonResponse
{
$attachmentIds = $request->input('attachment_ids');
$deleted = [];
$notDeleted = [];
$paths = Attachment::whereIn('id', $attachmentIds)->pluck('path', 'id');
foreach ($attachmentIds as $id) {
$path = $paths->get($id);
try {
Attachment::where('id', $id)->delete();
if ($path) {
Storage::delete($path);
}
$deleted[] = $id;
} catch (Exception $e) {
$notDeleted[] = $id;
Log::error('Failed to delete attachment', [
'attachment_id' => $id,
'exception' => $e,
]);
}
}

return response()->json([
'message' => 'Bulk delete operation completed',
'deleted' => $deleted,
'not_deleted' => $notDeleted,
]);
}
}
33 changes: 33 additions & 0 deletions src/App/Http/Requests/DeleteAttachmentsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Asseco\Attachments\App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class DeleteAttachmentsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'attachment_ids' => 'required|array',
'attachment_ids.*' => 'exists:attachments,id',
];
}
}

0 comments on commit e435256

Please sign in to comment.