Skip to content

Commit

Permalink
added captain and challenges to teams in adminpanel
Browse files Browse the repository at this point in the history
  • Loading branch information
romo4ko committed Oct 26, 2024
1 parent b9281b3 commit 7c9dcc3
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 5 deletions.
20 changes: 20 additions & 0 deletions app/Filament/Resources/TeamResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Challenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
33 changes: 32 additions & 1 deletion app/Models/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
}

}
6 changes: 3 additions & 3 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),

/*
|--------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions database/factories/TeamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Database\Factories;

use App\Models\Team;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
Expand All @@ -24,6 +25,7 @@ public function definition(): array
return [
'name' => $this->faker->company,
'description' => $this->faker->sentence,
'captain_id' => User::query()->first()->id,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down

0 comments on commit 7c9dcc3

Please sign in to comment.