Skip to content

Commit

Permalink
Merge pull request #227 from IT-Academy-BCN/fix/#217-update-a-project…
Browse files Browse the repository at this point in the history
…-for-a-student-to-make-sure-all-updates-happens

Fix/#217 update a project for a student to make sure all updates happens
  • Loading branch information
begoIT24 authored Jul 29, 2024
2 parents 996242a + 81eb104 commit f362ec4
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class UpdateStudentProjectAnnotation
{
/**
/**
* @OA\Put(
* path="/student/{studentId}/resume/projects/{projectId}",
* operationId="updateStudentProject",
Expand Down Expand Up @@ -50,13 +50,14 @@ class UpdateStudentProjectAnnotation
* example="Updated Company Name"
* ),
* @OA\Property(
* property="tags",
* type="array",
* @OA\Items(
* type="string",
* example="Updated Tag"
* ),
* description="List of tags associated with the project"
* property="tags",
* type="array",
* description="Array of tag IDs. Pass the complete array of tags to be associated with the project.",
* example={1, 2, 3},
* @OA\Items(
* type="integer",
* description="Tag ID"
* )
* ),
* @OA\Property(
* property="github_url",
Expand Down
7 changes: 4 additions & 3 deletions app/Http/Requests/UpdateStudentProjectRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public function authorize(): bool
public function rules(): array
{
return [
'project_name' => 'string|regex:/^([^0-9]*)$/|nullable',
'company_name' => 'string|regex:/^([^0-9]*)$/|nullable',
'tags' => 'array|nullable',
'project_name' => 'string|nullable',
'company_name' => 'string|nullable',
'tags' => 'array|nullable',
'tags.*' => 'integer|exists:tags,id',
'github_url' => 'string|url|max:60|nullable',
'project_url' => 'string|url|max:60|nullable',
];
Expand Down
15 changes: 9 additions & 6 deletions app/Service/Student/UpdateStudentProjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
use App\Models\Project;
use App\Models\Student;
use App\Models\Company;
use App\Models\Tag;
use Illuminate\Support\Facades\DB;
use App\Exceptions\StudentNotFoundException;
use App\Exceptions\ProjectNotFoundException;
use App\Exceptions\UnauthorizedException;


class UpdateStudentProjectService
{
Expand All @@ -20,7 +19,6 @@ public function execute(string $studentId, string $projectId, array $data): void
DB::transaction(function () use ($studentId, $projectId, $data) {
$this->getStudent($studentId);
$project = $this->getProject($projectId);

$this->updateProject($project, $data);
});
}
Expand All @@ -45,11 +43,16 @@ private function getProject(string $projectId): Project

private function updateProject(Project $project, array $data): void
{
$project->name = $data['name'] ?? $project->name;
$project->tags = json_encode($data['tags'] ?? json_decode($project->tags));
$project->name = $data['project_name'] ?? $project->name;
//$project->tags = json_encode($data['tags'] ?? json_decode($project->tags));
$project->github_url = $data['github_url'] ?? $project->github_url;
$project->project_url = $data['project_url'] ?? $project->project_url;


if (isset($data['tags'])) {
$tagsArray = $data['tags'];
$tagIds = Tag::whereIn('id', $tagsArray)->pluck('id')->toArray();
$project->tags = json_encode($tagIds);
}
if (isset($data['company_name'])) {
$company = Company::find($project->company_id);
$company->name = $data['company_name'];
Expand Down
3 changes: 2 additions & 1 deletion routes/api/v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
Route::get('languages', StudentLanguagesDetailController::class)->name('student.languages');
Route::put('languages', UpdateStudentLanguagesController::class)->name('student.languages.update');
Route::get('modality', StudentModalityController::class)->name('student.modality');
Route::put('projects/{projectId}', UpdateStudentProjectController::class)->name('student.updateproject');
Route::put('projects/{projectId}', UpdateStudentProjectController::class)->name('student.updateProject');
//Route::put('skills', UpdateStudentSkillsController::class)->middleware('auth:api')->name('student.skills');
Route::put('skills', UpdateStudentSkillsController::class)->middleware('auth:api', EnsureStudentOwner::class)->name('student.skills');
Route::put('profile', UpdateStudentProfileController::class)->name('student.updateProfile');
Route::post('languages', AddStudentLanguageController::class)->name('student.addLanguage');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected function setUp(): void

public function testControllerReturns200WithCorrectUuids(): void
{
$response = $this->put(route('student.updateproject', ['studentId' => $this->student->id, 'projectId' => $this->project->id]), [
$response = $this->put(route('student.updateProject', ['studentId' => $this->student->id, 'projectId' => $this->project->id]), [
'project_url' => 'https://new-project-url.com'
]);

Expand All @@ -46,7 +46,7 @@ public function testControllerReturns404WithIncorrectStudentUuid(): void
{
$invalidUuid = 'invalid_uuid';

$response = $this->put(route('student.updateproject', ['studentId' => $invalidUuid, 'projectId' => $this->project->id]), [
$response = $this->put(route('student.updateProject', ['studentId' => $invalidUuid, 'projectId' => $this->project->id]), [
'project_url' => 'https://new-project-url.com'
]);

Expand All @@ -58,7 +58,7 @@ public function testControllerReturns404WithIncorrectProjectUuid(): void
{
$invalidUuid = 'invalid_uuid';

$response = $this->put(route('student.updateproject', ['studentId' => $this->student->id, 'projectId' => $invalidUuid]), [
$response = $this->put(route('student.updateProject', ['studentId' => $this->student->id, 'projectId' => $invalidUuid]), [
'project_url' => 'https://new-project-url.com'
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Unit\Service\Student;
namespace Tests\Feature\Service\Student;

use Tests\TestCase;
use App\Models\Project;
Expand Down Expand Up @@ -35,8 +35,8 @@ public function testCanUpdateStudentProjectByProjectId(): void
$service = new UpdateStudentProjectService();

$data = [
'name' => 'Project One',
'tags' => ['tag1', 'tag2'],
'project_name' => 'Project One',
'tags' => ['1', '2'],
'github_url' => 'https://github.com/project1',
'project_url' => 'https://project1.com',
'company_name' => 'Company Name'
Expand All @@ -49,7 +49,7 @@ public function testCanUpdateStudentProjectByProjectId(): void
$this->assertEquals('Project One', $updatedProject->name);

$updatedTags = json_decode($updatedProject->tags, true);
$this->assertEquals(['tag1', 'tag2'], $updatedTags);
$this->assertEquals(['1', '2'], $updatedTags);

$this->assertEquals('https://github.com/project1', $updatedProject->github_url);
$this->assertEquals('https://project1.com', $updatedProject->project_url);
Expand Down

0 comments on commit f362ec4

Please sign in to comment.