prod #7

Merged
klllst merged 40 commits from feature/task-7 into master 2024-06-16 12:25:52 +04:00
7 changed files with 174 additions and 7 deletions
Showing only changes of commit ba38b9b1d8 - Show all commits

View File

@ -0,0 +1,23 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Grade>
*/
class GradeFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => 'Класс' . fake()->unique()->numberBetween(1,100)
];
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace Database\Factories;
use App\Enums\TypeLesson;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Lesson>
*/
class LessonFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$typeLessonCases = TypeLesson::cases();
return [
'name' => 'Занятие' . fake()->unique()->numberBetween(),
'description' => fake()->text(200),
'type' => $typeLessonCases[array_rand($typeLessonCases)],
'lesson_date' => fake()->date(),
];
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Student>
*/
class StudentFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$faker = \Faker\Factory::create('ru_RU');
return [
'name' => $faker->firstNameMale,
'last_name' => $faker->lastNameMale,
'middle_name' => $faker->middleNameMale,
'birthday' => $faker->date(),
];
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Subject>
*/
class SubjectFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => 'Предмет' . fake()->unique()->numberBetween(1, 100)
];
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Teacher>
*/
class TeacherFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$faker = \Faker\Factory::create('ru_RU');
return [
'name' => $faker->firstNameMale,
'last_name' => $faker->lastNameMale,
'middle_name' => $faker->middleNameMale,
'birthday' => $faker->date(),
];
}
}

View File

@ -24,7 +24,6 @@ class UserFactory extends Factory
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),

View File

@ -2,8 +2,13 @@
namespace Database\Seeders;
use App\Enums\ScoreEnum;
use App\Models\Grade;
use App\Models\Lesson;
use App\Models\Student;
use App\Models\Subject;
use App\Models\Teacher;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
@ -13,11 +18,43 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// User::factory(10)->create();
$teachers = Teacher::factory(15)->create();
$grades = Grade::factory(10)->create();
$subjects = Subject::factory(10)->create();
$scores = ScoreEnum::cases();
User::factory()->create([
'name' => 'Test User',
'email' => 'test@example.com',
]);
$teachers->each(function ($teacher) {
$user = User::factory()->create();
$teacher->user()->save($user);
});
$grades->each(function ($grade) use ($subjects, $teachers, $scores){
$grade->subjects()->sync($subjects);
$grade->teachers()->sync($teachers);
$students = Student::factory(10)->create([
'grade_id' => $grade->id,
])->each(function ($student) use ($scores) {
$user = User::factory()->create();
$student->user()->save($user);
});
$lessons = Lesson::factory(30)->create([
'grade_id' => $grade->id,
'subject_id' => $grade->subjects->random()->id,
'teacher_id' => $grade->teachers->random()->id,
]);
$students->each(function ($student) use ($lessons, $scores) {
$lessons->each(function ($lesson) use ($student, $scores) {
$student->lessons()
->syncWithoutDetaching([$lesson->id => ['score' => $scores[array_rand($scores)]]]);
});
});
});
$subjects->each(function ($subject) use ($teachers) {
$subject->teachers()->sync($teachers->random(2));
});
}
}