Skip to content

Commit

Permalink
feat: create logic for show challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskorbakov committed Oct 27, 2024
1 parent 1578387 commit c468026
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/DTO/Api/Challenge/Response/ChallengeShowDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace App\DTO\Api\Challenge\Response;

use App\Models\Challenge;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
use Spatie\LaravelData\Data;

class ChallengeShowDTO extends Data
{
public function __construct(
public Challenge $challenge,
public Collection $members,
) {
}

public static function fromModel(Challenge $challenge): self
{
return new self(
$challenge,
$challenge->users()->get(),
);
}
}
8 changes: 8 additions & 0 deletions app/Http/Controllers/Api/ChallengeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api;

use App\DTO\Api\Challenge\Request\ChallengeFilterDTO;
use App\Models\Challenge;
use App\Services\Api\ChallengeService;

class ChallengeController extends Controller
Expand All @@ -16,4 +17,11 @@ public function index(ChallengeFilterDTO $challengeFilterDTO): array
{
return $this->challengeService->index($challengeFilterDTO);
}

public function show(int $id): array
{
$challenge = Challenge::query()->findOrFail($id);

return $this->challengeService->show($challenge);
}
}
6 changes: 6 additions & 0 deletions app/Services/Api/ChallengeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Services\Api;

use App\DTO\Api\Challenge\Request\ChallengeFilterDTO;
use App\DTO\Api\Challenge\Response\ChallengeShowDTO;
use App\Models\Challenge;
use Illuminate\Database\Eloquent\Builder;

Expand All @@ -25,4 +26,9 @@ public function index(ChallengeFilterDTO $challengeFilterDTO): array

return $games->toArray();
}

public function show(Challenge $challenge): array
{
return ChallengeShowDTO::from($challenge)->toArray();
}
}
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

Route::group(['prefix' => 'challenges'], static function () {
Route::get('/', [ChallengeController::class, 'index'])->name('teams.index');
Route::get('/{id}', [ChallengeController::class, 'show'])->name('teams.show');
});
});

Expand Down

0 comments on commit c468026

Please sign in to comment.