From a7064062611930e1ae2ec3f744202ec77b94d6fb Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Mon, 2 Dec 2024 10:37:48 +0330 Subject: [PATCH 1/3] add user id column to azmayesh table --- .../V3/Azmayesh/AzmayeshController.php | 16 ++++++++-- database/factories/AzmayeshFactory.php | 5 +++ ..._090036_add_column_to_azmayeshes_table.php | 31 +++++++++++++++++++ database/seeders/AzmayeshSeeder.php | 12 +++++++ tests/Feature/V3/Azmayesh/StoreTest.php | 2 ++ 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 database/migrations/2024_12_02_090036_add_column_to_azmayeshes_table.php diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php index 94bbd81a..938a5d8f 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshController.php @@ -8,6 +8,7 @@ use App\Http\Traits\ApiResponse; use App\Models\Azmayesh; use App\Models\AzmayeshType; use App\Models\Province; +use Illuminate\Database\QueryException; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use App\Http\Requests\V3\Azmayesh\StoreRequest; @@ -34,7 +35,8 @@ class AzmayeshController extends Controller 'azmayesh_type_name', 'project_name', 'updated_at', - 'created_at' + 'created_at', + 'user_name' ); $allowedFilters = $columns; @@ -58,6 +60,7 @@ class AzmayeshController extends Controller { $azmayeshType = AzmayeshType::query()->findOrFail($request->azmayesh_type_id); $province = Province::query()->findOrFail($request->province_id); + $user = auth()->user(); Azmayesh::query()->create([ 'lat' => $request->lat, @@ -75,6 +78,8 @@ class AzmayeshController extends Controller 'applicant' => $request->applicant ?? '-', 'province_id' => $province->id, 'province_name' => $province->name_fa, + 'user_id' => $user->id, + 'user_name' => $user->name, ]); return $this->successResponse(); @@ -122,8 +127,13 @@ class AzmayeshController extends Controller */ public function destroy(Azmayesh $azmayesh): JsonResponse { - $azmayesh->delete(); + try { + $azmayesh->delete(); - return $this->successResponse(); + return $this->successResponse(); + } + catch (QueryException $exception) { + return $this->errorResponse('can not be deleted'); + } } } diff --git a/database/factories/AzmayeshFactory.php b/database/factories/AzmayeshFactory.php index e8aa9d2e..f725541b 100644 --- a/database/factories/AzmayeshFactory.php +++ b/database/factories/AzmayeshFactory.php @@ -4,6 +4,7 @@ namespace Database\Factories; use App\Models\AzmayeshType; use App\Models\Province; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -38,6 +39,10 @@ class AzmayeshFactory extends Factory 'project_name' => $this->faker->name, 'consultant' => $this->faker->name, 'applicant' => $this->faker->name, + 'user_id' => User::factory(), + 'user_name' => function (array $attributes) { + return User::query()->find($attributes['user_id'])->name; + }, ]; } } diff --git a/database/migrations/2024_12_02_090036_add_column_to_azmayeshes_table.php b/database/migrations/2024_12_02_090036_add_column_to_azmayeshes_table.php new file mode 100644 index 00000000..10e6fa41 --- /dev/null +++ b/database/migrations/2024_12_02_090036_add_column_to_azmayeshes_table.php @@ -0,0 +1,31 @@ +unsignedInteger('user_id')->nullable(); + $table->foreign('user_id')->references('id')->on('users'); + $table->string('user_name'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('azmayeshes', function (Blueprint $table) { + $table->dropColumn('user_id'); + $table->dropColumn('user_name'); + }); + } +}; diff --git a/database/seeders/AzmayeshSeeder.php b/database/seeders/AzmayeshSeeder.php index b404afdb..2916154a 100644 --- a/database/seeders/AzmayeshSeeder.php +++ b/database/seeders/AzmayeshSeeder.php @@ -31,6 +31,8 @@ class AzmayeshSeeder extends Seeder 'project_name' => 'اجرای پروژه پل میرزای شیرازی شریفیه', 'consultant' => '-', 'applicant' => 'شهرداری شریفیه', + 'user_id' => 470, + 'user_name' => 'اداره کل ستاد', 'created_at' => now(), 'updated_at' => now() ], @@ -51,6 +53,8 @@ class AzmayeshSeeder extends Seeder 'project_name' => 'کنترل کیفی پروژه های اجرایی شهرداری الوند', 'consultant' => '-', 'applicant' => 'شهرداری الوند', + 'user_id' => 470, + 'user_name' => 'اداره کل ستاد', 'created_at' => now(), 'updated_at' => now() ], @@ -71,6 +75,8 @@ class AzmayeshSeeder extends Seeder 'project_name' => 'کنترل کیفی پروژه های روستای ولدآباد', 'consultant' => '-', 'applicant' => 'بنیاد مسکن استان قزوین', + 'user_id' => 470, + 'user_name' => 'اداره کل ستاد', 'created_at' => now(), 'updated_at' => now() ], @@ -91,6 +97,8 @@ class AzmayeshSeeder extends Seeder 'project_name' => 'نیوجرسی مفصلی جهت ایمن سازی آزادراه های قزوین-کرج و قزوین-زنجان', 'consultant' => '-', 'applicant' => 'اداره کل راهداری و حمل و نقل جاده ای استان قزوین', + 'user_id' => 470, + 'user_name' => 'اداره کل ستاد', 'created_at' => now(), 'updated_at' => now() ], @@ -111,6 +119,8 @@ class AzmayeshSeeder extends Seeder 'project_name' => 'احداث راه های دسترسی مجتمع خدمات رفاهی حوزه آبیک-مجتمع آقای کلانی', 'consultant' => '-', 'applicant' => 'اداره کل راهداری و حمل و نقل جاده ای استان قزوین', + 'user_id' => 470, + 'user_name' => 'اداره کل ستاد', 'created_at' => now(), 'updated_at' => now() ], @@ -131,6 +141,8 @@ class AzmayeshSeeder extends Seeder 'project_name' => 'آماده سازی محوطه دپوی سیلو کارخانه قند قزوین', 'consultant' => '-', 'applicant' => 'شرکت سهامی عام کارخانجات قند قزوین', + 'user_id' => 470, + 'user_name' => 'اداره کل ستاد', 'created_at' => now(), 'updated_at' => now() ], diff --git a/tests/Feature/V3/Azmayesh/StoreTest.php b/tests/Feature/V3/Azmayesh/StoreTest.php index 9f1d71ea..57a5c14c 100644 --- a/tests/Feature/V3/Azmayesh/StoreTest.php +++ b/tests/Feature/V3/Azmayesh/StoreTest.php @@ -386,6 +386,8 @@ class StoreTest extends TestCase 'consultant' => $data['consultant'], 'applicant' => $data['applicant'], 'province_id' => $data['province_id'], + 'user_id' => $user->id, + 'user_name' => $user->name, ]); } } From 985fda5cce633699ac42ecf1c2f21ae1ed8095fc Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Mon, 2 Dec 2024 10:44:25 +0330 Subject: [PATCH 2/3] resolve constrained delete --- .../Controllers/V3/Azmayesh/AzmayeshTypeController.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php index f3f372da..448f9d76 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -9,6 +9,7 @@ use App\Http\Requests\V3\AzmayeshType\UpdateRequest; use App\Http\Traits\ApiResponse; use App\Models\AzmayeshField; use App\Models\AzmayeshType; +use Illuminate\Database\QueryException; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; @@ -109,8 +110,13 @@ class AzmayeshTypeController extends Controller */ public function destroy(AzmayeshType $azmayeshType): JsonResponse { - $azmayeshType->delete(); + try { + $azmayeshType->delete(); - return $this->successResponse(); + return $this->successResponse(); + } + catch (QueryException $exception) { + return $this->errorResponse('can not be deleted'); + } } } From 49c27d8c547a38184a4ac4f62f55c60f22c8dd0d Mon Sep 17 00:00:00 2001 From: amirghasempoor Date: Mon, 2 Dec 2024 10:56:07 +0330 Subject: [PATCH 3/3] customize the message --- app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php index 448f9d76..5fd99ad0 100644 --- a/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php +++ b/app/Http/Controllers/V3/Azmayesh/AzmayeshTypeController.php @@ -116,7 +116,7 @@ class AzmayeshTypeController extends Controller return $this->successResponse(); } catch (QueryException $exception) { - return $this->errorResponse('can not be deleted'); + return $this->errorResponse('امکان حذف وجود ندارد. ابتدا آزمایش های مرتبط با این نوع را حذف نمایید'); } } }