- pending 상태로 영원히 남던 DataExport 문제 수정
- 미구현 비동기 Job 대신 ExportService::store() 동기 처리
- 5분 이상 stuck된 export 자동 만료 처리
- 파일 다운로드 엔드포인트 추가 (GET /export/{id}/download)
165 lines
4.3 KiB
PHP
165 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Helpers\ApiResponse;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\V1\Subscription\ExportStoreRequest;
|
|
use App\Http\Requests\V1\Subscription\SubscriptionCancelRequest;
|
|
use App\Http\Requests\V1\Subscription\SubscriptionIndexRequest;
|
|
use App\Http\Requests\V1\Subscription\SubscriptionStoreRequest;
|
|
use App\Services\ExportService;
|
|
use App\Services\SubscriptionService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class SubscriptionController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly SubscriptionService $subscriptionService,
|
|
private readonly ExportService $exportService
|
|
) {}
|
|
|
|
/**
|
|
* 구독 목록
|
|
*/
|
|
public function index(SubscriptionIndexRequest $request): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->index($request->validated()),
|
|
__('message.fetched')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 현재 활성 구독
|
|
*/
|
|
public function current(): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->current(),
|
|
__('message.fetched')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 구독 등록
|
|
*/
|
|
public function store(SubscriptionStoreRequest $request): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->store($request->validated()),
|
|
__('message.created')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 구독 상세
|
|
*/
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->show($id),
|
|
__('message.fetched')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 구독 취소
|
|
*/
|
|
public function cancel(SubscriptionCancelRequest $request, int $id): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->cancel($id, $request->validated()['reason'] ?? null),
|
|
__('message.subscription.cancelled')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 구독 갱신
|
|
*/
|
|
public function renew(SubscriptionStoreRequest $request, int $id): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->renew($id, $request->validated()),
|
|
__('message.subscription.renewed')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 구독 일시정지
|
|
*/
|
|
public function suspend(int $id): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->suspend($id),
|
|
__('message.subscription.suspended')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 구독 재개
|
|
*/
|
|
public function resume(int $id): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->resume($id),
|
|
__('message.subscription.resumed')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 사용량 조회
|
|
*/
|
|
public function usage(): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->usage(),
|
|
__('message.fetched')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 내보내기 요청 (동기 처리)
|
|
*/
|
|
public function export(ExportStoreRequest $request): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->createExport($request->validated(), $this->exportService),
|
|
__('message.export.requested')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 내보내기 상태 조회
|
|
*/
|
|
public function exportStatus(int $id): JsonResponse
|
|
{
|
|
return ApiResponse::handle(
|
|
fn () => $this->subscriptionService->getExport($id),
|
|
__('message.fetched')
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 내보내기 파일 다운로드
|
|
*/
|
|
public function exportDownload(int $id): BinaryFileResponse
|
|
{
|
|
$export = $this->subscriptionService->getExport($id);
|
|
|
|
if (! $export->is_downloadable) {
|
|
throw new NotFoundHttpException(__('error.export.not_found'));
|
|
}
|
|
|
|
$filePath = storage_path('app/'.$export->file_path);
|
|
|
|
if (! file_exists($filePath)) {
|
|
throw new NotFoundHttpException(__('error.export.not_found'));
|
|
}
|
|
|
|
return response()->download($filePath, $export->file_name);
|
|
}
|
|
}
|