Skip to content

Commit

Permalink
feat: add logic
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskorbakov committed Oct 26, 2024
1 parent ecc18fb commit c61cf87
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 5 deletions.
19 changes: 19 additions & 0 deletions app/DTO/Api/Team/Request/TeamUpdateDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\DTO\Api\Team\Request;

use Illuminate\Http\UploadedFile;
use Spatie\LaravelData\Attributes\Validation\Max;
use Spatie\LaravelData\Data;

class TeamUpdateDTO extends Data
{
public function __construct(
#[Max(255)]
public ?string $name,
#[Max(255)]
public ?string $description,
public ?UploadedFile $image,
){
}
}
30 changes: 30 additions & 0 deletions app/Http/Controllers/Api/TeamController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\DTO\Api\Team\Request\TeamUpdateDTO;
use App\Models\Team;
use App\Services\Api\TeamService;
use Illuminate\Http\JsonResponse;

class TeamController extends Controller
{
public function __construct(
protected TeamService $teamService
){
}

public function show(int $id): array
{
return Team::query()->findOrFail($id)?->toArray();
}

public function update(int $id, TeamUpdateDTO $teamUpdateDTO): array|JsonResponse
{
$team = Team::query()->findOrFail($id);

return $this->teamService->update($team, $teamUpdateDTO);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Enums;
namespace App\Models\Enums\Challenge;

enum ChallengeType: string
{
Expand Down
3 changes: 1 addition & 2 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ class Team extends Model
{
use HasFactory;

public $timestamps = true;

protected $fillable = [
'image',
'name',
'description',
'captain_id'
Expand Down
34 changes: 34 additions & 0 deletions app/Services/Api/TeamService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Services\Api;

use App\DTO\Api\Team\Request\TeamUpdateDTO;
use App\Models\Team;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;

class TeamService
{
public function update(Team $team, TeamUpdateDTO $teamUpdateDTO): array|JsonResponse
{
if ($team->captain_id === auth()->id()) {
$savedImage = null;

if ($teamUpdateDTO->image) {
$savedImage = $teamUpdateDTO->image->store(options: ['disk' => 'public']);
}

$team->update([
'image' => Storage::disk('public')->url($savedImage),
'name' => $teamUpdateDTO->name,
'description' => $teamUpdateDTO->description,
]);

return $team->toArray();
}

return response()->json(['message' => 'you are dont captain'], 403);
}
}
2 changes: 1 addition & 1 deletion config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,6 @@
|
*/

'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 10800),
'password_timeout' => env('AUTH_PASSWORD_TIMEOUT', 1080000000),

];
2 changes: 1 addition & 1 deletion database/factories/ChallengeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Database\Factories;

use App\Enums\ChallengeType;
use App\Models\Achievement;
use App\Models\Enums\Challenge\ChallengeType;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;

Expand Down
6 changes: 6 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\TeamController;
use App\Http\Controllers\Api\UserController;
use Illuminate\Support\Facades\Route;

Expand All @@ -17,4 +18,9 @@
Route::get('/{id}/challenges', [UserController::class, 'challenge'])->name('users.challenge');
Route::get('/{id}/achievements', [UserController::class, 'achievement'])->name('users.achievement');
});

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

0 comments on commit c61cf87

Please sign in to comment.