change the migration columns and add form request

This commit is contained in:
2024-11-03 06:26:26 +00:00
parent e9098b0a55
commit 1a15a5d7ed
16 changed files with 444 additions and 66 deletions

View File

@@ -10,6 +10,8 @@ use App\Models\AzmayeshType;
use App\Models\Province;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Requests\V3\Azmayesh\StoreRequest;
use App\Http\Requests\V3\Azmayesh\UpdateRequest;
class AzmayeshController extends Controller
{
@@ -48,27 +50,27 @@ class AzmayeshController extends Controller
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): JsonResponse
public function store(StoreRequest $request): JsonResponse
{
$azmayeshType = AzmayeshType::query()->find($request->azmayesh_type_id);
$province = Province::query()->find($request->province_id);
$azmayeshType = AzmayeshType::query()->findOrFail($request->azmayesh_type_id);
$province = Province::query()->findOrFail($request->province_id);
Azmayesh::query()->create([
'lat' => $request->lat,
'lng' => $request->lng,
'azmayesh_type_id' => $azmayeshType->id,
'azmayesh_type_name' => $azmayeshType->name,
'request_date' => $request->lat,
'report_date' => $request->lat,
'request_number' => $request->lat,
'employer' => $request->lat,
'contractor' => $request->lat,
'work_number' => $request->lat,
'project_name' => $request->lat,
'consultant' => $request->lat,
'applicant' => $request->lat,
'request_date' => $request->request_date,
'report_date' => $request->report_date,
'request_number' => $request->request_number ?? '-',
'employer' => $request->employer ?? '-',
'contractor' => $request->contractor ?? '-',
'work_number' => $request->work_number ?? '-',
'project_name' => $request->project_name,
'consultant' => $request->consultant ?? '-',
'applicant' => $request->applicant ?? '-',
'province_id' => $province->id,
'province_name' => $province->name,
'province_name' => $province->name_fa,
]);
return $this->successResponse();
@@ -85,19 +87,27 @@ class AzmayeshController extends Controller
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Azmayesh $azmayesh): JsonResponse
public function update(UpdateRequest $request, Azmayesh $azmayesh): JsonResponse
{
$province = Province::query()->find($request->province_id);
$province = Province::query()->findOrFail($request->province_id);
$azmayeshType = AzmayeshType::query()->findOrFail($request->azmayesh_type_id);
$azmayesh->update([
'name' => $request->name,
'username' => $request->username,
'email' => $request->email,
'phone_number' => $request->phone_number,
'national_id' => $request->national_id,
'position' => $request->position,
'lat' => $request->lat,
'lng' => $request->lng,
'azmayesh_type_id' => $azmayeshType->id,
'azmayesh_type_name' => $azmayeshType->name,
'request_date' => $request->request_date,
'report_date' => $request->report_date,
'request_number' => $request->request_number ?? '-',
'employer' => $request->employer ?? '-',
'contractor' => $request->contractor ?? '-',
'work_number' => $request->work_number ?? '-',
'project_name' => $request->project_name,
'consultant' => $request->consultant ?? '-',
'applicant' => $request->applicant ?? '-',
'province_id' => $province->id,
'province_name' => $province->name,
'province_name' => $province->name_fa,
]);
return $this->successResponse();

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Http\Controllers\V3\Azmayesh;
use App\Http\Controllers\Controller;
use App\Facades\DataTable\DataTableFacade;
use App\Http\Traits\ApiResponse;
use App\Models\Azmayesh;
use App\Models\AzmayeshType;
use App\Models\AzmayeshSample;
use App\Models\Province;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Requests\V3\AzmayeshSample\StoreRequest;
use App\Http\Requests\V3\AzmayeshSample\UpdateRequest;
class AzmayeshSampleController extends Controller
{
use ApiResponse;
/**
* Display a listing of the resource.
*/
public function index(Request $request, string $azmayesh_id): JsonResponse
{
$columns = array(
'id',
);
$allowedFilters = $columns;
$allowedSortings = $columns;
$query = AzmayeshSample::query()->where('azmayesh_id', '=', $azmayesh_id);
$data = DataTableFacade::run(
$query,
$request,
allowedFilters: $allowedFilters,
allowedSortings: $allowedSortings);
return response()->json($data);
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreRequest $request): JsonResponse
{
$azmayesh = Azmayesh::query()->findOrFail($request->azmayesh_id);
AzmayeshSample::query()->create([
'azmayesh_id' => $azmayesh->id,
'azmayesh_project_name' => $azmayesh->project_name,
'data' => $request->data,
]);
return $this->successResponse();
}
/**
* Display the specified resource.
*/
public function show(AzmayeshSample $azmayeshSample)
{
return $this->successResponse($azmayeshSample);
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateRequest $request, AzmayeshSample $azmayeshSample)
{
$azmayeshSample->update([
'azmayesh_id' => $azmayesh->id,
'azmayesh_project_name' => $azmayesh->project_name,
'data' => json_encode($request->data),
]);
return $this->successResponse();
}
/**
* Remove the specified resource from storage.
*/
public function destroy(AzmayeshSample $azmayeshSample)
{
$azmayeshSample->delete();
return $this->successResponse();
}
}

View File

@@ -21,7 +21,7 @@ class AzmayeshTypeController extends Controller
public function fields(AzmayeshType $azmayeshType): JsonResponse
{
$fields = $azmayeshType->azmayeshFields;
$fields = $azmayeshType->azmayeshFields->select('id', 'name', 'unit', 'type', 'option');
return $this->successResponse($fields);
}

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Http\Requests\V3\Azmayesh;
use Illuminate\Foundation\Http\FormRequest;
class StoreRequest 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 [
'lat' => 'required|string',
'lng' => 'required|string',
'azmayesh_type_id' => 'required|exists:azmayesh_types,id',
'request_date' => 'date',
'report_date' => 'date',
'request_number' => 'string',
'employer' => 'string',
'contractor' => 'string',
'work_number' => 'string',
'project_name' => 'required|string',
'consultant' => 'string',
'applicant' => 'string',
'province_id' => 'required|exists:provinces,id',
];
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Http\Requests\V3\Azmayesh;
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 [
'lat' => 'required|string',
'lng' => 'required|string',
'azmayesh_type_id' => 'required|exists:azmayesh_types,id',
'request_date' => 'date',
'report_date' => 'date',
'request_number' => 'string',
'employer' => 'string',
'contractor' => 'string',
'work_number' => 'string',
'project_name' => 'required|string',
'consultant' => 'string',
'applicant' => 'string',
'province_id' => 'required|exists:provinces,id',
];
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Http\Requests\V3\AzmayeshSample;
use Illuminate\Foundation\Http\FormRequest;
class StoreRequest 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 [
'azmayesh_id' => 'required|exists:azmayeshes,id',
'data' => 'required',
];
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Http\Requests\V3\AzmayeshSample;
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 [
'azmayesh_id' => 'required|exists:azmayeshes,id',
'data' => 'required',
];
}
}

View File

@@ -5,6 +5,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Casts\Attribute;
class AzmayeshField extends Model
{
@@ -12,8 +13,15 @@ class AzmayeshField extends Model
protected $guarded = [];
protected function option(): Attribute
{
return Attribute::make(
get: fn ($value) => json_decode($value),
);
}
public function azmayeshTypes(): BelongsToMany
{
return $this->belongsToMany(AzmayeshType::class);
return $this->belongsToMany(AzmayeshType::class, 'azmayesh_field_azmayesh_type');
}
}