From ba3ea4570efc675bf67bf362cde0fd9f3af32a14 Mon Sep 17 00:00:00 2001 From: yungstarry <111007711+yungstarry@users.noreply.github.com> Date: Thu, 8 Aug 2024 14:22:46 +0100 Subject: [PATCH 1/3] feat/ get user regions by id --- .../Api/V1/PreferenceController.php | 51 ++++++++--- database/factories/RegionFactory.php | 28 ++++++ database/seeders/RegionSeeder.php | 18 ++++ public/uploads/1723122054.jpg | Bin 0 -> 695 bytes routes/api.php | 4 + tests/Feature/PreferenceControllerTest.php | 82 ++++++++++++++++++ 6 files changed, 169 insertions(+), 14 deletions(-) create mode 100644 database/factories/RegionFactory.php create mode 100644 database/seeders/RegionSeeder.php create mode 100644 public/uploads/1723122054.jpg create mode 100644 tests/Feature/PreferenceControllerTest.php diff --git a/app/Http/Controllers/Api/V1/PreferenceController.php b/app/Http/Controllers/Api/V1/PreferenceController.php index 29f63c4e..25af2418 100755 --- a/app/Http/Controllers/Api/V1/PreferenceController.php +++ b/app/Http/Controllers/Api/V1/PreferenceController.php @@ -6,6 +6,7 @@ use App\Http\Requests\Preference\DeletePreferenceRequest; use App\Http\Requests\Preference\StorePreferenceRequest; use App\Http\Requests\Preference\UpdatePreferenceRequest; +use App\Models\Preference; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; @@ -29,13 +30,13 @@ public function index() 'message' => 'Languages fetched successfully', 'preferences' => $preferences ], 200); - } // - - public function store(StorePreferenceRequest $request){ + + public function store(StorePreferenceRequest $request) + { if (!auth()->check()) { return response()->json([ 'status' => 401, @@ -44,7 +45,7 @@ public function store(StorePreferenceRequest $request){ } $validated = $request->validated(); - + if (!$validated) { return response()->json([ 'status' => 400, @@ -52,12 +53,12 @@ public function store(StorePreferenceRequest $request){ 'errors' => $validator->errors() ], 400); } - + try { $preference = Auth::user()->preferences()->create($request->all()); - + Log::info('Preference created', ['user_id' => Auth::id(), 'preference' => $preference]); - + return response()->json([ 'status' => 201, 'message' => 'Preference created successfully', @@ -67,10 +68,9 @@ public function store(StorePreferenceRequest $request){ 'value' => $preference->value, ] ], 201); - } catch (\Exception $e) { Log::error("Error creating preference: {$e->getMessage()}"); - + return response()->json([ 'status' => 500, 'message' => 'Internal Server Error', @@ -89,7 +89,7 @@ public function update(UpdatePreferenceRequest $request, $id) } $validated = $request->validated(); - + if (!$validated) { return response()->json([ 'status' => 400, @@ -101,7 +101,7 @@ public function update(UpdatePreferenceRequest $request, $id) try { $preference = Auth::user()->preferences()->findOrFail($id); - if(!$preference) { + if (!$preference) { return response()->json([ 'status' => 404, 'message' => 'Preference not found', @@ -130,7 +130,7 @@ public function update(UpdatePreferenceRequest $request, $id) ], 500); } } - + public function delete(DeletePreferenceRequest $request, $id) { $preference = Auth::user()->preferences()->find($id); @@ -145,12 +145,35 @@ public function delete(DeletePreferenceRequest $request, $id) $preference->delete(); Log::info('Preference deleted', ['user_id' => Auth::id(), 'preference_id' => $id]); - + return response()->json([ 'status' => 200, 'message' => 'Preference deleted successfully.', ], 200); } - + public function showRegion($user_id) + { + $preference = Preference::where('user_id', $user_id)->first(); + if ($preference) { + if ($preference->region) { + return response()->json([ + 'status' => 200, + 'message' => "Region retrieved successfully", + 'data' => $preference->region, + ], 200); + } else { + return response()->json([ + 'status' => 200, + 'message' => "Region has not been set", + 'data' => [], + ], 200); + } + } else { + return response()->json([ + 'status' => 404, + 'message' => 'Preference not found for user', + ], 404); + } + } } diff --git a/database/factories/RegionFactory.php b/database/factories/RegionFactory.php new file mode 100644 index 00000000..73697c94 --- /dev/null +++ b/database/factories/RegionFactory.php @@ -0,0 +1,28 @@ + + */ + public function definition(): array + { + return [ + 'id' => $this->faker->uuid(), + 'name' => $this->faker->city(), + + ]; + } +} diff --git a/database/seeders/RegionSeeder.php b/database/seeders/RegionSeeder.php new file mode 100644 index 00000000..61d3ed29 --- /dev/null +++ b/database/seeders/RegionSeeder.php @@ -0,0 +1,18 @@ +count(10)->create(); // Creates 10 regions + } +} diff --git a/public/uploads/1723122054.jpg b/public/uploads/1723122054.jpg new file mode 100644 index 0000000000000000000000000000000000000000..b00797624aa746a4a3578303e4fa34d50be62291 GIT binary patch literal 695 zcmex=_1P|rX?qqI0P zFI~aY%U!`Mz|~!$%)&rZMU_b4?usLlYAdd38%$3nLpnV-q8g zA&i`yoIKn-61=<;Mv5|uMkIs(2N(o77`Pa?m>HEAm;@P_1sVSzVUTBFU}OdQ7UW?l zU}R!uVP#|I;N;>4D%dK(z{JSR%*4XX%F4n5R9y>{XJ8Rz6;d>GWD^cdWLGK_F>0K+ zkVDyN<3Z7&iyu^slZu)+xx~aJB&Af<)HO7&0Dr^+rDGx zu0w~996fgY#K}{aE?>EN?fQ+Iw;n!v{N(Ag=PzEq`uOSdm#^Qx|M>X}>z(JGL-`{vmgtrq9L1*V<3BCp|FxsBZr97#DyCVaw;1KeGpA5 xy2vG_V)9V+BgkuDpAqM=CbE16_ZY%ow-|Vs8G(_ ['auth:api']], function () { + Route::get('/regions/{user_id}', [PreferenceController::class, 'showRegion']); + }); // Notification settings Route::patch('/notification-settings/{user_id}', [NotificationPreferenceController::class, 'update']); diff --git a/tests/Feature/PreferenceControllerTest.php b/tests/Feature/PreferenceControllerTest.php new file mode 100644 index 00000000..18643922 --- /dev/null +++ b/tests/Feature/PreferenceControllerTest.php @@ -0,0 +1,82 @@ +user = User::factory()->create(); + $this->preference = Preference::factory()->create(['user_id' => $this->user->id]); + $this->region = Region::factory()->create(); + } + + public function test_get_region() + { + // Authenticate the user with a valid JWT token + $token = JWTAuth::fromUser($this->user); + + // Scenario: Region is set + $this->preference->region_id = $this->region->id; + $this->preference->save(); + + $response = $this->withHeaders([ + 'Authorization' => "Bearer $token", + ])->getJson("/api/v1/regions/{$this->user->id}"); + + $response->assertStatus(200) + ->assertJson([ + 'status' => 200, + 'message' => 'Region retrieved successfully', + 'data' => [ + 'id' => $this->region->id, + 'name' => $this->region->name, + ], + ]); + + // Scenario: Region is not set + $this->preference->region_id = null; + $this->preference->save(); + + $response = $this->withHeaders([ + 'Authorization' => "Bearer $token", + ])->getJson("/api/v1/regions/{$this->user->id}"); + + $response->assertStatus(200) + ->assertJson([ + 'status' => 200, + 'message' => 'Region has not been set', + 'data' => [], + ]); + + // Scenario: Preference not found + $newUser = User::factory()->create(); + $newToken = JWTAuth::fromUser($newUser); + + $response = $this->withHeaders([ + 'Authorization' => "Bearer $newToken", + ])->getJson("/api/v1/regions/{$newUser->id}"); + + $response->assertStatus(404) + ->assertJson([ + 'status' => 404, + 'message' => 'Preference not found for user', + ]); + } +} From 1bc397d2f3bbd5ae00c203c576b92509b05b94d7 Mon Sep 17 00:00:00 2001 From: yungstarry <111007711+yungstarry@users.noreply.github.com> Date: Thu, 8 Aug 2024 20:42:33 +0100 Subject: [PATCH 2/3] feat: get user region --- app/Http/Controllers/Api/V1/PreferenceController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/V1/PreferenceController.php b/app/Http/Controllers/Api/V1/PreferenceController.php index 7169c10a..5c44a4f1 100755 --- a/app/Http/Controllers/Api/V1/PreferenceController.php +++ b/app/Http/Controllers/Api/V1/PreferenceController.php @@ -154,7 +154,7 @@ public function delete(DeletePreferenceRequest $request, $id) 'message' => 'Preference deleted successfully.', ], 200); } - +//show region public function showRegion($user_id) { $preference = Preference::where('user_id', $user_id)->first(); From b2a324384cbbc77171da0d36c69729990816e99f Mon Sep 17 00:00:00 2001 From: yungstarry <111007711+yungstarry@users.noreply.github.com> Date: Thu, 8 Aug 2024 21:40:45 +0100 Subject: [PATCH 3/3] feat: get user region --- app/Http/Controllers/Api/V1/PreferenceController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/V1/PreferenceController.php b/app/Http/Controllers/Api/V1/PreferenceController.php index 5c44a4f1..fded0ddd 100755 --- a/app/Http/Controllers/Api/V1/PreferenceController.php +++ b/app/Http/Controllers/Api/V1/PreferenceController.php @@ -154,7 +154,7 @@ public function delete(DeletePreferenceRequest $request, $id) 'message' => 'Preference deleted successfully.', ], 200); } -//show region +//show regions public function showRegion($user_id) { $preference = Preference::where('user_id', $user_id)->first();