[CLEAR-45] returning recipes
This commit is contained in:
parent
1b59a248ff
commit
4bc8a58534
25
api/app/Helpers/NutrictionCalculator.php
Normal file
25
api/app/Helpers/NutrictionCalculator.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Models\Product;
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use App\Helpers\NutritionCalculator;
|
||||||
|
|
||||||
class Recipe extends Model
|
class Recipe extends Model
|
||||||
{
|
{
|
||||||
@ -13,6 +13,8 @@ class Recipe extends Model
|
|||||||
|
|
||||||
protected $hidden = ['created_at', 'updated_at'];
|
protected $hidden = ['created_at', 'updated_at'];
|
||||||
|
|
||||||
|
protected $appends = ['nutrition'];
|
||||||
|
|
||||||
public function steps()
|
public function steps()
|
||||||
{
|
{
|
||||||
return $this->hasMany(RecipeStep::class);
|
return $this->hasMany(RecipeStep::class);
|
||||||
@ -22,4 +24,9 @@ class Recipe extends Model
|
|||||||
{
|
{
|
||||||
return $this->hasMany(RecipeProduct::class);
|
return $this->hasMany(RecipeProduct::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getNutritionAttribute()
|
||||||
|
{
|
||||||
|
return NutritionCalculator::calculateNutritionForRecipe($this->ingredients);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App;
|
namespace App\Models;
|
||||||
|
|
||||||
use App\Models\Product;
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use App\Traits\HasCompositeKey;
|
use App\Traits\HasCompositeKey;
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App;
|
namespace App\Models;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
@ -4,9 +4,9 @@
|
|||||||
namespace App\Repositories;
|
namespace App\Repositories;
|
||||||
|
|
||||||
|
|
||||||
use App\Recipe;
|
use App\Models\Recipe;
|
||||||
use App\RecipeProduct;
|
use App\Models\RecipeProduct;
|
||||||
use App\RecipeStep;
|
use App\Models\RecipeStep;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
@ -60,6 +60,13 @@ class RecipesRepository implements RecipesRepositoryInterface
|
|||||||
|
|
||||||
public function paginate(array $filters)
|
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']);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user