Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] Update Job Listing Endpoint #146

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion app/Http/Controllers/Api/V1/JobController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Job;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class JobController extends Controller
{
Expand Down Expand Up @@ -60,7 +62,62 @@ public function show(string $id)
*/
public function update(Request $request, string $id)
{
//
// Validate the request data
$validator = Validator::make($request->all(), [
'title' => 'string|max:255',
'location' => 'string|max:255',
'job_type' => 'string|max:255',
'company_name' => 'string|max:255',
'organisation_id' => 'string|exists:organisations,org_id', // Ensure org_id is referenced
]);

if ($validator->fails()) {
Log::error('Validation errors:', ['errors' => $validator->errors()]);
return response()->json([
'message' => 'Invalid request data',
'errors' => $validator->errors(),
'status_code' => 400,
], 400);
}

try {
// Get the authenticated user
$user = $request->user();

// Find the job by ID
$job = Job::find($id);

if (!$job) {
return response()->json([
'message' => 'Job not found',
'status_code' => 404,
], 404);
}


// Update the job listing
$job->update($request->only([
'title',
'description',
'location',
'job_type',
'company_name',
'organisation_id',
]));

return response()->json([
'message' => 'Job listing updated successfully',
'status_code' => 200,
'data' => $job,
], 200);
} catch (\Exception $e) {
Log::error($e->getMessage());
return response()->json([
'message' => 'An error occurred',
'status_code' => 500,
'error' => $e->getMessage(),
], 500);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion database/factories/JobFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function definition(): array
'job_type' => $this->faker->randomElement(['Full-time', 'Part-time', 'Contract']),
'company_name' => $this->faker->company,
'user_id' => User::factory(),
'organisation_id' => User::factory()->create()->organisation_id,
'organisation_id' => $organisation->org_id,
];
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@

// Jobs
Route::get('/jobs', [JobController::class, 'index']);
Route::patch('/jobs/{id}', [JobController::class, 'update']);
});

Route::middleware(['auth:api', 'admin'])->get('/customers', [CustomerController::class, 'index']);
Expand Down
78 changes: 78 additions & 0 deletions tests/Feature/JobListingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use App\Models\Organisation;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Str;

class JobListingTest extends TestCase
{
Expand Down Expand Up @@ -54,4 +56,80 @@ public function test_job_listings_are_paginated()
->assertJsonPath('pagination.page_size', 15)
->assertJsonPath('pagination.total_items', 30);
}

protected function authenticateUser($user)
{
$token = JWTAuth::fromUser($user);
$this->withHeader('Authorization', 'Bearer ' . $token);
}

public function testUpdateJobWithValidData()
{
$user = User::factory()->create();
$job = Job::factory()->create(['user_id' => $user->id]);
$organisation = Organisation::factory()->create();

$this->authenticateUser($user);

$response = $this->patchJson("/api/v1/jobs/{$job->id}", [
'title' => 'Updated Job Title',
'description' => 'Updated Job Description',
'location' => 'Updated Location',
'job_type' => 'Updated Type',
'company_name' => 'Updated Company',
'organisation_id' => $organisation->org_id,
]);

$response->assertStatus(200)
->assertJson([
'message' => 'Job listing updated successfully',
'status_code' => 200,
]);

$this->assertDatabaseHas('jobs', [
'id' => $job->id,
'title' => 'Updated Job Title',
'description' => 'Updated Job Description',
'location' => 'Updated Location',
'job_type' => 'Updated Type',
'company_name' => 'Updated Company',
]);
}

public function testUpdateJobWithInvalidJobId()
{
$user = User::factory()->create();
$invalidJobId = Str::uuid();

$this->authenticateUser($user);

$response = $this->patchJson("/api/v1/jobs/{$invalidJobId}", [
'title' => 'Updated Job Title',
]);

$response->assertStatus(404)
->assertJson([
'message' => 'Job not found',
'status_code' => 404,
]);
}

public function testUpdateJobWithInvalidData()
{
$user = User::factory()->create();
$job = Job::factory()->create(['user_id' => $user->id]);

$this->authenticateUser($user);

$response = $this->patchJson("/api/v1/jobs/{$job->id}", [
'title' => '',
'description' => '',
]);

$response->assertStatus(400)
->assertJson([
'message' => 'Invalid request data',
'status_code' => 400,
]);
}
}
Loading