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', ]); } }