117 lines
3.5 KiB
PHP
117 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\DomainFluctuation\App\Http\Controllers\Api\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\DomainFluctuation\App\Http\Requests\StoreTermRequest;
|
|
use Modules\DomainFluctuation\App\Http\Resources\DomainFluctuationTermResource;
|
|
use Modules\DomainFluctuation\App\Models\DomainFluctuationTerm;
|
|
use Modules\DomainFluctuation\App\Models\TermFluctuationType;
|
|
|
|
class TermController extends Controller
|
|
{
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$perPage = (int) $request->input('per_page', 20);
|
|
|
|
$terms = DomainFluctuationTerm::query()
|
|
->with('type')
|
|
->withCount('fluctuations')
|
|
->orderByDesc('id')
|
|
->paginate($perPage);
|
|
|
|
return DomainFluctuationTermResource::collection($terms)->response();
|
|
}
|
|
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$term = DomainFluctuationTerm::with('type')->withCount('fluctuations')->find($id);
|
|
|
|
if (! $term) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Không tìm thấy đợt khai báo',
|
|
], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => DomainFluctuationTermResource::make($term),
|
|
]);
|
|
}
|
|
|
|
public function store(StoreTermRequest $request): JsonResponse
|
|
{
|
|
$userId = (int) ($request->user()?->id ?? 0);
|
|
$dto = $request->toDTO($userId);
|
|
|
|
$term = DB::transaction(function () use ($dto, $request) {
|
|
$term = DomainFluctuationTerm::create($dto->toArray());
|
|
|
|
TermFluctuationType::create([
|
|
'term_fluctuation_id' => $term->id,
|
|
'type' => $request->input('type', 'vnnic_06'),
|
|
]);
|
|
|
|
return $term->load('type');
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Tạo đợt khai báo thành công',
|
|
'data' => DomainFluctuationTermResource::make($term),
|
|
], 201);
|
|
}
|
|
|
|
public function update(int $id, StoreTermRequest $request): JsonResponse
|
|
{
|
|
$term = DomainFluctuationTerm::find($id);
|
|
|
|
if (! $term) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Không tìm thấy đợt khai báo',
|
|
], 404);
|
|
}
|
|
|
|
DB::transaction(function () use ($term, $request) {
|
|
$term->update($request->only(['term_name', 'description', 'date_create', 'status']));
|
|
|
|
if ($request->filled('type')) {
|
|
TermFluctuationType::updateOrCreate(
|
|
['term_fluctuation_id' => $term->id],
|
|
['type' => $request->input('type')],
|
|
);
|
|
}
|
|
});
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Cập nhật đợt khai báo thành công',
|
|
'data' => DomainFluctuationTermResource::make($term->fresh('type')),
|
|
]);
|
|
}
|
|
|
|
public function destroy(int $id): JsonResponse
|
|
{
|
|
$term = DomainFluctuationTerm::find($id);
|
|
|
|
if (! $term) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Không tìm thấy đợt khai báo',
|
|
], 404);
|
|
}
|
|
|
|
$term->delete();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Xoá đợt khai báo thành công',
|
|
]);
|
|
}
|
|
}
|