Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vsl30 del 435 bulk delete attachments #39

Merged
merged 6 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
];
}
}
Loading