create controller
This commit is contained in:
@@ -2,62 +2,69 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\V3\Dashboard\Mission;
|
namespace App\Http\Controllers\V3\Dashboard\Mission;
|
||||||
|
|
||||||
|
use App\Facades\DataTable\DataTableFacade;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\V3\Mission\StoreRequest;
|
||||||
|
use App\Http\Traits\ApiResponse;
|
||||||
|
use App\Models\CMMSMachine;
|
||||||
|
use App\Models\Mission;
|
||||||
|
use App\Models\Rahdaran;
|
||||||
|
use App\Services\Missions\CartableService;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
class CartableController extends Controller
|
class CartableController extends Controller
|
||||||
{
|
{
|
||||||
/**
|
use ApiResponse;
|
||||||
* Display a listing of the resource.
|
public function index(Request $request,CartableService $cartableService): JsonResponse
|
||||||
*/
|
|
||||||
public function index()
|
|
||||||
{
|
{
|
||||||
//
|
return response()->json($cartableService->datatable($request));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function store(StoreRequest $request): JsonResponse
|
||||||
* Show the form for creating a new resource.
|
|
||||||
*/
|
|
||||||
public function create()
|
|
||||||
{
|
{
|
||||||
//
|
|
||||||
|
$mission = Mission::query()->create([
|
||||||
|
'user_id'=> auth()->user()->id(),
|
||||||
|
'state_id' => $request->state_id,
|
||||||
|
'state_name' => $request->state_name,
|
||||||
|
'requested_machine_type' =>$request->requested_machine_type,
|
||||||
|
'requested_machine_numbers' =>$request->requested_machine_numbers,
|
||||||
|
'type' =>$request->type,
|
||||||
|
'start_date' =>$request->start_date,
|
||||||
|
'end_date' =>$request->end_date,
|
||||||
|
'request_date' =>$request->request_date,
|
||||||
|
'description' =>$request->description,
|
||||||
|
'start_point' =>$request->start_point,
|
||||||
|
'end_point' =>$request->end_point,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($request->filled('machine_id')) {
|
||||||
|
$mission->machines()->attach($request->machine_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->filled('rahdar_id')) {
|
||||||
|
$mission->rahdars()->attach($request->rahdar_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->successResponse();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Store a newly created resource in storage.
|
public function show(Request $request,Mission $mission,CartableService $cartableService): JsonResponse
|
||||||
*/
|
|
||||||
public function store(Request $request)
|
|
||||||
{
|
{
|
||||||
//
|
return response()->json($cartableService->getMission($request->user(), $mission));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Display the specified resource.
|
|
||||||
*/
|
|
||||||
public function show(string $id)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Show the form for editing the specified resource.
|
|
||||||
*/
|
|
||||||
public function edit(string $id)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update the specified resource in storage.
|
|
||||||
*/
|
|
||||||
public function update(Request $request, string $id)
|
public function update(Request $request, string $id)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove the specified resource from storage.
|
|
||||||
*/
|
|
||||||
public function destroy(string $id)
|
public function destroy(string $id)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
|
|||||||
46
app/Http/Requests/V3/Mission/StoreRequest.php
Normal file
46
app/Http/Requests/V3/Mission/StoreRequest.php
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\V3\Mission;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class StoreRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
$user = $this->user();
|
||||||
|
return $user->hasPermissionTo('mission-manage-country') ||
|
||||||
|
($user->hasPermissionTo('mission-manage-province') && $this->province_id == $user->province_id)||
|
||||||
|
($user->hasPermissionTo('mission-manage-city') && $this->province_id == $user->province_id && $this->city_id == $user->city_id);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'machine_id' => 'nullable|exists:cmms_machines|id',
|
||||||
|
'machine_id.*' => 'exists:cmms_machines,id',
|
||||||
|
'rahdar_id' => 'required|exists:rahdaran|id',
|
||||||
|
'rahdar_id.*' => 'exists:rahdaran,id',
|
||||||
|
'state_id' => 'required|exists:mission_states|id',
|
||||||
|
'state_name' => 'required|string|max:255',
|
||||||
|
'requested_machine_type' => 'required|string|max:255',
|
||||||
|
'requested_machine_numbers'=> 'required|string|max:255',
|
||||||
|
'type' => 'required'|'string'|'max:50',
|
||||||
|
'start_date' => 'required|date',
|
||||||
|
'end_date' => 'required|date',
|
||||||
|
'request_date' => 'required|date',
|
||||||
|
'description' => 'nullable|string',
|
||||||
|
'start_point' => 'required|string|max:255',
|
||||||
|
'end_point' => 'required|string|max:255',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
28
app/Http/Requests/V3/Mission/UpdateRequest.php
Normal file
28
app/Http/Requests/V3/Mission/UpdateRequest.php
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\V3\Mission;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class UpdateRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
52
app/Services/Mission/CartableService.php
Normal file
52
app/Services/Mission/CartableService.php
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\Missions;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Facades\DataTable\DataTableFacade;
|
||||||
|
use App\Http\Traits\ApiResponse;
|
||||||
|
use App\Models\Mission;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class CartableService
|
||||||
|
{
|
||||||
|
use ApiResponse;
|
||||||
|
public function datatable(Request $request)
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
$allowedFilters = ['*'];
|
||||||
|
$allowedSortings = ['*'];
|
||||||
|
|
||||||
|
$query = Mission::query();
|
||||||
|
|
||||||
|
if ($user->hasPermissionTo('mission-manage-city')) {
|
||||||
|
$query->where('city_id', $user->edarate_shahri_id);
|
||||||
|
}
|
||||||
|
elseif ($user->hasPermissionTo('mission-manage-province')) {
|
||||||
|
$query->where('province_id', $user->province_id);
|
||||||
|
}
|
||||||
|
elseif ($user->hasPermissionTo('mission-manage-country')) {
|
||||||
|
|
||||||
|
}
|
||||||
|
return DataTableFacade::run(
|
||||||
|
$query,
|
||||||
|
$request,
|
||||||
|
allowedFilters: $allowedFilters,
|
||||||
|
allowedSortings: $allowedSortings
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
|
public function getMission(User $user, Mission $mission): Mission
|
||||||
|
{
|
||||||
|
$allowed =
|
||||||
|
$user->hasPermissionTo('mission-manage-country') ||
|
||||||
|
($user->hasPermissionTo('mission-manage-province') &&
|
||||||
|
$mission->province_id === $user->province_id) ||
|
||||||
|
($user->hasPermissionTo('mission-manage-city') &&
|
||||||
|
$mission->city_id === $user->edarate_shahri_id);
|
||||||
|
|
||||||
|
abort_unless($allowed, 403);
|
||||||
|
|
||||||
|
return $mission->load(['machines','rahdars']);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user