Skip to content

Commit

Permalink
Merge pull request #433 from yungstarry/feat/fetchregion
Browse files Browse the repository at this point in the history
feat: get user regions by id
  • Loading branch information
timiajayi authored Aug 9, 2024
2 parents 77085fc + b2a3243 commit 1c984e4
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 13 deletions.
49 changes: 36 additions & 13 deletions app/Http/Controllers/Api/V1/PreferenceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,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_code' => 401,
Expand All @@ -48,20 +48,20 @@ public function store(StorePreferenceRequest $request){
}

$validated = $request->validated();

if (!$validated) {
return response()->json([
'status_code' => 400,
'message' => 'Validation Error',
'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_code' => 201,
'message' => 'Preference created successfully',
Expand All @@ -71,10 +71,9 @@ public function store(StorePreferenceRequest $request){
'value' => $preference->value,
]
], 201);

} catch (\Exception $e) {
Log::error("Error creating preference: {$e->getMessage()}");

return response()->json([
'status_code' => 500,
'message' => 'Internal Server Error',
Expand All @@ -93,7 +92,7 @@ public function update(UpdatePreferenceRequest $request, $id)
}

$validated = $request->validated();

if (!$validated) {
return response()->json([
'status_code' => 400,
Expand All @@ -105,7 +104,7 @@ public function update(UpdatePreferenceRequest $request, $id)
try {
$preference = Auth::user()->preferences()->findOrFail($id);

if(!$preference) {
if (!$preference) {
return response()->json([
'status_code' => 404,
'message' => 'Preference not found',
Expand Down Expand Up @@ -134,7 +133,7 @@ public function update(UpdatePreferenceRequest $request, $id)
], 500);
}
}

public function delete(DeletePreferenceRequest $request, $id)
{
$preference = Auth::user()->preferences()->find($id);
Expand All @@ -155,5 +154,29 @@ public function delete(DeletePreferenceRequest $request, $id)
'message' => 'Preference deleted successfully.',
], 200);
}

//show regions
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);
}
}
}
28 changes: 28 additions & 0 deletions database/factories/RegionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Database\Factories;

use App\Models\Region;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
* @extends
*/
class RegionFactory extends Factory
{
protected $model = Region::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'id' => $this->faker->uuid(),
'name' => $this->faker->city(),

];
}
}
18 changes: 18 additions & 0 deletions database/seeders/RegionSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Database\Seeders;

use App\Models\Region;
use Illuminate\Database\Seeder;


class RegionSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run()
{
Region::factory()->count(10)->create(); // Creates 10 regions
}
}
Binary file added public/uploads/1723122054.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@
Route::get('/user-sales', [DashboardController::class, 'recent_sales']);
});

//region get and update
Route::group(['middleware' => ['auth:api']], function () {
Route::get('/regions/{user_id}', [PreferenceController::class, 'showRegion']);
});
// Notification settings
Route::patch('/notification-settings/{user_id}', [NotificationPreferenceController::class, 'update']);

Expand Down
82 changes: 82 additions & 0 deletions tests/Feature/PreferenceControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Tests\Feature;

use App\Models\Preference;
use App\Models\Region;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Tymon\JWTAuth\Facades\JWTAuth;

class PreferenceControllerTest extends TestCase
{
use RefreshDatabase;

protected $user;
protected $preference;
protected $region;

public function setUp(): void
{
parent::setUp();

$this->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',
]);
}
}

0 comments on commit 1c984e4

Please sign in to comment.