Skip to content

Commit

Permalink
storing project file with original name (#7)
Browse files Browse the repository at this point in the history
* storing project file with original name

* github workflows

* file_name in resource

---------

Co-authored-by: dawid.miklas <dawid.miklas@escolasoft.com>
  • Loading branch information
dicani0 and dawid.miklas committed Jun 26, 2023
1 parent cc993a3 commit 5f6df6d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

services:
mysql:
image: mariadb
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: database
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:

services:
mysql:
image: mariadb
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: database
Expand Down
1 change: 1 addition & 0 deletions src/Http/Resources/ProjectSolutionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function toArray($request): array
'id' => $this->getKey(),
'created_at' => $this->created_at,
'file_url' => Storage::url($this->path),
'file_name' => basename($this->path),
'topic_id' => $this->topic_id,
'user_id' => $this->user_id,
];
Expand Down
5 changes: 4 additions & 1 deletion src/Services/ProjectSolutionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public function findAllByUser(CriteriaDto $criteriaDto, PageDto $pageDto, int $u

public function create(CreateProjectSolutionDto $dto): ProjectSolution
{
$path = $dto->getFile()->storePublicly(self::DIR . DIRECTORY_SEPARATOR . $dto->getTopicId() . DIRECTORY_SEPARATOR . $dto->getUserId());
$path = $dto->getFile()->storeAs(
self::DIR . DIRECTORY_SEPARATOR . $dto->getTopicId() . DIRECTORY_SEPARATOR . $dto->getUserId(),
$dto->getFile()->getClientOriginalName(),
);

/** @var ProjectSolution $projectSolution */
$projectSolution = $this->projectSolutionRepository->create(array_merge($dto->toArray(), [
Expand Down
15 changes: 12 additions & 3 deletions tests/Api/ProjectSolutionCreateApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class ProjectSolutionCreateApiTest extends TestCase
{
Expand Down Expand Up @@ -69,12 +70,20 @@ public function testCreateProjectSolution(): void
$student = $this->makeStudent();
$this->course->users()->sync($student);

$this->actingAs($student, 'api')
$response = $this->actingAs($student, 'api')
->postJson('api/topic-project-solutions', [
'topic_id' => $this->topic->getKey(),
'file' => UploadedFile::fake()->create('solution.zip'),
])
->assertCreated();
]);

$response
->assertCreated()
->assertJsonFragment([
'file_name' => 'solution.zip',
]);

$this->assertTrue(Str::contains($response->json('data.file_url'), 'solution.zip'));


/** @var ProjectSolution $solution */
$solution = ProjectSolution::query()->latest()->first();
Expand Down

0 comments on commit 5f6df6d

Please sign in to comment.