authorize('viewAny', InvestmentLimitProfile::class); $profile = InvestmentLimitProfile::query()->find($investmentLimitProfile); if (! $profile) { return $this->notFound(); } $limits = $profile->profileLimits() ->with('investmentType:id,code,name') ->orderBy('created_at') ->get(); return response()->json([ 'success' => true, 'data' => ProfileInvestmentLimitResource::collection($limits), 'message' => 'Had profil berjaya dimuatkan', ]); } public function store(ProfileInvestmentLimitRequest $request, string $investmentLimitProfile): JsonResponse { $this->authorize('create', InvestmentLimitProfile::class); $profile = InvestmentLimitProfile::query()->find($investmentLimitProfile); if (! $profile) { return $this->notFound(); } $limit = $profile->profileLimits()->create($request->validated()); $limit->load('investmentType:id,code,name'); return response()->json([ 'success' => true, 'data' => new ProfileInvestmentLimitResource($limit), 'message' => 'Had profil berjaya ditambah', ], 201); } public function update( ProfileInvestmentLimitRequest $request, string $investmentLimitProfile, string $limit, ): JsonResponse { $this->authorize('update', InvestmentLimitProfile::class); $item = ProfileInvestmentLimit::query() ->where('profile_id', $investmentLimitProfile) ->whereKey($limit) ->first(); if (! $item) { return $this->notFound('Had profil tidak dijumpai.'); } $item->update($request->validated()); $item->load('investmentType:id,code,name'); return response()->json([ 'success' => true, 'data' => new ProfileInvestmentLimitResource($item), 'message' => 'Had profil berjaya dikemaskini', ]); } public function destroy(string $investmentLimitProfile, string $limit): JsonResponse { $this->authorize('delete', InvestmentLimitProfile::class); $item = ProfileInvestmentLimit::query() ->where('profile_id', $investmentLimitProfile) ->whereKey($limit) ->first(); if (! $item) { return $this->notFound('Had profil tidak dijumpai.'); } $item->delete(); return response()->json([ 'success' => true, 'message' => 'Had profil berjaya dipadam', ]); } protected function notFound(string $message = 'Profil had tidak dijumpai.'): JsonResponse { return response()->json([ 'success' => false, 'message' => $message, ], 404); } }