Skip to content

Commit

Permalink
#79 Add recipe overview an detail page in blade
Browse files Browse the repository at this point in the history
  • Loading branch information
MGHollander committed May 8, 2024
1 parent 01562f5 commit 9d95cda
Show file tree
Hide file tree
Showing 15 changed files with 5,849 additions and 5,399 deletions.
81 changes: 81 additions & 0 deletions app/Http/Controllers/Blade/RecipeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Http\Controllers\Blade;

use App\Http\Controllers\Controller;
use App\Http\Resources\Recipe\IngredientsResource;
use App\Http\Resources\StructuredData\Recipe\IngredientsResource as StructuredDataIngredientsResource;
use App\Http\Resources\StructuredData\Recipe\InstructionsResource;
use App\Http\Traits\FillableAttributes;
use App\Models\Recipe;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;

class RecipeController extends Controller
{
use FillableAttributes;

/**
* Display a listing of the resource.
*
* @return \Illuminate\View\View
*/
public function index(): \Illuminate\View\View
{
return view('blade.recipe.index', [
'recipes' => Recipe::query()
->paginate(12)
->through(fn($recipe) => [
'id' => $recipe->id,
'title' => $recipe->title,
'slug' => $recipe->slug,
'image' => $recipe->getFirstMediaUrl('recipe_image', 'card'),
]),
]);
}

/**
* Display the specified resource.
*
* @param \Illuminate\Http\Request $request
* @param string $slug
* @return \Illuminate\View\View
*/
public function show(Request $request, string $slug): \Illuminate\View\View
{
$recipe = Recipe::findBySlug($slug);

return view('blade.recipe.show', [
'recipe' => [
'id' => $recipe->id,
'title' => $recipe->title,
'slug' => $recipe->slug,
'image' => $recipe->getFirstMediaUrl('recipe_image', 'show'),
'summary' => $recipe->summary,
'tags' => $recipe->tags->pluck('name'),
'servings' => $recipe->servings,
'preparation_minutes' => $recipe->preparation_minutes,
'cooking_minutes' => $recipe->cooking_minutes,
'difficulty' => Str::ucfirst(__('recipes.' . $recipe->difficulty)),
// TODO I think this is not the way to go, but for the experiment it's fine.
'ingredients' => (new IngredientsResource(""))->transformIngredients($recipe->ingredients),
'instructions' => $recipe->instructions,
'source_label' => $recipe->source_label,
'source_link' => $recipe->source_link,
'created_at' => $recipe->created_at,
'structured_data' => [
'description' => strip_tags($recipe->summary),
'ingredients' => new StructuredDataIngredientsResource($recipe->ingredients),
'instructions' => new InstructionsResource($recipe->instructions),
'keywords' => implode(',', $recipe->tags->pluck('name')->toArray()),
],
],
'open_graph' => [
'title' => $recipe->title,
'image' => $recipe->getFirstMediaUrl('recipe_image', 'show'),
'url' => URL::current(),
],
]);
}
}
14 changes: 14 additions & 0 deletions app/View/Components/Blade/Layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\View\Components\Blade;

use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class Layout extends Component
{
public function render(): View
{
return view('blade.layout');
}
}
Loading

0 comments on commit 9d95cda

Please sign in to comment.