2023-03-09 10:27:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Fo;
|
|
|
|
|
|
2023-03-20 13:17:58 +00:00
|
|
|
use App\Enums\PreparationTimeEnum;
|
2023-03-09 10:27:19 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\Recipe;
|
2023-03-20 13:17:58 +00:00
|
|
|
use Illuminate\Http\Request;
|
2023-03-09 10:27:19 +00:00
|
|
|
|
|
|
|
|
class RecipesController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Return a list of filtered recipes.
|
|
|
|
|
*
|
2023-03-20 13:17:58 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2023-03-09 10:27:19 +00:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
2023-03-20 13:17:58 +00:00
|
|
|
public function index(Request $request)
|
2023-03-09 10:27:19 +00:00
|
|
|
{
|
2023-03-20 13:17:58 +00:00
|
|
|
// Get recipes.
|
|
|
|
|
$filters_activated = $request->filtered_infos;
|
|
|
|
|
|
|
|
|
|
$publishedRecipes = Recipe::when(
|
|
|
|
|
isset($filters_activated),
|
|
|
|
|
function ($query) use ($filters_activated) {
|
|
|
|
|
if ($filters_activated[0] >= 0) {
|
|
|
|
|
$diff_oper = '=';
|
|
|
|
|
} else {
|
|
|
|
|
$diff_oper = '>';
|
|
|
|
|
}
|
|
|
|
|
if ($filters_activated[1] >= 0) {
|
2023-08-28 08:46:47 +00:00
|
|
|
$min_time = PreparationTimeEnum::from($filters_activated[1])->time()['min'];
|
|
|
|
|
$max_time = PreparationTimeEnum::from($filters_activated[1])->time()['max'];
|
2023-03-20 13:17:58 +00:00
|
|
|
} else {
|
|
|
|
|
$min_time = 0;
|
|
|
|
|
$max_time = 9999;
|
|
|
|
|
}
|
|
|
|
|
if ($filters_activated[2] >= 0) {
|
|
|
|
|
$cook_oper = '=';
|
|
|
|
|
} else {
|
|
|
|
|
$cook_oper = '>';
|
|
|
|
|
}
|
|
|
|
|
$query->where('difficulty', $diff_oper, $filters_activated[0])
|
|
|
|
|
->whereBetween('preparation_time', [$min_time, $max_time])
|
|
|
|
|
->where('cooking_mode', $cook_oper, $filters_activated[2]);
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
->where('status', true)
|
|
|
|
|
->orderBy('created_at')
|
|
|
|
|
->paginate(9);
|
2023-03-09 10:27:19 +00:00
|
|
|
|
2023-03-20 13:17:58 +00:00
|
|
|
return view('fo.pages.recipes.index', compact('publishedRecipes', 'filters_activated'));
|
2023-03-09 10:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a view with a specific model.
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\Recipe $publishedRecipe
|
|
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
|
*/
|
|
|
|
|
public function show(Recipe $publishedRecipe)
|
|
|
|
|
{
|
2023-06-07 12:40:53 +00:00
|
|
|
$products = $publishedRecipe->products()->get();
|
|
|
|
|
$relatedRecipes = $publishedRecipe->recipes()->where('status', true)->get();
|
2023-03-20 13:17:58 +00:00
|
|
|
|
2023-03-10 13:02:48 +00:00
|
|
|
// 3 Recettes.
|
|
|
|
|
$publishedRecipes = Recipe::where('status', true)
|
2023-03-10 15:33:15 +00:00
|
|
|
->where('id', '!=', $publishedRecipe->id)
|
2023-03-10 13:02:48 +00:00
|
|
|
->take(3)
|
|
|
|
|
->get();
|
|
|
|
|
|
2023-03-20 13:17:58 +00:00
|
|
|
$recipeModel = $publishedRecipe;
|
2023-03-10 15:33:15 +00:00
|
|
|
|
2023-06-07 12:40:53 +00:00
|
|
|
return view(
|
|
|
|
|
'fo.pages.recipes.recipe',
|
|
|
|
|
compact('recipeModel', 'products', 'relatedRecipes')
|
|
|
|
|
);
|
2023-03-09 10:27:19 +00:00
|
|
|
}
|
|
|
|
|
}
|