create show,store,update and delete for RoadMaintenanceStationController
This commit is contained in:
21
app/Enums/RoadMaintenanceStationTypes.php
Normal file
21
app/Enums/RoadMaintenanceStationTypes.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
enum RoadMaintenanceStationTypes: int
|
||||||
|
{
|
||||||
|
case DAEMI= 1;
|
||||||
|
case MOVAGHAT = 2;
|
||||||
|
case FASLI = 3;
|
||||||
|
|
||||||
|
public static function name(int $state): string
|
||||||
|
{
|
||||||
|
$mapArray = [
|
||||||
|
1 => "دائمی",
|
||||||
|
2 => "موقت",
|
||||||
|
3 => "فصلی",
|
||||||
|
];
|
||||||
|
|
||||||
|
return $mapArray[$state];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,12 +2,22 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\V3\Dashboard;
|
namespace App\Http\Controllers\V3\Dashboard;
|
||||||
|
|
||||||
|
use App\Enums\RoadMaintenanceStationTypes;
|
||||||
use App\Facades\DataTable\DataTableFacade;
|
use App\Facades\DataTable\DataTableFacade;
|
||||||
|
use App\Facades\File\FileFacade;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Requests\V3\RoadMaintenanceStation\StoreRequest;
|
||||||
|
use App\Http\Requests\V3\RoadMaintenanceStation\UpdateRequest;
|
||||||
use App\Http\Traits\ApiResponse;
|
use App\Http\Traits\ApiResponse;
|
||||||
|
use App\Models\City;
|
||||||
|
use App\Models\Province;
|
||||||
use App\Models\RahdariPoint;
|
use App\Models\RahdariPoint;
|
||||||
|
use App\Models\RahdariPointHistory;
|
||||||
|
use Hekmatinasser\Verta\Facades\Verta;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
|
||||||
class RoadMaintenanceStationController extends Controller
|
class RoadMaintenanceStationController extends Controller
|
||||||
@@ -22,36 +32,21 @@ class RoadMaintenanceStationController extends Controller
|
|||||||
$allowedFilters = ['*'];
|
$allowedFilters = ['*'];
|
||||||
$allowedSortings = ['*'];
|
$allowedSortings = ['*'];
|
||||||
|
|
||||||
$province = $request->province ?? null;
|
$province = $request->province;
|
||||||
$status = $request->status ?? null;
|
$status = $request->status;
|
||||||
$type = $request->type ?? null;
|
$type = $request->type;
|
||||||
|
|
||||||
|
$query = RahdariPoint::with('files')
|
||||||
|
->when($status, fn ($query, $status) => $query->where('status', $status))
|
||||||
|
->when($type, fn ($query, $type) => $query->where('type', $type));
|
||||||
|
|
||||||
$query = RahdariPoint::with('files');
|
|
||||||
if ($user->can('show-tollhouse')) {
|
if ($user->can('show-tollhouse')) {
|
||||||
$query
|
$query->when($province, fn($query) => $query->where('province_id', $province));
|
||||||
->when($province, function ($query, $province) {
|
|
||||||
return $query->where('province_id', $province);
|
|
||||||
})
|
|
||||||
->when($status, function ($query, $status) {
|
|
||||||
return $query->where('status', $status);
|
|
||||||
})
|
|
||||||
->when($type, function ($query, $type) {
|
|
||||||
return $query->where('type', $type);
|
|
||||||
});
|
|
||||||
|
|
||||||
} elseif ($user->can('show-tollhouse-province')) {
|
|
||||||
$query
|
|
||||||
->where('province_id', $user->province_id)
|
|
||||||
->when($status, function ($query, $status) {
|
|
||||||
return $query->where('status', $status);
|
|
||||||
})
|
|
||||||
->when($type, function ($query, $type) {
|
|
||||||
return $query->where('type', $type);
|
|
||||||
});
|
|
||||||
|
|
||||||
} else {
|
|
||||||
return response()->json(['message' => 'Forbidden'], 403);
|
|
||||||
}
|
}
|
||||||
|
elseif ($user->can('show-tollhouse-province')) {
|
||||||
|
$query->where('province_id', $user->province_id);
|
||||||
|
}
|
||||||
|
|
||||||
$data = DataTableFacade::run(
|
$data = DataTableFacade::run(
|
||||||
$query,
|
$query,
|
||||||
$request,
|
$request,
|
||||||
@@ -60,79 +55,190 @@ class RoadMaintenanceStationController extends Controller
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
$processedData = [];
|
foreach ($data['data'] as &$stations) {
|
||||||
|
|
||||||
foreach ($data['data'] as $key => $item) {
|
|
||||||
$processedData[$key] = $item;
|
|
||||||
|
|
||||||
|
|
||||||
if ($user->can('edit-tollhouse')) {
|
if ($user->can('edit-tollhouse')) {
|
||||||
$processedData[$key]['canEdit'] = 1;
|
$stations['canEdit'] = 1;
|
||||||
} elseif ($user->can('edit-tollhouse-province')) {
|
} elseif ($user->can('edit-tollhouse-province')) {
|
||||||
$processedData[$key]['canEdit'] = $item['province_id'] == $user->province_id ? 1 : 0;
|
if ($user->province_id) {
|
||||||
|
$stations['canEdit'] = 1;
|
||||||
|
} else {
|
||||||
|
$stations['canEdit'] = 0;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$processedData[$key]['canEdit'] = 0;
|
$stations['canEdit'] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if ($user->can('delete-tollhouse')) {
|
if ($user->can('delete-tollhouse')) {
|
||||||
$processedData[$key]['canDelete'] = 1;
|
$stations['canDelete'] = 1;
|
||||||
} elseif ($user->can('delete-tollhouse-province')) {
|
} elseif ($user->can('delete-tollhouse-province')) {
|
||||||
$processedData[$key]['canDelete'] = $item['province_id'] == $user->province_id ? 1 : 0;
|
if ($user->province_id) {
|
||||||
|
$stations['canDelete'] = 1;
|
||||||
|
} else {
|
||||||
|
$stations['canDelete'] = 0;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$processedData[$key]['canDelete'] = 0;
|
$stations['canDelete'] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$data['data'] = $processedData;
|
|
||||||
|
|
||||||
return response()->json($data);
|
return response()->json($data);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Show the form for creating a new resource.
|
|
||||||
*/
|
|
||||||
public function create()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a newly created resource in storage.
|
* Store a newly created resource in storage.
|
||||||
*/
|
*/
|
||||||
public function store(Request $request)
|
public function store(StoreRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
//
|
$provinceName = Province::query()->find($request->province_id)->name_fa;
|
||||||
|
|
||||||
|
$cityName = City::query()->find($request->city_id)->name_fa;
|
||||||
|
|
||||||
|
$station = RahdariPoint::query()->create([
|
||||||
|
'province_id' => $request->province_id,
|
||||||
|
'province_name' => $provinceName,
|
||||||
|
'city_id' => $request->city_id,
|
||||||
|
'city_name' => $cityName,
|
||||||
|
'name' => $request->name,
|
||||||
|
'status' => $request->status,
|
||||||
|
'type' => $request->type,
|
||||||
|
'type_fa' =>RoadMaintenanceStationTypes::name($request->type),
|
||||||
|
'team_num' => $request->team_num,
|
||||||
|
'staff_num' => $request->staff_num,
|
||||||
|
'phone' => $request->phone,
|
||||||
|
'responsible_name' => $request->responsible_name,
|
||||||
|
'responsible_mobile' => $request->responsible_mobile,
|
||||||
|
'successor_name' => $request->successor_name,
|
||||||
|
'successor_mobile' => $request->successor_mobile,
|
||||||
|
'machines_light' => $request->machines_light,
|
||||||
|
'machines_sheavy' => $request->machines_sheavy,
|
||||||
|
'machines_heavy' => $request->machines_heavy,
|
||||||
|
'code' => $request->code,
|
||||||
|
'lat' => $request->lat,
|
||||||
|
'lng' => $request->lng,
|
||||||
|
'updated_at_fa' => verta()->now()->format('Y/m/d H:i:s'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$uploadPaths = [
|
||||||
|
'overview_files_1' => 'road_stations',
|
||||||
|
'overview_files_2' => 'road_stations',
|
||||||
|
'overview_files_3' => 'road_stations',
|
||||||
|
'overview_files_4' => 'road_stations',
|
||||||
|
'overview_files_5' => 'road_stations',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($uploadPaths as $field => $path) {
|
||||||
|
if ($request->hasFile($field)) {
|
||||||
|
$station->files()->create([
|
||||||
|
'path' => FileFacade::save($request->$field, $path)
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->successResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display the specified resource.
|
* Display the specified resource.
|
||||||
*/
|
*/
|
||||||
public function show(string $id)
|
public function show(RahdariPoint $rahdariPoint): JsonResponse
|
||||||
{
|
{
|
||||||
//
|
return $this->successResponse(
|
||||||
}
|
$rahdariPoint->load('files')
|
||||||
|
);
|
||||||
/**
|
|
||||||
* Show the form for editing the specified resource.
|
|
||||||
*/
|
|
||||||
public function edit(string $id)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the specified resource in storage.
|
* Update the specified resource in storage.
|
||||||
*/
|
*/
|
||||||
public function update(Request $request, string $id)
|
public function update(UpdateRequest $request, RahdariPoint $rahdariPoint): JsonResponse
|
||||||
{
|
{
|
||||||
//
|
|
||||||
|
RahdariPointHistory::query()->create([
|
||||||
|
'user_id' => $request->user()->id,
|
||||||
|
'point_id' => $rahdariPoint->id,
|
||||||
|
'previous_status' => $rahdariPoint->status,
|
||||||
|
'previous_type' => $rahdariPoint->type,
|
||||||
|
'previous_team_num' => $rahdariPoint->team_num,
|
||||||
|
'previous_staff_num' => $rahdariPoint->staff_num,
|
||||||
|
'previous_phone' => $rahdariPoint->phone,
|
||||||
|
'previous_responsible_name' => $rahdariPoint->responsible_name,
|
||||||
|
'previous_responsible_mobile' => $rahdariPoint->responsible_mobile,
|
||||||
|
'previous_successor_name' => $rahdariPoint->successor_name,
|
||||||
|
'previous_successor_mobile' => $rahdariPoint->successor_mobile,
|
||||||
|
'previous_machines_light' => $rahdariPoint->machines_light,
|
||||||
|
'previous_machines_sheavy' => $rahdariPoint->machines_sheavy,
|
||||||
|
'previous_machines_heavy' => $rahdariPoint->machines_heavy,
|
||||||
|
'previous_lat' => $rahdariPoint->lat,
|
||||||
|
'previous_lng' => $rahdariPoint->lng,
|
||||||
|
'new_files' => json_encode($rahdariPoint->files),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$rahdariPoint->update([
|
||||||
|
'status' => $request->input('status', $rahdariPoint->status),
|
||||||
|
'type' => $request->input('type', $rahdariPoint->type),
|
||||||
|
'type_fa' => RoadMaintenanceStationTypes::name($request->type) ?? $rahdariPoint->type_fa,
|
||||||
|
'team_num' => $request->input('team_num', $rahdariPoint->team_num),
|
||||||
|
'staff_num' => $request->input('staff_num', $rahdariPoint->staff_num),
|
||||||
|
'phone' => $request->input('phone', $rahdariPoint->phone),
|
||||||
|
'responsible_name' => $request->input('responsible_name', $rahdariPoint->responsible_name),
|
||||||
|
'responsible_mobile' => $request->input('responsible_mobile', $rahdariPoint->responsible_mobile),
|
||||||
|
'successor_name' => $request->input('successor_name', $rahdariPoint->successor_name),
|
||||||
|
'successor_mobile' => $request->input('successor_mobile', $rahdariPoint->successor_mobile),
|
||||||
|
'machines_light' => $request->input('machines_light', $rahdariPoint->machines_light),
|
||||||
|
'machines_sheavy' => $request->input('machines_sheavy', $rahdariPoint->machines_sheavy),
|
||||||
|
'machines_heavy' => $request->input('machines_heavy', $rahdariPoint->machines_heavy),
|
||||||
|
'lat' => $request->input('lat', $rahdariPoint->lat),
|
||||||
|
'lng' => $request->input('lng', $rahdariPoint->lng),
|
||||||
|
'lat_from' => $request->lat_from,
|
||||||
|
'lng_from' => $request->lng_from,
|
||||||
|
'geometry' => $request->geometry,
|
||||||
|
'lat_to' => $request->lat_to,
|
||||||
|
'lng_to' => $request->lng_to,
|
||||||
|
'color' => $request->color,
|
||||||
|
'updated_at_fa' => verta()->now()->format('Y/m/d H:i:s'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$uploadFields = [
|
||||||
|
'overview_files_1',
|
||||||
|
'overview_files_2',
|
||||||
|
'overview_files_3',
|
||||||
|
'overview_files_4',
|
||||||
|
'overview_files_5',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($uploadFields as $index => $field) {
|
||||||
|
if ($request->hasFile($field)) {
|
||||||
|
$existingFile = $rahdariPoint->files->get($index);
|
||||||
|
|
||||||
|
$newPath = FileFacade::save($request->$field, 'rahdari_points/overview');
|
||||||
|
|
||||||
|
if ($existingFile) {
|
||||||
|
FileFacade::delete($existingFile->path);
|
||||||
|
$existingFile->update(['path' => $newPath]);
|
||||||
|
} else {
|
||||||
|
// dd($rahdariPoint->id);
|
||||||
|
$rahdariPoint->files()->create(['path' => $newPath]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->successResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the specified resource from storage.
|
* Remove the specified resource from storage.
|
||||||
*/
|
*/
|
||||||
public function destroy(string $id)
|
public function destroy(RahdariPoint $rahdariPoint): JsonResponse
|
||||||
{
|
{
|
||||||
//
|
foreach ($rahdariPoint->files as $file) {
|
||||||
|
FileFacade::delete($file->path);
|
||||||
|
}
|
||||||
|
|
||||||
|
RahdariPointHistory::query()->where('point_id', $rahdariPoint->id)->delete();
|
||||||
|
|
||||||
|
$rahdariPoint->files()->delete();
|
||||||
|
$rahdariPoint->delete();
|
||||||
|
|
||||||
|
return $this->successResponse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
57
app/Http/Requests/V3/RoadMaintenanceStation/StoreRequest.php
Normal file
57
app/Http/Requests/V3/RoadMaintenanceStation/StoreRequest.php
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\V3\RoadMaintenanceStation;
|
||||||
|
|
||||||
|
use App\Models\RahdariPoint;
|
||||||
|
use Hekmatinasser\Verta\Facades\Verta;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
|
class StoreRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
|
return $user->hasPermissionTo('add-tollhouse') ||
|
||||||
|
($user->hasPermissionTo('add-tollhouse-province') && $this->province_id == $user->province_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 [
|
||||||
|
'province_id' => 'required|exists:provinces,id',
|
||||||
|
'city_id' => 'required|exists:cities,id',
|
||||||
|
'name' => 'required|string|max:255',
|
||||||
|
'status' => 'required|in:0,1',
|
||||||
|
'type' =>'required|in:1,2,3',
|
||||||
|
'team_num' => 'numeric',
|
||||||
|
'staff_num' => 'numeric',
|
||||||
|
'phone' => 'string|max:50',
|
||||||
|
'responsible_name' => 'string|max:255',
|
||||||
|
'responsible_mobile' => 'string|max:50',
|
||||||
|
'successor_name' => 'string|max:255',
|
||||||
|
'successor_mobile' => 'string|max:50',
|
||||||
|
'machines_light' => 'numeric',
|
||||||
|
'machines_sheavy' => 'numeric',
|
||||||
|
'machines_heavy' => 'numeric',
|
||||||
|
'code' => 'string|max:50',
|
||||||
|
'lat' => 'numeric',
|
||||||
|
'lng' => 'numeric',
|
||||||
|
'overview_files_1' => 'file|mimes:jpg,jpeg,png,pdf|max:51200',
|
||||||
|
'overview_files_2' => 'file|mimes:jpg,jpeg,png,pdf|max:51200',
|
||||||
|
'overview_files_3' => 'file|mimes:jpg,jpeg,png,pdf|max:51200',
|
||||||
|
'overview_files_4' => 'file|mimes:jpg,jpeg,png,pdf|max:51200',
|
||||||
|
'overview_files_5' => 'file|mimes:jpg,jpeg,png,pdf|max:51200',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests\V3\RoadMaintenanceStation;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class UpdateRequest extends FormRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Determine if the user is authorized to make this request.
|
||||||
|
*/
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'status' => 'integer|in:0,1',
|
||||||
|
'type' => 'integer|in:1,2,3',
|
||||||
|
'team_num' => 'numeric',
|
||||||
|
'staff_num' => 'numeric',
|
||||||
|
'phone' => 'string|max:50',
|
||||||
|
'responsible_name' => 'string|max:255',
|
||||||
|
'responsible_mobile' => 'string|max:50',
|
||||||
|
'successor_name' => 'string|max:255',
|
||||||
|
'successor_mobile' => 'string|max:50',
|
||||||
|
'machines_light' => 'numeric',
|
||||||
|
'machines_sheavy' => 'numeric',
|
||||||
|
'machines_heavy' => 'numeric',
|
||||||
|
'lat' => 'numeric',
|
||||||
|
'lng' => 'numeric',
|
||||||
|
'lat_from' => 'numeric',
|
||||||
|
'lng_from' => 'numeric',
|
||||||
|
'lat_to' => 'numeric',
|
||||||
|
'lng_to' => 'numeric',
|
||||||
|
'color' => 'numeric',
|
||||||
|
'geometry' => 'numeric',
|
||||||
|
'overview_files_1' => 'file|mimes:jpg,jpeg,png,pdf|max:5120',
|
||||||
|
'overview_files_2' => 'file|mimes:jpg,jpeg,png,pdf|max:5120',
|
||||||
|
'overview_files_3' => 'file|mimes:jpg,jpeg,png,pdf|max:5120',
|
||||||
|
'overview_files_4' => 'file|mimes:jpg,jpeg,png,pdf|max:5120',
|
||||||
|
'overview_files_5' => 'file|mimes:jpg,jpeg,png,pdf|max:5120',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -391,6 +391,16 @@ Route::prefix('Road-Maintenance-Station')
|
|||||||
->name('Road-Maintenance-Station.')
|
->name('Road-Maintenance-Station.')
|
||||||
->controller( RoadMaintenanceStationController::class)
|
->controller( RoadMaintenanceStationController::class)
|
||||||
->group(function () {
|
->group(function () {
|
||||||
Route::get('/', 'index')->name('index');
|
Route::get('/', 'index')->name('index')
|
||||||
|
->middleware('permission:show-tollhouse|show-tollhouse-province');
|
||||||
|
Route::post('/', 'store')->name('store')
|
||||||
|
->middleware('permission:add-tollhouse|add-tollhouse-province');
|
||||||
|
Route::get('/{rahdariPoint}', 'show')->name('show')
|
||||||
|
->middleware('permission:show-tollhouse|show-tollhouse-province');
|
||||||
|
Route::post('/{rahdariPoint}', 'update')->name('update')
|
||||||
|
->middleware('permission:edit-tollhouse|edit-tollhouse-province');
|
||||||
|
Route::delete('/{rahdariPoint}', 'destroy')->name('destroy')
|
||||||
|
->middleware('permission:delete-tollhouse|delete-tollhouse-province');
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user