diff --git a/app/Filament/Resources/TeamResource.php b/app/Filament/Resources/TeamResource.php index ac2483b..43d11ea 100644 --- a/app/Filament/Resources/TeamResource.php +++ b/app/Filament/Resources/TeamResource.php @@ -3,6 +3,7 @@ namespace App\Filament\Resources; use App\Filament\Resources\TeamResource\Pages; +use App\Models\Challenge; use App\Models\Team; use Filament\Forms; use Filament\Forms\Form; @@ -39,15 +40,34 @@ public static function form(Form $form): Form Forms\Components\Textarea::make('description') ->label('Описание') ->rows(5), + Forms\Components\Select::make('captain_id') + ->label('Капитан') + ->relationship('users', 'name') + ->searchable() + ->preload(), Forms\Components\Section::make() ->schema([ Forms\Components\Select::make('users') ->label('Участники') ->relationship('users', 'name') + ->preload() ->multiple(), Forms\Components\Select::make('achievements') ->label('Достижения') ->relationship('achievements', 'name') + ->preload() + ->multiple(), + ])->columns(2), + Forms\Components\Section::make() + ->schema([ + Forms\Components\Select::make('challenges') + ->label('Челленджи') + ->relationship('challenges', 'name') + ->getOptionLabelFromRecordUsing( + function (Challenge $record) { + return $record->name . ' ' . ($record->is_finished ? '(Завершён)' : ''); + }) + ->preload() ->multiple(), ])->columns(2) ])->columns(1); diff --git a/app/Models/Challenge.php b/app/Models/Challenge.php index f2ea2bf..4e7c154 100644 --- a/app/Models/Challenge.php +++ b/app/Models/Challenge.php @@ -2,6 +2,7 @@ namespace App\Models; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; @@ -38,4 +39,9 @@ public function teams(): belongsToMany { return $this->belongsToMany(Team::class, 'teams_challenges'); } + + public function getIsFinishedAttribute(): bool + { + return $this->end_date < Carbon::now(); + } } diff --git a/app/Models/Team.php b/app/Models/Team.php index 97eeea2..2c2a679 100644 --- a/app/Models/Team.php +++ b/app/Models/Team.php @@ -2,20 +2,35 @@ namespace App\Models; +use Carbon\Carbon; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasOne; class Team extends Model { use HasFactory; + public $timestamps = true; + protected $fillable = [ 'name', 'description', - 'image' + 'captain_id' + ]; + + protected $appends = [ + 'active_challenges', + 'completed_challenges' ]; + public function captain(): HasOne + { + return $this->hasOne(User::class, 'captain_id'); + } + public function users(): belongsToMany { return $this->belongsToMany(User::class, 'users_teams'); @@ -30,4 +45,20 @@ public function achievements(): belongsToMany { return $this->belongsToMany(Achievement::class, 'teams_achievements'); } + + public function getActiveChallengesAttribute(): Collection + { + return $this->challenges() + ->where('start_date', '<=', Carbon::now()) + ->where('end_date', '>=', Carbon::now()) + ->get(); + } + + public function getCompletedChallengesAttribute(): Collection + { + return $this->challenges() + ->where('end_date', '<', Carbon::now()) + ->get(); + } + } diff --git a/config/app.php b/config/app.php index f467267..c037eca 100644 --- a/config/app.php +++ b/config/app.php @@ -78,11 +78,11 @@ | */ - 'locale' => env('APP_LOCALE', 'en'), + 'locale' => env('APP_LOCALE', 'ru'), - 'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'), + 'fallback_locale' => env('APP_FALLBACK_LOCALE', 'ru'), - 'faker_locale' => env('APP_FAKER_LOCALE', 'en_US'), + 'faker_locale' => env('APP_FAKER_LOCALE', 'ru_RU'), /* |-------------------------------------------------------------------------- diff --git a/database/factories/TeamFactory.php b/database/factories/TeamFactory.php index a71bc52..556db5b 100644 --- a/database/factories/TeamFactory.php +++ b/database/factories/TeamFactory.php @@ -5,6 +5,7 @@ namespace Database\Factories; use App\Models\Team; +use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -24,6 +25,7 @@ public function definition(): array return [ 'name' => $this->faker->company, 'description' => $this->faker->sentence, + 'captain_id' => User::query()->first()->id, ]; } } diff --git a/database/migrations/2024_10_26_081419_create_teams_table.php b/database/migrations/2024_10_26_081419_create_teams_table.php index 79b7899..f002860 100644 --- a/database/migrations/2024_10_26_081419_create_teams_table.php +++ b/database/migrations/2024_10_26_081419_create_teams_table.php @@ -16,6 +16,7 @@ public function up(): void $table->string('name'); $table->string('image')->nullable(); $table->text('description'); + $table->foreignId('captain_id')->constrained('users')->onDelete('cascade'); $table->timestamps(); }); } diff --git a/database/migrations/2024_10_26_081833_create_users_teams_table.php b/database/migrations/2024_10_26_081833_create_users_teams_table.php index d8bd152..c503738 100644 --- a/database/migrations/2024_10_26_081833_create_users_teams_table.php +++ b/database/migrations/2024_10_26_081833_create_users_teams_table.php @@ -15,7 +15,6 @@ public function up(): void $table->id(); $table->foreignId('user_id')->constrained('users')->onDelete('cascade'); $table->foreignId('team_id')->constrained('teams')->onDelete('cascade'); - $table->boolean('is_captain')->default(false); $table->timestamps(); }); }