enhanced services attachment
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\AttachmentForm;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AttachmentService
|
||||
{
|
||||
/**
|
||||
* Store single attachment
|
||||
*/
|
||||
public function addAttachment(int $formId, string $tableName, string $filePath, ?string $category = null): AttachmentForm
|
||||
{
|
||||
return AttachmentForm::create([
|
||||
'form_id' => $formId,
|
||||
'table_name' => $tableName,
|
||||
'file_category'=> $category,
|
||||
'file_path' => $filePath,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store multiple attachments
|
||||
*/
|
||||
public function addMultipleAttachments(int $formId, string $tableName, array $attachments): void
|
||||
{
|
||||
$data = [];
|
||||
|
||||
foreach ($attachments as $file) {
|
||||
$data[] = [
|
||||
'form_id' => $formId,
|
||||
'table_name' => $tableName,
|
||||
'file_category' => $file['file_category'] ?? null,
|
||||
'file_path' => $file['file_path'],
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
AttachmentForm::insert($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attachments by form
|
||||
*/
|
||||
public function getAttachments(int $formId, string $tableName)
|
||||
{
|
||||
return AttachmentForm::where('form_id', $formId)
|
||||
->where('table_name', $tableName)
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete attachment by ID (soft delete)
|
||||
*/
|
||||
public function deleteAttachment(int $attachmentId): bool
|
||||
{
|
||||
return AttachmentForm::where('id', $attachmentId)->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all attachment by form
|
||||
*/
|
||||
public function deleteByForm(int $formId, string $tableName): bool
|
||||
{
|
||||
return AttachmentForm::where('form_id', $formId)
|
||||
->where('table_name', $tableName)
|
||||
->delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user