:Merge branch 'alex_chevski-recipe-seeders-action'

This commit is contained in:
Anatoliy Poloz
2023-12-22 15:36:28 +03:00
8 changed files with 255 additions and 9 deletions

View File

@@ -6,6 +6,7 @@
"license": "MIT",
"require": {
"php": "^8.0.2",
"aalaap/faker-youtube": "^2.0",
"asantibanez/livewire-charts": "^3.0",
"barryvdh/laravel-debugbar": "^3.6",
"cyrildewit/eloquent-viewable": "^7.0",

51
composer.lock generated
View File

@@ -4,8 +4,57 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "71767f4485e6335b0a9d934fafcfad8e",
"content-hash": "f1504229d84c399a1c8aea12005e3530",
"packages": [
{
"name": "aalaap/faker-youtube",
"version": "2.0.3",
"source": {
"type": "git",
"url": "https://github.com/aalaap/faker-youtube.git",
"reference": "eff2f96ee3f9a845d23140dd1721b14103b5c4d3"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/aalaap/faker-youtube/zipball/eff2f96ee3f9a845d23140dd1721b14103b5c4d3",
"reference": "eff2f96ee3f9a845d23140dd1721b14103b5c4d3",
"shasum": ""
},
"require": {
"fakerphp/faker": "^1.10",
"php": "^7.1 || ^8.0"
},
"require-dev": {
"phpunit/phpunit": "^7.5.20 || ^8.5.8 || ^9.4.2"
},
"type": "library",
"autoload": {
"psr-4": {
"Faker\\Provider\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"WTFPL"
],
"authors": [
{
"name": "Aalaap Ghag",
"email": "aalaap@gmail.com"
}
],
"description": "Faker provider for generating fake YouTube video URLs",
"keywords": [
"faker",
"provider",
"youtube"
],
"support": {
"issues": "https://github.com/aalaap/faker-youtube/issues",
"source": "https://github.com/aalaap/faker-youtube/tree/2.0.3"
},
"time": "2020-11-07T06:43:34+00:00"
},
{
"name": "asantibanez/livewire-charts",
"version": "v3.0.1",

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\PlanComment>
*/
class RecipeCommentFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'grade' => $this->faker->randomDigit(),
'body' => $this->faker->text(),
];
}
}

View File

@@ -0,0 +1,87 @@
<?php
declare(strict_types=1);
namespace Database\Factories;
use Faker\Provider\Youtube;
use FakerRestaurant\Provider\en_US\Restaurant;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Plan>
*/
class RecipeFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
$faker = \Faker\Factory::create();
$this->faker->addProvider(new Restaurant($faker));
$this->faker->addProvider(new Youtube($faker));
return [
'title' => $this->faker->foodName(),
'image' => $this->generateImageFile('recipes'),
'state' => $this->faker->randomElement(['active', 'archived']),
'cooking_time' => $this->faker->numberBetween(0, 59),
'ingredients' => $this->createQuentityIngredients($this->faker->randomDigitNotNull()), // quentity - от 1 до 10 randomDigitNotNull
'type' => $this->faker->randomElement(['first_meals', 'second_meals', 'salads', 'snacks', 'drinks', 'other']),
'description' => $this->faker->text(),
'steps' => $this->createQuentitySteps($this->faker->randomDigitNotNull()),
'video_url' => $this->faker->youtubeRandomUri(),
];
}
private function generateImageFile(string $name)
{
Storage::fake($name);
$file = UploadedFile::fake()->image($name . '.png');
return $file->store($name, 'public');
}
private function generateIngredients(): array
{
return [
'name' => $this->faker->foodName(),
'unit' => $this->faker->randomElement(['гр', 'ч. ложек', 'cт. ложек', 'мл', 'шт', 'зубчик', 'по вкусу']),
'portion' => $this->faker->randomNumber(3, true),
];
}
private function generateSteps(): array
{
return [
'image' => $this->generateImageFile('steps'),
'description' => $this->faker->text(),
];
}
private function createQuentitySteps($count)
{
$result = [];
for ($i = 0; $i < $count; ++$i) {
$result[] = $this->generateSteps();
}
return $result;
}
private function createQuentityIngredients($count)
{
$result = [];
for ($i = 0; $i < $count; ++$i) {
$result[] = $this->generateIngredients();
}
return $result;
}
}

View File

@@ -1,23 +1,21 @@
<?php
declare(strict_types=1);
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
public function run(): void
{
$this->call(PlanSeeder::class);
$this->call(UserSeeder::class);
$this->call(PlanAuthor::class);
$this->call(PlanCommentSeeder::class);
// $this->call(RecipeSeeder::class);
// \App\Models\User::factory(10)->create();
$this->call(RecipeSeeder::class);
$this->call(RecipeAuthor::class);
$this->call(RecipeCommentSeeder::class);
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Database\Seeders;
use App\Models\Recipe;
use App\Models\User;
use Illuminate\Database\Seeder;
class RecipeAuthor extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
if (User::count() > 0 && Recipe::count() > 0) {
Recipe::each(static function ($recipe): void {
User::inRandomOrder()
->first()
->author_of_recipe()
->attach($recipe->id);
});
}
}
}

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Database\Seeders;
use App\Models\Recipe;
use App\Models\RecipeComment;
use App\Models\User;
use Illuminate\Database\Seeder;
class RecipeCommentSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
if (User::count() > 0 && Recipe::count() > 0) {
$comments = RecipeComment::factory()->count(5)->make();
$comments->each(static function ($comment): void {
$comment->author()->associate(
User::inRandomOrder()
->first()
->id
);
$comment->recipe()->associate(
Recipe::inRandomOrder()
->first()
->id
);
$comment->saveOrFail();
});
}
}
}

View File

@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace Database\Seeders;
use App\Models\Recipe;
use Illuminate\Database\Seeder;
class RecipeSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
Recipe::factory()->count(10)->create();
}
}