From 064ebf51c076658f34c58ad0c5c32c8a6f5603c1 Mon Sep 17 00:00:00 2001 From: JuanH941214 Date: Wed, 15 May 2024 11:36:23 +0200 Subject: [PATCH] refactoring student/project endpoint --- app/Exceptions/ProjectNotFoundException.php | 19 ------------------ .../api/StudentProjectsDetailController.php | 8 ++++---- .../Student/StudentProjectsDetailService.php | 17 ++++++---------- .../StudentProjectsDetailServiceTest.php | 17 ++++++++-------- .../StudentProjectsDetailControllerTest.php | 20 +++---------------- 5 files changed, 22 insertions(+), 59 deletions(-) delete mode 100644 app/Exceptions/ProjectNotFoundException.php diff --git a/app/Exceptions/ProjectNotFoundException.php b/app/Exceptions/ProjectNotFoundException.php deleted file mode 100644 index f85757ca..00000000 --- a/app/Exceptions/ProjectNotFoundException.php +++ /dev/null @@ -1,19 +0,0 @@ -studentProjectsDetailService = $studentProjectsDetailService; } - public function __invoke($uuid): JsonResponse + public function __invoke($studentId): JsonResponse { try { - $service = $this->studentProjectsDetailService->execute($uuid); + $service = $this->studentProjectsDetailService->execute($studentId); return response()->json(['projects' => $service]); - } catch (StudentNotFoundException | ProjectNotFoundException | ResumeNotFoundException $e) { + } catch (StudentNotFoundException | ResumeNotFoundException $e) { return response()->json(['message' => $e->getMessage()], $e->getCode()); } catch (\Exception $e) { return response()->json(['message' => $e->getMessage()], $e->getCode() ?: 500); diff --git a/app/Service/Student/StudentProjectsDetailService.php b/app/Service/Student/StudentProjectsDetailService.php index 04a4f1a7..50e9705d 100644 --- a/app/Service/Student/StudentProjectsDetailService.php +++ b/app/Service/Student/StudentProjectsDetailService.php @@ -9,26 +9,26 @@ use App\Models\Tag; use App\Exceptions\StudentNotFoundException; use App\Exceptions\ResumeNotFoundException; -use App\Exceptions\ProjectNotFoundException; + class StudentProjectsDetailService { - public function execute($uuid) + public function execute($studentId) { - $student = $this->getStudent($uuid); + $student = $this->getStudent($studentId); $resume = $this->getResume($student); $projects = $this->getProjects($resume); return $this->formatProjectsDetail($projects); } - private function getStudent($uuid) + private function getStudent($studentId) { - $student = Student::where('id', $uuid)->with('resume')->first(); + $student = Student::where('id', $studentId)->with('resume')->first(); if (!$student) { - throw new StudentNotFoundException($uuid); + throw new StudentNotFoundException($studentId); } return $student; @@ -49,11 +49,6 @@ private function getProjects($resume) { $projectIds = json_decode($resume->project_ids); $projects = Project::findMany($projectIds); - - if ($projects->isEmpty()) { - throw new ProjectNotFoundException($resume->student_id); - } - return $projects; } diff --git a/tests/Feature/Service/StudentProjectsDetailServiceTest.php b/tests/Feature/Service/StudentProjectsDetailServiceTest.php index 326d975e..4aabf41f 100644 --- a/tests/Feature/Service/StudentProjectsDetailServiceTest.php +++ b/tests/Feature/Service/StudentProjectsDetailServiceTest.php @@ -11,8 +11,9 @@ use Tests\Fixtures\ProjectsForResume; use App\Service\Student\StudentProjectsDetailService; use App\Exceptions\StudentNotFoundException; -use App\Exceptions\ProjectNotFoundException; use App\Exceptions\ResumeNotFoundException; +use App\Models\Student; + class StudentProjectsDetailServiceTest extends TestCase @@ -61,16 +62,16 @@ public function test_execute_throws_exception_for_student_without_resume() $this->expectException(ResumeNotFoundException::class); $this->projectsService->execute($student->id); } -public function test_execute_throws_exception_for_student_with_empty_projects() + +public function testProjectsServiceReturnsEmptyArrayWhenNoProjectsFound(): void { - - $student = Students::aStudent(); - + $student = Student::factory()->create(); Resumes::createResumeWithEmptyProjects($student->id); - - $this->expectException(ProjectNotFoundException::class); - $this->projectsService->execute($student->id); + $service = new StudentProjectsDetailService(); + $projects = $service->execute($student->id); + $this->assertIsArray($projects); + $this->assertEmpty($projects); } diff --git a/tests/Feature/Student/StudentProjectsDetailControllerTest.php b/tests/Feature/Student/StudentProjectsDetailControllerTest.php index 552a403f..4fc272da 100644 --- a/tests/Feature/Student/StudentProjectsDetailControllerTest.php +++ b/tests/Feature/Student/StudentProjectsDetailControllerTest.php @@ -8,11 +8,13 @@ use App\Models\Student; use App\Models\Project; use Tests\Fixtures\Students; -use Tests\Fixtures\Resumes; use App\Models\Resume; +use Illuminate\Foundation\Testing\DatabaseTransactions; + class StudentProjectsDetailControllerTest extends TestCase { + use DatabaseTransactions; protected $student; protected $projects; @@ -58,22 +60,6 @@ public function test_controller_returns_404_with_invalid_uuid() $response->assertStatus(404); } - public function test_controller_returns_404_with_no_projects_listed() - { - $student = Students::aStudent(); - - Resumes::createResumeWithEmptyProjects($student->id); - - Resume::where('student_id', $student->id) - ->update(['project_ids' => '[]']); - - $response = $this->get(route('projects.list', ['student' => $student->id])); - - $response->assertJson(['message' => 'No s\'ha trobat cap projecte associat a aquest estudiant amb ID: ' . $student->id]); - - $response->assertStatus(404); - } - public function test_controller_returns_404_with_no_resume() { $student = Students::aStudent();