Skip to content

Commit

Permalink
Merge branch 'refactor/#148/refactor-services' of https://github.com/…
Browse files Browse the repository at this point in the history
…IT-Academy-BCN/ita-profiles-backend into refactor/#148/refactor-services
  • Loading branch information
Agonpas committed May 15, 2024
2 parents ab0c07e + 064ebf5 commit f06ec14
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 59 deletions.
19 changes: 0 additions & 19 deletions app/Exceptions/ProjectNotFoundException.php

This file was deleted.

8 changes: 4 additions & 4 deletions app/Http/Controllers/api/StudentProjectsDetailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Http\JsonResponse;
use App\Exceptions\StudentNotFoundException;
use App\Exceptions\ResumeNotFoundException;
use App\Exceptions\ProjectNotFoundException;



class StudentProjectsDetailController extends Controller
Expand All @@ -21,12 +21,12 @@ public function __construct(StudentProjectsDetailService $studentProjectsDetailS
$this->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);
Expand Down
17 changes: 6 additions & 11 deletions app/Service/Student/StudentProjectsDetailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down
17 changes: 9 additions & 8 deletions tests/Feature/Service/StudentProjectsDetailServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}


Expand Down
20 changes: 3 additions & 17 deletions tests/Feature/Student/StudentProjectsDetailControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f06ec14

Please sign in to comment.