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

refactor: implementing Laravel's model binding on student.modality #336

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class ModalityAnnotation
{
/**
* @OA\Get(
* path="/student/{studentId}/resume/modality/",
* path="/student/{student}/resume/modality/",
* operationId="getStudentResumeModality",
* summary="Get the modality of a specific resume",
* description="Returns the modality of a specific student's resume ",
* tags={"Student -> Resume"},
* @OA\Parameter(
* name="studentId",
* name="student",
* in="path",
* description="Student ID",
* required=true,
Expand All @@ -43,7 +43,7 @@ class ModalityAnnotation
* description="Student or Resume not found",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="message", type="string", example="No s'ha trobat cap estudiant amb aquest ID {studentId}"),
* @OA\Property(property="message", type="string", example="No s'ha trobat cap estudiant amb aquest ID {student}"),
* @OA\Property(property="message2", type="string", example="No s'ha trobat cap currículum per a l'estudiant amb id: {studentId}")
* )
* ),
Expand Down
26 changes: 7 additions & 19 deletions app/Http/Controllers/api/Student/StudentModalityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,19 @@

namespace App\Http\Controllers\api\Student;

use Exception;
use App\Http\Controllers\Controller;
use App\Service\Student\StudentModalityService;
use App\Exceptions\StudentNotFoundException;
use App\Exceptions\ResumeNotFoundException;
use App\Models\Student;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;

class StudentModalityController extends Controller
{
private StudentModalityService $studentModalityService;

public function __construct(StudentModalityService $studentModalityService)
public function __invoke(Student $student): JsonResponse
{
$this->studentModalityService = $studentModalityService;
}
$resume = $student->resume ?? throw new ModelNotFoundException();
$modality = $resume->modality;

public function __invoke(string $studentId): JsonResponse
{
try {
$service = $this->studentModalityService->execute($studentId);
return response()->json(['modality' => $service]);
} catch (StudentNotFoundException | ResumeNotFoundException $e) {
return response()->json(['message' => $e->getMessage()], $e->getCode());
} catch (Exception $e) {
return response()->json(['message' => $e->getMessage()], $e->getCode() ?: 500);
}
return response()->json(['modality' => $modality]);
}
}
}
36 changes: 0 additions & 36 deletions app/Service/Student/StudentModalityService.php

This file was deleted.

2 changes: 1 addition & 1 deletion routes/api/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
Route::get('student/{student}/resume/collaborations', StudentCollaborationDetailController::class)->name('student.collaborations');
Route::put('student/{student}/resume/collaborations', UpdateStudentCollaborationsController::class)->name('student.updateCollaborations');
Route::put('student/{student}/resume/photo', UpdateStudentImageController::class)->name('student.updatePhoto');
Route::get('student/{student}/resume/modality', StudentModalityController::class)->name('student.modality');

Route::prefix('student/{studentId}/resume')->group(function () {
Route::put('languages', UpdateStudentLanguagesController::class)->name('student.languages.update');
Route::get('modality', StudentModalityController::class)->name('student.modality');
Route::get('photo', GetStudentImageController::class)->middleware('auth:api', EnsureStudentOwner::class)->name('student.photo.get');
Route::delete('languages/{languageId}', DeleteStudentResumeLanguageController::class)->name('student.language.delete');
});
Expand Down
30 changes: 6 additions & 24 deletions tests/Feature/Controller/Student/StudentModalityControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,38 @@
use Tests\TestCase;
use Tests\Fixtures\Students;
use Tests\Fixtures\Resumes;
use App\Service\Student\StudentModalityService;
use App\Http\Controllers\api\Student\StudentModalityController;

class StudentModalityControllerTest extends TestCase
{
use DatabaseTransactions;

public function testStudentModalityControllerReturns_200StatusForValidStudentUuidWithModality():void
public function testStudentModalityControllerReturns_200StatusForValidStudentUuidWithModality(): void
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the underscore

{
$student = Students::aStudent();

$studentId = $student->id;

Resumes::createResumeWithModality($studentId, 'frontend', [1, 3], 'Presencial');

$response = $this->getJson(route('student.modality', ['studentId' => $studentId]));
$response = $this->getJson(route('student.modality', ['student' => $studentId]));

$response->assertStatus(200);

$response->assertJsonStructure(['modality']);
}

public function testStudentModalityControllerReturns_404StatusAndResumeNotFoundExceptionMessageForValidStudentUuidWithoutResume():void
public function testStudentModalityControllerReturns_404StatusAndResumeNotFoundExceptionMessageForValidStudentUuidWithoutResume(): void
{
$student = Students::aStudent();

$studentId = $student->id;

$response = $this->getJson(route('student.modality', ['studentId' => $studentId]));
$response = $this->getJson(route('student.modality', ['student' => $studentId]));

$response->assertStatus(404);

$response->assertJson(['message' => 'No s\'ha trobat cap currículum per a l\'estudiant amb id: ' . $studentId]);
}

public function testStudentModalityControllerReturns_404StatusAndStudentNotFoundExceptionMessageForInvalidStudentUuid(): void
{
$response = $this->getJson(route('student.modality', ['studentId' => 'nonExistentStudentId']));
$response->assertStatus(404);
$response->assertJson(['message' => 'No s\'ha trobat cap estudiant amb aquest ID: nonExistentStudentId']);
}

public function testStudentModalityControllerCanBeInstantiated():void
{
$studentModalityService = $this->createMock(StudentModalityService::class);

$controller = new StudentModalityController($studentModalityService);
$response = $this->getJson(route('student.modality', ['student' => 'nonExistentStudentId']));

$this->assertInstanceOf(StudentModalityController::class, $controller);
$response->assertStatus(404);
}
}


79 changes: 0 additions & 79 deletions tests/Feature/Service/Student/StudentModalityServiceTest.php

This file was deleted.

Loading