forked from eatvio/eatvio-infra
make static analise product_nutritional
This commit is contained in:
@@ -4,10 +4,12 @@ namespace App\Livewire\ProductNutritional;
|
||||
|
||||
use App\Models\ProductGroup;
|
||||
use App\Models\ProductNutritionalValue;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Livewire\Features\SupportRedirects\Redirector;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
|
||||
class Create extends Component
|
||||
{
|
||||
@@ -15,56 +17,56 @@ class Create extends Component
|
||||
|
||||
public string $name = '';
|
||||
|
||||
public ProductGroup|null $product_group = null;
|
||||
public int|string $product_group;
|
||||
|
||||
public UploadedFile|null $image = null;
|
||||
|
||||
public string $description = '';
|
||||
|
||||
public $formulations;
|
||||
public string $formulations;
|
||||
|
||||
public $energy_on_100gr;
|
||||
public int $energy_on_100gr;
|
||||
|
||||
public $proteins_on_100gr;
|
||||
public float $proteins_on_100gr;
|
||||
|
||||
public $fats_on_100gr;
|
||||
public float $fats_on_100gr;
|
||||
|
||||
public $carbohydrates_on_100gr;
|
||||
public float $carbohydrates_on_100gr;
|
||||
|
||||
public $portion_mass_gr;
|
||||
public int $portion_mass_gr = 0;
|
||||
|
||||
public $energy_on_portion;
|
||||
public float $energy_on_portion;
|
||||
|
||||
public $proteins_on_portion;
|
||||
public float $proteins_on_portion;
|
||||
|
||||
public $fats_on_portion;
|
||||
public float $fats_on_portion;
|
||||
|
||||
public $carbohydrates_on_portion;
|
||||
public float $carbohydrates_on_portion;
|
||||
|
||||
public $curProductNutritionalValue;
|
||||
public ProductNutritionalValue $curProductNutritionalValue;
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.product-nutritional.create', ['productGroups' => ProductGroup::all()]);
|
||||
}
|
||||
|
||||
public function save()
|
||||
public function save(): Redirector|RedirectResponse
|
||||
{
|
||||
$productNutritionalVal = ProductNutritionalValue::create([
|
||||
'name' => $this->name,
|
||||
'product_group_id' => $this->product_group,
|
||||
'image' => is_null($this->image) ? '' : $this->image->store('product_nutritional', 'public'),
|
||||
'image' => is_null($this->image) ? '' : $this->image->store('product_nutritional'),
|
||||
'description' => $this->description ?? '',
|
||||
'formulations' => $this->formulations ?? '',
|
||||
'energy_on_100gr' => (int) $this->energy_on_100gr,
|
||||
'proteins_on_100gr' => (float) $this->proteins_on_100gr,
|
||||
'fats_on_100gr' => (float) $this->fats_on_100gr,
|
||||
'carbohydrates_on_100gr' => (float) $this->carbohydrates_on_100gr,
|
||||
'portion_mass_gr' => (int) $this->portion_mass_gr,
|
||||
'energy_on_portion' => (float) $this->energy_on_portion,
|
||||
'proteins_on_portion' => (float) $this->proteins_on_portion,
|
||||
'fats_on_portion' => (float) $this->fats_on_portion,
|
||||
'carbohydrates_on_portion' => (float) $this->carbohydrates_on_portion,
|
||||
'energy_on_100gr' => $this->energy_on_100gr,
|
||||
'proteins_on_100gr' => $this->proteins_on_100gr,
|
||||
'fats_on_100gr' => $this->fats_on_100gr,
|
||||
'carbohydrates_on_100gr' => $this->carbohydrates_on_100gr,
|
||||
'portion_mass_gr' => $this->portion_mass_gr,
|
||||
'energy_on_portion' => $this->energy_on_portion,
|
||||
'proteins_on_portion' => $this->proteins_on_portion,
|
||||
'fats_on_portion' => $this->fats_on_portion,
|
||||
'carbohydrates_on_portion' => $this->carbohydrates_on_portion,
|
||||
]);
|
||||
|
||||
$this->curProductNutritionalValue = $productNutritionalVal;
|
||||
@@ -109,17 +111,17 @@ class Create extends Component
|
||||
}
|
||||
}
|
||||
|
||||
public function updated(string $name, $value): void
|
||||
public function updated(string $name, string $value): void
|
||||
{
|
||||
if ($name === 'portion_mass_gr') {
|
||||
$this->recalulatePortionVals((int) $this->portion_mass_gr);
|
||||
}
|
||||
|
||||
if (
|
||||
(int) $this->portion_mass_gr > 0
|
||||
&& in_array($name, ['energy_on_portion', 'proteins_on_portion', 'fats_on_portion', 'carbohydrates_on_portion'])
|
||||
$this->portion_mass_gr > 0 &&
|
||||
in_array($name, ['energy_on_portion', 'proteins_on_portion', 'fats_on_portion', 'carbohydrates_on_portion'])
|
||||
) {
|
||||
$this->recalulateVals((int) $this->portion_mass_gr);
|
||||
$this->recalulateVals($this->portion_mass_gr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,42 +6,46 @@ use App\Models\ProductGroup;
|
||||
use App\Models\ProductNutritionalValue;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithFileUploads;
|
||||
use Illuminate\View\View;
|
||||
use Livewire\Features\SupportRedirects\Redirector;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
|
||||
class Edit extends Component
|
||||
{
|
||||
use WithFileUploads;
|
||||
|
||||
public $name;
|
||||
public string $name = '';
|
||||
|
||||
public $product_group;
|
||||
public int|string $product_group;
|
||||
|
||||
public $image;
|
||||
public UploadedFile|string $image = '';
|
||||
|
||||
public $description;
|
||||
public string $description = '';
|
||||
|
||||
public $formulations;
|
||||
public string $formulations;
|
||||
|
||||
public $energy_on_100gr;
|
||||
public int $energy_on_100gr;
|
||||
|
||||
public $proteins_on_100gr;
|
||||
public float $proteins_on_100gr;
|
||||
|
||||
public $fats_on_100gr;
|
||||
public float $fats_on_100gr;
|
||||
|
||||
public $carbohydrates_on_100gr;
|
||||
public float $carbohydrates_on_100gr;
|
||||
|
||||
public $portion_mass_gr;
|
||||
public int $portion_mass_gr = 0;
|
||||
|
||||
public $energy_on_portion;
|
||||
public float $energy_on_portion;
|
||||
|
||||
public $proteins_on_portion;
|
||||
public float $proteins_on_portion;
|
||||
|
||||
public $fats_on_portion;
|
||||
public float $fats_on_portion;
|
||||
|
||||
public $carbohydrates_on_portion;
|
||||
public float $carbohydrates_on_portion;
|
||||
|
||||
public $product;
|
||||
public ProductNutritionalValue $product;
|
||||
|
||||
public function mount(ProductNutritionalValue $product)
|
||||
public function mount(ProductNutritionalValue $product): void
|
||||
{
|
||||
$this->name = $product->name;
|
||||
$this->product_group = $product->product_group->id;
|
||||
@@ -59,15 +63,15 @@ class Edit extends Component
|
||||
$this->carbohydrates_on_portion = $product->carbohydrates_on_portion;
|
||||
}
|
||||
|
||||
public function render()
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.product-nutritional.edit', ['productGroups' => ProductGroup::all()]);
|
||||
}
|
||||
|
||||
public function save()
|
||||
public function save(): RedirectResponse|Redirector
|
||||
{
|
||||
if (! is_string($this->image)) {
|
||||
$this->product->image = $this->image->store('recipe', 'public');
|
||||
$this->product->image = $this->image->store('product_nutritional');
|
||||
}
|
||||
|
||||
$this->product->fill([
|
||||
@@ -91,7 +95,7 @@ class Edit extends Component
|
||||
return redirect()->route('product_nutritional_value.index');
|
||||
}
|
||||
|
||||
public function recalulateVals($mass)
|
||||
public function recalulateVals(int $mass): void
|
||||
{
|
||||
$productKeys = [
|
||||
['energy_on_100gr', 'energy_on_portion'],
|
||||
@@ -110,7 +114,7 @@ class Edit extends Component
|
||||
}
|
||||
}
|
||||
|
||||
public function recalulatePortionVals($mass)
|
||||
public function recalulatePortionVals(int $mass): void
|
||||
{
|
||||
$productKeys = [
|
||||
['energy_on_100gr', 'energy_on_portion'],
|
||||
@@ -128,7 +132,7 @@ class Edit extends Component
|
||||
}
|
||||
}
|
||||
|
||||
public function updated($name, $value)
|
||||
public function updated(string $name, string $value): void
|
||||
{
|
||||
if ($name === 'portion_mass_gr') {
|
||||
$this->recalulatePortionVals((int) $this->portion_mass_gr);
|
||||
@@ -142,7 +146,7 @@ class Edit extends Component
|
||||
}
|
||||
}
|
||||
|
||||
public function delete()
|
||||
public function delete(): RedirectResponse|Redirector
|
||||
{
|
||||
$this->product->delete();
|
||||
|
||||
|
||||
@@ -4,10 +4,11 @@ namespace App\Livewire\ProductNutritional;
|
||||
|
||||
use App\Models\ProductNutritionalValue;
|
||||
use Livewire\Component;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class Index extends Component
|
||||
{
|
||||
public function render()
|
||||
public function render(): View
|
||||
{
|
||||
return view('livewire.product-nutritional.index', ['products' => ProductNutritionalValue::all()]);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
* App\Models\ProductGroup
|
||||
@@ -34,7 +35,7 @@ class ProductGroup extends Model
|
||||
|
||||
protected $fillable = ['name'];
|
||||
|
||||
public function product_nutritional_value()
|
||||
public function product_nutritional_value(): HasMany
|
||||
{
|
||||
return $this->hasMany(ProductNutritionalValue::class);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* App\Models\ProductNutritionalValue
|
||||
@@ -57,7 +58,7 @@ class ProductNutritionalValue extends Model
|
||||
|
||||
protected $guarded = [];
|
||||
|
||||
public function product_group()
|
||||
public function product_group(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ProductGroup::class);
|
||||
}
|
||||
|
||||
@@ -4,10 +4,8 @@ namespace Database\Factories;
|
||||
|
||||
use App\Models\BlogPost;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Models\User;
|
||||
use Faker\Provider\Youtube;
|
||||
use Faker\Generator;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user