Files
sam-api/app/Http/Controllers/Api/V1/ItemsFileController.php
hskwon f09fa3791c feat: [items] 아이템 API 기능 개선
- ItemsController, ItemsBomController, ItemsFileController 수정
- ItemBatchDeleteRequest 추가
- ItemsService 개선
- ItemsApi Swagger 문서 업데이트
2025-11-30 21:05:44 +09:00

174 lines
5.6 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Controllers\Controller;
use App\Http\Requests\ItemsFileUploadRequest;
use App\Http\Responses\ApiResponse;
use App\Models\Products\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* 품목 파일 관리 컨트롤러
*
* ID-based 파일 업로드/삭제 API
* - 절곡도 (bending_diagram)
* - 시방서 (specification)
* - 인정서 (certification)
*/
class ItemsFileController extends Controller
{
/**
* 파일 업로드
*
* POST /api/v1/items/{id}/files
*/
public function upload(int $id, ItemsFileUploadRequest $request)
{
return ApiResponse::handle(function () use ($id, $request) {
$product = $this->getProductById($id);
$validated = $request->validated();
$fileType = $request->route('type') ?? $validated['type'];
$file = $validated['file'];
// 파일 저장 경로: items/{id}/{type}/{filename}
$directory = sprintf('items/%d/%s', $id, $fileType);
$filePath = Storage::disk('public')->putFile($directory, $file);
$fileUrl = Storage::disk('public')->url($filePath);
$originalName = $file->getClientOriginalName();
// Product 모델 업데이트
$updateData = $this->buildUpdateData($fileType, $filePath, $originalName, $validated);
$product->update($updateData);
return [
'file_type' => $fileType,
'file_url' => $fileUrl,
'file_path' => $filePath,
'file_name' => $originalName,
'product' => $product->fresh(),
];
}, __('message.file.uploaded'));
}
/**
* 파일 삭제
*
* DELETE /api/v1/items/{id}/files/{type}
*/
public function delete(int $id, string $type, Request $request)
{
return ApiResponse::handle(function () use ($id, $type) {
$product = $this->getProductById($id);
// 파일 타입 검증
if (! in_array($type, ['bending_diagram', 'specification', 'certification'])) {
throw new \InvalidArgumentException(__('error.file.invalid_file_type'));
}
// 파일 경로 가져오기
$filePath = $this->getFilePath($product, $type);
if ($filePath) {
// 물리적 파일 삭제
Storage::disk('public')->delete($filePath);
}
// DB 필드 null 처리
$updateData = $this->buildDeleteData($type);
$product->update($updateData);
return [
'file_type' => $type,
'deleted' => (bool) $filePath,
'product' => $product->fresh(),
];
}, __('message.file.deleted'));
}
/**
* ID로 Product 조회
*/
private function getProductById(int $id): Product
{
$tenantId = app('tenant_id');
$product = Product::query()
->where('tenant_id', $tenantId)
->find($id);
if (! $product) {
throw new NotFoundHttpException(__('error.not_found'));
}
return $product;
}
/**
* 파일 타입별 업데이트 데이터 구성
*/
private function buildUpdateData(string $fileType, string $filePath, string $originalName, array $validated): array
{
$updateData = match ($fileType) {
'bending_diagram' => [
'bending_diagram' => $filePath,
'bending_details' => $validated['bending_details'] ?? null,
],
'specification' => [
'specification_file' => $filePath,
'specification_file_name' => $originalName,
],
'certification' => [
'certification_file' => $filePath,
'certification_file_name' => $originalName,
'certification_number' => $validated['certification_number'] ?? null,
'certification_start_date' => $validated['certification_start_date'] ?? null,
'certification_end_date' => $validated['certification_end_date'] ?? null,
],
default => throw new \InvalidArgumentException(__('error.file.invalid_file_type')),
};
return $updateData;
}
/**
* 파일 타입별 삭제 데이터 구성
*/
private function buildDeleteData(string $fileType): array
{
return match ($fileType) {
'bending_diagram' => [
'bending_diagram' => null,
'bending_details' => null,
],
'specification' => [
'specification_file' => null,
'specification_file_name' => null,
],
'certification' => [
'certification_file' => null,
'certification_file_name' => null,
'certification_number' => null,
'certification_start_date' => null,
'certification_end_date' => null,
],
default => throw new \InvalidArgumentException(__('error.file.invalid_file_type')),
};
}
/**
* Product에서 파일 경로 가져오기
*/
private function getFilePath(Product $product, string $fileType): ?string
{
return match ($fileType) {
'bending_diagram' => $product->bending_diagram,
'specification' => $product->specification_file,
'certification' => $product->certification_file,
default => null,
};
}
}