feat: [vehicle] 법인차량 사진 API 추가
- CorporateVehicle 모델 (photos 관계 포함) - VehiclePhotoService (R2 저장, 최대 10장 제한) - VehiclePhotoController (index/store/destroy) - StoreVehiclePhotoRequest (동적 max 검증) - finance.php 라우트 등록
This commit is contained in:
124
app/Services/Finance/VehiclePhotoService.php
Normal file
124
app/Services/Finance/VehiclePhotoService.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Finance;
|
||||
|
||||
use App\Models\Commons\File;
|
||||
use App\Models\Finance\CorporateVehicle;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class VehiclePhotoService extends Service
|
||||
{
|
||||
private const MAX_PHOTOS = 10;
|
||||
|
||||
public function index(int $vehicleId): array
|
||||
{
|
||||
$vehicle = $this->getVehicle($vehicleId);
|
||||
|
||||
return $vehicle->photos->map(fn ($file) => $this->formatFileResponse($file))->values()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UploadedFile[] $files
|
||||
*/
|
||||
public function store(int $vehicleId, array $files): array
|
||||
{
|
||||
$vehicle = $this->getVehicle($vehicleId);
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
$currentCount = File::where('document_id', $vehicleId)
|
||||
->where('document_type', 'corporate_vehicle')
|
||||
->whereNull('deleted_at')
|
||||
->count();
|
||||
|
||||
if ($currentCount + count($files) > self::MAX_PHOTOS) {
|
||||
throw new \Exception(__('error.vehicle.photo_limit_exceeded'));
|
||||
}
|
||||
|
||||
$uploaded = [];
|
||||
|
||||
foreach ($files as $uploadedFile) {
|
||||
$extension = $uploadedFile->getClientOriginalExtension();
|
||||
$storedName = bin2hex(random_bytes(8)).'.'.$extension;
|
||||
$displayName = $uploadedFile->getClientOriginalName();
|
||||
|
||||
$year = date('Y');
|
||||
$month = date('m');
|
||||
$directory = sprintf('%d/corporate-vehicles/%s/%s', $tenantId, $year, $month);
|
||||
$filePath = $directory.'/'.$storedName;
|
||||
|
||||
Storage::disk('r2')->putFileAs($directory, $uploadedFile, $storedName);
|
||||
|
||||
$mimeType = $uploadedFile->getMimeType();
|
||||
|
||||
$file = File::create([
|
||||
'tenant_id' => $tenantId,
|
||||
'display_name' => $displayName,
|
||||
'stored_name' => $storedName,
|
||||
'file_path' => $filePath,
|
||||
'file_size' => $uploadedFile->getSize(),
|
||||
'mime_type' => $mimeType,
|
||||
'file_type' => 'image',
|
||||
'document_id' => $vehicleId,
|
||||
'document_type' => 'corporate_vehicle',
|
||||
'is_temp' => false,
|
||||
'uploaded_by' => $userId,
|
||||
'created_by' => $userId,
|
||||
]);
|
||||
|
||||
$uploaded[] = $this->formatFileResponse($file);
|
||||
}
|
||||
|
||||
return $uploaded;
|
||||
}
|
||||
|
||||
public function destroy(int $vehicleId, int $fileId): array
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
$file = File::where('tenant_id', $tenantId)
|
||||
->where('document_id', $vehicleId)
|
||||
->where('document_type', 'corporate_vehicle')
|
||||
->where('id', $fileId)
|
||||
->first();
|
||||
|
||||
if (! $file) {
|
||||
throw new NotFoundHttpException(__('error.file.not_found'));
|
||||
}
|
||||
|
||||
$file->softDeleteFile($userId);
|
||||
|
||||
return [
|
||||
'file_id' => $fileId,
|
||||
'deleted' => true,
|
||||
];
|
||||
}
|
||||
|
||||
private function getVehicle(int $vehicleId): CorporateVehicle
|
||||
{
|
||||
$vehicle = CorporateVehicle::find($vehicleId);
|
||||
|
||||
if (! $vehicle) {
|
||||
throw new NotFoundHttpException(__('error.vehicle.not_found'));
|
||||
}
|
||||
|
||||
return $vehicle;
|
||||
}
|
||||
|
||||
private function formatFileResponse(File $file): array
|
||||
{
|
||||
return [
|
||||
'id' => $file->id,
|
||||
'file_name' => $file->display_name,
|
||||
'file_path' => $file->file_path,
|
||||
'file_url' => url("/api/v1/files/{$file->id}/download"),
|
||||
'file_size' => $file->file_size,
|
||||
'mime_type' => $file->mime_type,
|
||||
'created_at' => $file->created_at?->format('Y-m-d H:i:s'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user