114 lines
3.4 KiB
PHP
114 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\Investment\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Modules\Investment\Entities\InvestmentLimitProfile;
|
|
use Modules\Investment\Entities\ProfileInvestmentLimit;
|
|
use Modules\Investment\Http\Requests\ProfileInvestmentLimitRequest;
|
|
use Modules\Investment\Transformers\ProfileInvestmentLimitResource;
|
|
|
|
class ProfileInvestmentLimitController extends Controller
|
|
{
|
|
use AuthorizesRequests;
|
|
|
|
public function index(string $investmentLimitProfile): JsonResponse
|
|
{
|
|
$this->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);
|
|
}
|
|
}
|