[CLEAR-45] returning recipes

This commit is contained in:
Artur Nowakowski 2020-01-06 17:14:54 +01:00
parent 1b59a248ff
commit 4bc8a58534
5 changed files with 47 additions and 9 deletions

View File

@ -0,0 +1,25 @@
<?php
namespace App\Helpers;
class NutritionCalculator
{
public static function calculateNutritionForRecipe(object $ingredients)
{
$nutrition = [
'kcal' => 0,
'carbohydrates' => 0,
'protein' => 0,
'fat' => 0,
];
foreach($ingredients as $ingredient)
{
foreach(array_keys($nutrition) as $nutritionValue) {
$nutrition[$nutritionValue] += $ingredient['weight'] / 100 * $ingredient['product'][$nutritionValue];
}
}
return $nutrition;
}
}

View File

@ -1,9 +1,9 @@
<?php
namespace App;
namespace App\Models;
use App\Models\Product;
use Illuminate\Database\Eloquent\Model;
use App\Helpers\NutritionCalculator;
class Recipe extends Model
{
@ -13,6 +13,8 @@ class Recipe extends Model
protected $hidden = ['created_at', 'updated_at'];
protected $appends = ['nutrition'];
public function steps()
{
return $this->hasMany(RecipeStep::class);
@ -22,4 +24,9 @@ class Recipe extends Model
{
return $this->hasMany(RecipeProduct::class);
}
public function getNutritionAttribute()
{
return NutritionCalculator::calculateNutritionForRecipe($this->ingredients);
}
}

View File

@ -1,8 +1,7 @@
<?php
namespace App;
namespace App\Models;
use App\Models\Product;
use Illuminate\Database\Eloquent\Model;
use App\Traits\HasCompositeKey;

View File

@ -1,6 +1,6 @@
<?php
namespace App;
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

View File

@ -4,9 +4,9 @@
namespace App\Repositories;
use App\Recipe;
use App\RecipeProduct;
use App\RecipeStep;
use App\Models\Recipe;
use App\Models\RecipeProduct;
use App\Models\RecipeStep;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
@ -60,6 +60,13 @@ class RecipesRepository implements RecipesRepositoryInterface
public function paginate(array $filters)
{
return Recipe::paginate($filters['limit']);
$query = Recipe::query();
if(isset($filters['search'])){
$search = ucfirst($filters['search']);
$query->where('name', 'like', "%${search}%");
}
return $query->paginate($filters['limit']);
}
}