first commit
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace App\Dtos;
|
||||
|
||||
class RecipeDto
|
||||
{
|
||||
public ?int $id = null;
|
||||
public ?string $title = null;
|
||||
public ?string $slug = null;
|
||||
public ?string $description = null;
|
||||
public ?int $prepTime = null;
|
||||
public ?int $cookTime = null;
|
||||
public ?int $totalTime = null;
|
||||
public ?int $servings = null;
|
||||
public ?string $difficulty = null;
|
||||
public ?string $cuisine = null;
|
||||
public ?string $category = null;
|
||||
public ?int $calories = null;
|
||||
public ?string $featuredImage = null;
|
||||
public ?string $videoUrl = null;
|
||||
public ?int $status = null;
|
||||
public ?int $viewsCount = null;
|
||||
public ?float $ratingAvg = null;
|
||||
public ?int $ratingCount = null;
|
||||
public ?int $createdAt = null;
|
||||
public ?int $updatedAt = null;
|
||||
|
||||
/** @var RecipeIngredientDto[] */
|
||||
public array $ingredients = [];
|
||||
|
||||
/** @var RecipeStepDto[] */
|
||||
public array $steps = [];
|
||||
|
||||
/** @var RecipeImageDto[] */
|
||||
public array $images = [];
|
||||
|
||||
public ?RecipeNutritionDto $nutrition = null;
|
||||
|
||||
/** @var TagDto[] */
|
||||
public array $tags = [];
|
||||
|
||||
/**
|
||||
* Create DTO from array
|
||||
*
|
||||
* @param array $data
|
||||
* @return static
|
||||
*/
|
||||
public static function fromArray(array $data): static
|
||||
{
|
||||
$dto = new static();
|
||||
|
||||
$dto->id = $data['id'] ?? null;
|
||||
$dto->title = $data['title'] ?? null;
|
||||
$dto->slug = $data['slug'] ?? null;
|
||||
$dto->description = $data['description'] ?? null;
|
||||
$dto->prepTime = isset($data['prepTime']) ? (int)$data['prepTime'] : null;
|
||||
$dto->cookTime = isset($data['cookTime']) ? (int)$data['cookTime'] : null;
|
||||
$dto->totalTime = isset($data['totalTime']) ? (int)$data['totalTime'] : null;
|
||||
$dto->servings = isset($data['servings']) ? (int)$data['servings'] : null;
|
||||
$dto->difficulty = $data['difficulty'] ?? null;
|
||||
$dto->cuisine = $data['cuisine'] ?? null;
|
||||
$dto->category = $data['category'] ?? null;
|
||||
$dto->calories = isset($data['calories']) ? (int)$data['calories'] : null;
|
||||
$dto->featuredImage = $data['featuredImage'] ?? null;
|
||||
$dto->videoUrl = $data['videoUrl'] ?? null;
|
||||
$dto->status = isset($data['status']) ? (int)$data['status'] : null;
|
||||
$dto->viewsCount = isset($data['viewsCount']) ? (int)$data['viewsCount'] : null;
|
||||
$dto->ratingAvg = isset($data['ratingAvg']) ? (float)$data['ratingAvg'] : null;
|
||||
$dto->ratingCount = isset($data['ratingCount']) ? (int)$data['ratingCount'] : null;
|
||||
$dto->createdAt = isset($data['createdAt']) ? (int)$data['createdAt'] : null;
|
||||
$dto->updatedAt = isset($data['updatedAt']) ? (int)$data['updatedAt'] : null;
|
||||
|
||||
// Parse nested DTOs
|
||||
if (isset($data['ingredients']) && is_array($data['ingredients'])) {
|
||||
$dto->ingredients = array_map(
|
||||
fn($item) => RecipeIngredientDto::fromArray($item),
|
||||
$data['ingredients']
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($data['steps']) && is_array($data['steps'])) {
|
||||
$dto->steps = array_map(
|
||||
fn($item) => RecipeStepDto::fromArray($item),
|
||||
$data['steps']
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($data['images']) && is_array($data['images'])) {
|
||||
$dto->images = array_map(
|
||||
fn($item) => RecipeImageDto::fromArray($item),
|
||||
$data['images']
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($data['nutrition']) && is_array($data['nutrition'])) {
|
||||
$dto->nutrition = RecipeNutritionDto::fromArray($data['nutrition']);
|
||||
}
|
||||
|
||||
if (isset($data['tags']) && is_array($data['tags'])) {
|
||||
$dto->tags = array_map(
|
||||
fn($item) => TagDto::fromArray($item),
|
||||
$data['tags']
|
||||
);
|
||||
}
|
||||
|
||||
return $dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert DTO to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'slug' => $this->slug,
|
||||
'description' => $this->description,
|
||||
'prepTime' => $this->prepTime,
|
||||
'cookTime' => $this->cookTime,
|
||||
'totalTime' => $this->totalTime,
|
||||
'servings' => $this->servings,
|
||||
'difficulty' => $this->difficulty,
|
||||
'cuisine' => $this->cuisine,
|
||||
'category' => $this->category,
|
||||
'calories' => $this->calories,
|
||||
'featuredImage' => $this->featuredImage,
|
||||
'videoUrl' => $this->videoUrl,
|
||||
'status' => $this->status,
|
||||
'viewsCount' => $this->viewsCount,
|
||||
'ratingAvg' => $this->ratingAvg,
|
||||
'ratingCount' => $this->ratingCount,
|
||||
'createdAt' => $this->createdAt,
|
||||
'updatedAt' => $this->updatedAt,
|
||||
'ingredients' => array_map(fn($item) => $item->toArray(), $this->ingredients),
|
||||
'steps' => array_map(fn($item) => $item->toArray(), $this->steps),
|
||||
'images' => array_map(fn($item) => $item->toArray(), $this->images),
|
||||
'nutrition' => $this->nutrition?->toArray(),
|
||||
'tags' => array_map(fn($item) => $item->toArray(), $this->tags),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Dtos;
|
||||
|
||||
class RecipeImageDto
|
||||
{
|
||||
public ?int $id = null;
|
||||
public ?string $imagePath = null;
|
||||
public ?string $title = null;
|
||||
public ?string $description = null;
|
||||
public ?int $isFeatured = null;
|
||||
public ?int $sortOrder = null;
|
||||
public ?int $createdAt = null;
|
||||
|
||||
/**
|
||||
* Create DTO from array
|
||||
*
|
||||
* @param array $data
|
||||
* @return static
|
||||
*/
|
||||
public static function fromArray(array $data): static
|
||||
{
|
||||
$dto = new static();
|
||||
|
||||
$dto->id = $data['id'] ?? null;
|
||||
$dto->imagePath = $data['imagePath'] ?? null;
|
||||
$dto->title = $data['title'] ?? null;
|
||||
$dto->description = $data['description'] ?? null;
|
||||
$dto->isFeatured = isset($data['isFeatured']) ? (int)$data['isFeatured'] : null;
|
||||
$dto->sortOrder = isset($data['sortOrder']) ? (int)$data['sortOrder'] : null;
|
||||
$dto->createdAt = isset($data['createdAt']) ? (int)$data['createdAt'] : null;
|
||||
|
||||
return $dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert DTO to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'imagePath' => $this->imagePath,
|
||||
'title' => $this->title,
|
||||
'description' => $this->description,
|
||||
'isFeatured' => $this->isFeatured,
|
||||
'sortOrder' => $this->sortOrder,
|
||||
'createdAt' => $this->createdAt,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Dtos;
|
||||
|
||||
class RecipeIngredientDto
|
||||
{
|
||||
public ?int $id = null;
|
||||
public ?int $ingredientId = null;
|
||||
public ?string $ingredientName = null;
|
||||
public ?string $ingredientSlug = null;
|
||||
public ?string $ingredientCategory = null;
|
||||
public ?string $quantity = null;
|
||||
public ?string $unit = null;
|
||||
public ?string $notes = null;
|
||||
public ?int $sortOrder = null;
|
||||
|
||||
/**
|
||||
* Create DTO from array
|
||||
*
|
||||
* @param array $data
|
||||
* @return static
|
||||
*/
|
||||
public static function fromArray(array $data): static
|
||||
{
|
||||
$dto = new static();
|
||||
|
||||
$dto->id = $data['id'] ?? null;
|
||||
$dto->ingredientId = isset($data['ingredientId']) ? (int)$data['ingredientId'] : null;
|
||||
$dto->ingredientName = $data['ingredientName'] ?? null;
|
||||
$dto->ingredientSlug = $data['ingredientSlug'] ?? null;
|
||||
$dto->ingredientCategory = $data['ingredientCategory'] ?? null;
|
||||
$dto->quantity = $data['quantity'] ?? null;
|
||||
$dto->unit = $data['unit'] ?? null;
|
||||
$dto->notes = $data['notes'] ?? null;
|
||||
$dto->sortOrder = isset($data['sortOrder']) ? (int)$data['sortOrder'] : null;
|
||||
|
||||
return $dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert DTO to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'ingredientId' => $this->ingredientId,
|
||||
'ingredientName' => $this->ingredientName,
|
||||
'ingredientSlug' => $this->ingredientSlug,
|
||||
'ingredientCategory' => $this->ingredientCategory,
|
||||
'quantity' => $this->quantity,
|
||||
'unit' => $this->unit,
|
||||
'notes' => $this->notes,
|
||||
'sortOrder' => $this->sortOrder,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Dtos;
|
||||
|
||||
class RecipeNutritionDto
|
||||
{
|
||||
public ?int $id = null;
|
||||
public ?int $calories = null;
|
||||
public ?float $protein = null;
|
||||
public ?float $carbohydrates = null;
|
||||
public ?float $fat = null;
|
||||
public ?float $saturatedFat = null;
|
||||
public ?float $fiber = null;
|
||||
public ?float $sugar = null;
|
||||
public ?float $sodium = null;
|
||||
public ?float $cholesterol = null;
|
||||
public ?int $createdAt = null;
|
||||
public ?int $updatedAt = null;
|
||||
|
||||
/**
|
||||
* Create DTO from array
|
||||
*
|
||||
* @param array $data
|
||||
* @return static
|
||||
*/
|
||||
public static function fromArray(array $data): static
|
||||
{
|
||||
$dto = new static();
|
||||
|
||||
$dto->id = $data['id'] ?? null;
|
||||
$dto->calories = isset($data['calories']) ? (int)$data['calories'] : null;
|
||||
$dto->protein = isset($data['protein']) ? (float)$data['protein'] : null;
|
||||
$dto->carbohydrates = isset($data['carbohydrates']) ? (float)$data['carbohydrates'] : null;
|
||||
$dto->fat = isset($data['fat']) ? (float)$data['fat'] : null;
|
||||
$dto->saturatedFat = isset($data['saturatedFat']) ? (float)$data['saturatedFat'] : null;
|
||||
$dto->fiber = isset($data['fiber']) ? (float)$data['fiber'] : null;
|
||||
$dto->sugar = isset($data['sugar']) ? (float)$data['sugar'] : null;
|
||||
$dto->sodium = isset($data['sodium']) ? (float)$data['sodium'] : null;
|
||||
$dto->cholesterol = isset($data['cholesterol']) ? (float)$data['cholesterol'] : null;
|
||||
$dto->createdAt = isset($data['createdAt']) ? (int)$data['createdAt'] : null;
|
||||
$dto->updatedAt = isset($data['updatedAt']) ? (int)$data['updatedAt'] : null;
|
||||
|
||||
return $dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert DTO to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'calories' => $this->calories,
|
||||
'protein' => $this->protein,
|
||||
'carbohydrates' => $this->carbohydrates,
|
||||
'fat' => $this->fat,
|
||||
'saturatedFat' => $this->saturatedFat,
|
||||
'fiber' => $this->fiber,
|
||||
'sugar' => $this->sugar,
|
||||
'sodium' => $this->sodium,
|
||||
'cholesterol' => $this->cholesterol,
|
||||
'createdAt' => $this->createdAt,
|
||||
'updatedAt' => $this->updatedAt,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Dtos;
|
||||
|
||||
class RecipeStepDto
|
||||
{
|
||||
public ?int $id = null;
|
||||
public ?int $stepNumber = null;
|
||||
public ?string $title = null;
|
||||
public ?string $instruction = null;
|
||||
public ?string $image = null;
|
||||
public ?int $timeEstimate = null;
|
||||
public ?int $sortOrder = null;
|
||||
|
||||
/**
|
||||
* Create DTO from array
|
||||
*
|
||||
* @param array $data
|
||||
* @return static
|
||||
*/
|
||||
public static function fromArray(array $data): static
|
||||
{
|
||||
$dto = new static();
|
||||
|
||||
$dto->id = $data['id'] ?? null;
|
||||
$dto->stepNumber = isset($data['stepNumber']) ? (int)$data['stepNumber'] : null;
|
||||
$dto->title = $data['title'] ?? null;
|
||||
$dto->instruction = $data['instruction'] ?? null;
|
||||
$dto->image = $data['image'] ?? null;
|
||||
$dto->timeEstimate = isset($data['timeEstimate']) ? (int)$data['timeEstimate'] : null;
|
||||
$dto->sortOrder = isset($data['sortOrder']) ? (int)$data['sortOrder'] : null;
|
||||
|
||||
return $dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert DTO to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'stepNumber' => $this->stepNumber,
|
||||
'title' => $this->title,
|
||||
'instruction' => $this->instruction,
|
||||
'image' => $this->image,
|
||||
'timeEstimate' => $this->timeEstimate,
|
||||
'sortOrder' => $this->sortOrder,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Dtos;
|
||||
|
||||
class TagDto
|
||||
{
|
||||
public ?int $id = null;
|
||||
public ?string $name = null;
|
||||
public ?string $slug = null;
|
||||
public ?int $createdAt = null;
|
||||
|
||||
/**
|
||||
* Create DTO from array
|
||||
*
|
||||
* @param array $data
|
||||
* @return static
|
||||
*/
|
||||
public static function fromArray(array $data): static
|
||||
{
|
||||
$dto = new static();
|
||||
|
||||
$dto->id = $data['id'] ?? null;
|
||||
$dto->name = $data['name'] ?? null;
|
||||
$dto->slug = $data['slug'] ?? null;
|
||||
$dto->createdAt = isset($data['createdAt']) ? (int)$data['createdAt'] : null;
|
||||
|
||||
return $dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert DTO to array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'createdAt' => $this->createdAt,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Dtos\RecipeDto;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\RecipeIngestService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Throwable;
|
||||
|
||||
class RecipeIngestController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
protected RecipeIngestService $ingestService
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Ingest a recipe from external source
|
||||
*
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function ingest(Request $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
// Parse JSON to DTO
|
||||
$recipeDto = RecipeDto::fromArray($request->all());
|
||||
|
||||
// Validate required fields
|
||||
if (empty($recipeDto->slug)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Recipe slug is required',
|
||||
], 422);
|
||||
}
|
||||
|
||||
// Ingest the recipe
|
||||
$recipe = $this->ingestService->ingest($recipeDto);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Recipe ingested successfully',
|
||||
'data' => [
|
||||
'id' => $recipe->id,
|
||||
'slug' => $recipe->slug,
|
||||
'title' => $recipe->title,
|
||||
],
|
||||
], 200);
|
||||
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Recipe ingestion failed', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
'data' => $request->all(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Failed to ingest recipe: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ingest multiple recipes in batch
|
||||
*
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function batchIngest(Request $request): JsonResponse
|
||||
{
|
||||
try {
|
||||
$recipes = $request->input('recipes', []);
|
||||
|
||||
if (empty($recipes) || !is_array($recipes)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Invalid recipes data. Expected array of recipes.',
|
||||
], 422);
|
||||
}
|
||||
|
||||
$results = [
|
||||
'success' => 0,
|
||||
'failed' => 0,
|
||||
'errors' => [],
|
||||
];
|
||||
|
||||
foreach ($recipes as $index => $recipeData) {
|
||||
try {
|
||||
$recipeDto = RecipeDto::fromArray($recipeData);
|
||||
|
||||
if (empty($recipeDto->slug)) {
|
||||
$results['failed']++;
|
||||
$results['errors'][] = [
|
||||
'index' => $index,
|
||||
'error' => 'Recipe slug is required',
|
||||
];
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->ingestService->ingest($recipeDto);
|
||||
$results['success']++;
|
||||
|
||||
} catch (Throwable $e) {
|
||||
$results['failed']++;
|
||||
$results['errors'][] = [
|
||||
'index' => $index,
|
||||
'slug' => $recipeData['slug'] ?? 'unknown',
|
||||
'error' => $e->getMessage(),
|
||||
];
|
||||
|
||||
Log::error('Batch recipe ingestion failed for item', [
|
||||
'index' => $index,
|
||||
'error' => $e->getMessage(),
|
||||
'data' => $recipeData,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => $results['failed'] === 0,
|
||||
'message' => "Processed {$results['success']} recipes successfully, {$results['failed']} failed",
|
||||
'data' => $results,
|
||||
], $results['failed'] > 0 ? 207 : 200);
|
||||
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Batch recipe ingestion failed', [
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Failed to process batch: ' . $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Auth\LoginRequest;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class AuthenticatedSessionController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the login view.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('auth.login');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming authentication request.
|
||||
*/
|
||||
public function store(LoginRequest $request): RedirectResponse
|
||||
{
|
||||
$request->authenticate();
|
||||
|
||||
$request->session()->regenerate();
|
||||
|
||||
return redirect()->intended(route('dashboard', absolute: false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy an authenticated session.
|
||||
*/
|
||||
public function destroy(Request $request): RedirectResponse
|
||||
{
|
||||
Auth::guard('web')->logout();
|
||||
|
||||
$request->session()->invalidate();
|
||||
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
return redirect('/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ConfirmablePasswordController extends Controller
|
||||
{
|
||||
/**
|
||||
* Show the confirm password view.
|
||||
*/
|
||||
public function show(): View
|
||||
{
|
||||
return view('auth.confirm-password');
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm the user's password.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
if (! Auth::guard('web')->validate([
|
||||
'email' => $request->user()->email,
|
||||
'password' => $request->password,
|
||||
])) {
|
||||
throw ValidationException::withMessages([
|
||||
'password' => __('auth.password'),
|
||||
]);
|
||||
}
|
||||
|
||||
$request->session()->put('auth.password_confirmed_at', time());
|
||||
|
||||
return redirect()->intended(route('dashboard', absolute: false));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class EmailVerificationNotificationController extends Controller
|
||||
{
|
||||
/**
|
||||
* Send a new email verification notification.
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
if ($request->user()->hasVerifiedEmail()) {
|
||||
return redirect()->intended(route('dashboard', absolute: false));
|
||||
}
|
||||
|
||||
$request->user()->sendEmailVerificationNotification();
|
||||
|
||||
return back()->with('status', 'verification-link-sent');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class EmailVerificationPromptController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the email verification prompt.
|
||||
*/
|
||||
public function __invoke(Request $request): RedirectResponse|View
|
||||
{
|
||||
return $request->user()->hasVerifiedEmail()
|
||||
? redirect()->intended(route('dashboard', absolute: false))
|
||||
: view('auth.verify-email');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Events\PasswordReset;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Password;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rules;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class NewPasswordController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the password reset view.
|
||||
*/
|
||||
public function create(Request $request): View
|
||||
{
|
||||
return view('auth.reset-password', ['request' => $request]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming new password request.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'token' => ['required'],
|
||||
'email' => ['required', 'email'],
|
||||
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
||||
]);
|
||||
|
||||
// Here we will attempt to reset the user's password. If it is successful we
|
||||
// will update the password on an actual user model and persist it to the
|
||||
// database. Otherwise we will parse the error and return the response.
|
||||
$status = Password::reset(
|
||||
$request->only('email', 'password', 'password_confirmation', 'token'),
|
||||
function (User $user) use ($request) {
|
||||
$user->forceFill([
|
||||
'password' => Hash::make($request->password),
|
||||
'remember_token' => Str::random(60),
|
||||
])->save();
|
||||
|
||||
event(new PasswordReset($user));
|
||||
}
|
||||
);
|
||||
|
||||
// If the password was successfully reset, we will redirect the user back to
|
||||
// the application's home authenticated view. If there is an error we can
|
||||
// redirect them back to where they came from with their error message.
|
||||
return $status == Password::PASSWORD_RESET
|
||||
? redirect()->route('login')->with('status', __($status))
|
||||
: back()->withInput($request->only('email'))
|
||||
->withErrors(['email' => __($status)]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules\Password;
|
||||
|
||||
class PasswordController extends Controller
|
||||
{
|
||||
/**
|
||||
* Update the user's password.
|
||||
*/
|
||||
public function update(Request $request): RedirectResponse
|
||||
{
|
||||
$validated = $request->validateWithBag('updatePassword', [
|
||||
'current_password' => ['required', 'current_password'],
|
||||
'password' => ['required', Password::defaults(), 'confirmed'],
|
||||
]);
|
||||
|
||||
$request->user()->update([
|
||||
'password' => Hash::make($validated['password']),
|
||||
]);
|
||||
|
||||
return back()->with('status', 'password-updated');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Password;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class PasswordResetLinkController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the password reset link request view.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('auth.forgot-password');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming password reset link request.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'email' => ['required', 'email'],
|
||||
]);
|
||||
|
||||
// We will send the password reset link to this user. Once we have attempted
|
||||
// to send the link, we will examine the response then see the message we
|
||||
// need to show to the user. Finally, we'll send out a proper response.
|
||||
$status = Password::sendResetLink(
|
||||
$request->only('email')
|
||||
);
|
||||
|
||||
return $status == Password::RESET_LINK_SENT
|
||||
? back()->with('status', __($status))
|
||||
: back()->withInput($request->only('email'))
|
||||
->withErrors(['email' => __($status)]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Validation\Rules;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class RegisteredUserController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the registration view.
|
||||
*/
|
||||
public function create(): View
|
||||
{
|
||||
return view('auth.register');
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming registration request.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
|
||||
'password' => ['required', 'confirmed', Rules\Password::defaults()],
|
||||
]);
|
||||
|
||||
$user = User::create([
|
||||
'name' => $request->name,
|
||||
'email' => $request->email,
|
||||
'password' => Hash::make($request->password),
|
||||
]);
|
||||
|
||||
event(new Registered($user));
|
||||
|
||||
Auth::login($user);
|
||||
|
||||
return redirect(route('dashboard', absolute: false));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Auth;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Auth\Events\Verified;
|
||||
use Illuminate\Foundation\Auth\EmailVerificationRequest;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
|
||||
class VerifyEmailController extends Controller
|
||||
{
|
||||
/**
|
||||
* Mark the authenticated user's email address as verified.
|
||||
*/
|
||||
public function __invoke(EmailVerificationRequest $request): RedirectResponse
|
||||
{
|
||||
if ($request->user()->hasVerifiedEmail()) {
|
||||
return redirect()->intended(route('dashboard', absolute: false).'?verified=1');
|
||||
}
|
||||
|
||||
if ($request->user()->markEmailAsVerified()) {
|
||||
event(new Verified($request->user()));
|
||||
}
|
||||
|
||||
return redirect()->intended(route('dashboard', absolute: false).'?verified=1');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\ProfileUpdateRequest;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Redirect;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class ProfileController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display the user's profile form.
|
||||
*/
|
||||
public function edit(Request $request): View
|
||||
{
|
||||
return view('profile.edit', [
|
||||
'user' => $request->user(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the user's profile information.
|
||||
*/
|
||||
public function update(ProfileUpdateRequest $request): RedirectResponse
|
||||
{
|
||||
$request->user()->fill($request->validated());
|
||||
|
||||
if ($request->user()->isDirty('email')) {
|
||||
$request->user()->email_verified_at = null;
|
||||
}
|
||||
|
||||
$request->user()->save();
|
||||
|
||||
return Redirect::route('profile.edit')->with('status', 'profile-updated');
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the user's account.
|
||||
*/
|
||||
public function destroy(Request $request): RedirectResponse
|
||||
{
|
||||
$request->validateWithBag('userDeletion', [
|
||||
'password' => ['required', 'current_password'],
|
||||
]);
|
||||
|
||||
$user = $request->user();
|
||||
|
||||
Auth::logout();
|
||||
|
||||
$user->delete();
|
||||
|
||||
$request->session()->invalidate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
return Redirect::to('/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Recipe;
|
||||
use App\Models\RecipeComment;
|
||||
use App\Models\RecipeRating;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RecipeController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Recipe::with(['tags', 'nutrition'])
|
||||
->where('status', 1); // Only published recipes
|
||||
|
||||
// Search filter
|
||||
if ($request->filled('search')) {
|
||||
$search = $request->search;
|
||||
$query->where(function($q) use ($search) {
|
||||
$q->where('title', 'LIKE', "%{$search}%")
|
||||
->orWhere('description', 'LIKE', "%{$search}%");
|
||||
});
|
||||
}
|
||||
|
||||
// Category filter
|
||||
if ($request->filled('category')) {
|
||||
$query->where('category', $request->category);
|
||||
}
|
||||
|
||||
// Cuisine filter
|
||||
if ($request->filled('cuisine')) {
|
||||
$query->where('cuisine', $request->cuisine);
|
||||
}
|
||||
|
||||
// Difficulty filter
|
||||
if ($request->filled('difficulty')) {
|
||||
$query->where('difficulty', $request->difficulty);
|
||||
}
|
||||
|
||||
// Tag filter
|
||||
if ($request->filled('tag')) {
|
||||
$query->whereHas('tags', function($q) use ($request) {
|
||||
$q->where('slug', $request->tag);
|
||||
});
|
||||
}
|
||||
|
||||
// Sorting
|
||||
$sort = $request->get('sort', 'latest');
|
||||
switch ($sort) {
|
||||
case 'popular':
|
||||
$query->orderBy('views_count', 'desc');
|
||||
break;
|
||||
case 'rated':
|
||||
$query->orderBy('rating_avg', 'desc');
|
||||
break;
|
||||
case 'title':
|
||||
$query->orderBy('title', 'asc');
|
||||
break;
|
||||
default:
|
||||
$query->latest();
|
||||
}
|
||||
|
||||
$recipes = $query->paginate(12);
|
||||
|
||||
// Get filter options
|
||||
$categories = Recipe::where('status', 1)
|
||||
->distinct()
|
||||
->pluck('category')
|
||||
->filter()
|
||||
->sort();
|
||||
|
||||
$cuisines = Recipe::where('status', 1)
|
||||
->distinct()
|
||||
->pluck('cuisine')
|
||||
->filter()
|
||||
->sort();
|
||||
|
||||
$difficulties = Recipe::where('status', 1)
|
||||
->distinct()
|
||||
->pluck('difficulty')
|
||||
->filter()
|
||||
->sort();
|
||||
|
||||
return view('recipes.index', compact('recipes', 'categories', 'cuisines', 'difficulties'));
|
||||
}
|
||||
|
||||
public function show(Request $request, string $slug)
|
||||
{
|
||||
$recipe = Recipe::with([
|
||||
'ingredients',
|
||||
'steps',
|
||||
'images',
|
||||
'nutrition',
|
||||
'tags',
|
||||
'comments.user',
|
||||
'ratings'
|
||||
])
|
||||
->where('slug', $slug)
|
||||
->where('status', 1)
|
||||
->firstOrFail();
|
||||
|
||||
// Increment views
|
||||
$recipe->increment('views_count');
|
||||
|
||||
// Get similar recipes (same category or cuisine)
|
||||
$similarRecipes = Recipe::where('id', '!=', $recipe->id)
|
||||
->where('status', 1)
|
||||
->where(function($q) use ($recipe) {
|
||||
$q->where('category', $recipe->category)
|
||||
->orWhere('cuisine', $recipe->cuisine);
|
||||
})
|
||||
->limit(4)
|
||||
->get();
|
||||
|
||||
return view('recipes.show', compact('recipe', 'similarRecipes'));
|
||||
}
|
||||
|
||||
public function toggleFavorite(Request $request, Recipe $recipe)
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
if ($user->favoriteRecipes()->where('recipe_id', $recipe->id)->exists()) {
|
||||
$user->favoriteRecipes()->detach($recipe->id);
|
||||
$isFavorited = false;
|
||||
} else {
|
||||
$user->favoriteRecipes()->attach($recipe->id);
|
||||
$isFavorited = true;
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'is_favorited' => $isFavorited,
|
||||
]);
|
||||
}
|
||||
|
||||
public function rate(Request $request, Recipe $recipe)
|
||||
{
|
||||
$request->validate([
|
||||
'rating' => 'required|integer|min:1|max:5',
|
||||
]);
|
||||
|
||||
$user = $request->user();
|
||||
|
||||
RecipeRating::updateOrCreate(
|
||||
[
|
||||
'user_id' => $user->id,
|
||||
'recipe_id' => $recipe->id,
|
||||
],
|
||||
[
|
||||
'rating' => $request->rating,
|
||||
]
|
||||
);
|
||||
|
||||
// Recalculate average rating
|
||||
$avgRating = $recipe->ratings()->avg('rating');
|
||||
$ratingCount = $recipe->ratings()->count();
|
||||
|
||||
$recipe->update([
|
||||
'rating_avg' => $avgRating,
|
||||
'rating_count' => $ratingCount,
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'rating_avg' => round($avgRating, 2),
|
||||
'rating_count' => $ratingCount,
|
||||
]);
|
||||
}
|
||||
|
||||
public function comment(Request $request, Recipe $recipe)
|
||||
{
|
||||
$request->validate([
|
||||
'comment' => 'required|string|min:3|max:1000',
|
||||
]);
|
||||
|
||||
$comment = RecipeComment::create([
|
||||
'user_id' => $request->user()->id,
|
||||
'recipe_id' => $recipe->id,
|
||||
'comment' => $request->comment,
|
||||
]);
|
||||
|
||||
$comment->load('user');
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'comment' => $comment,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Auth;
|
||||
|
||||
use Illuminate\Auth\Events\Lockout;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'email' => ['required', 'string', 'email'],
|
||||
'password' => ['required', 'string'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to authenticate the request's credentials.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function authenticate(): void
|
||||
{
|
||||
$this->ensureIsNotRateLimited();
|
||||
|
||||
if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
|
||||
RateLimiter::hit($this->throttleKey());
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'email' => trans('auth.failed'),
|
||||
]);
|
||||
}
|
||||
|
||||
RateLimiter::clear($this->throttleKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the login request is not rate limited.
|
||||
*
|
||||
* @throws \Illuminate\Validation\ValidationException
|
||||
*/
|
||||
public function ensureIsNotRateLimited(): void
|
||||
{
|
||||
if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
|
||||
return;
|
||||
}
|
||||
|
||||
event(new Lockout($this));
|
||||
|
||||
$seconds = RateLimiter::availableIn($this->throttleKey());
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'email' => trans('auth.throttle', [
|
||||
'seconds' => $seconds,
|
||||
'minutes' => ceil($seconds / 60),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the rate limiting throttle key for the request.
|
||||
*/
|
||||
public function throttleKey(): string
|
||||
{
|
||||
return Str::transliterate(Str::lower($this->string('email')).'|'.$this->ip());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ProfileUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'email' => [
|
||||
'required',
|
||||
'string',
|
||||
'lowercase',
|
||||
'email',
|
||||
'max:255',
|
||||
Rule::unique(User::class)->ignore($this->user()->id),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Ingredient extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'category',
|
||||
];
|
||||
|
||||
public function recipes(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Recipe::class, 'recipe_ingredient')
|
||||
->withPivot('quantity', 'unit', 'notes', 'sort_order')
|
||||
->withTimestamps();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
|
||||
class Recipe extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'title',
|
||||
'slug',
|
||||
'description',
|
||||
'prep_time',
|
||||
'cook_time',
|
||||
'total_time',
|
||||
'servings',
|
||||
'difficulty',
|
||||
'cuisine',
|
||||
'category',
|
||||
'calories',
|
||||
'featured_image',
|
||||
'video_url',
|
||||
'status',
|
||||
'views_count',
|
||||
'rating_avg',
|
||||
'rating_count',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'prep_time' => 'integer',
|
||||
'cook_time' => 'integer',
|
||||
'total_time' => 'integer',
|
||||
'servings' => 'integer',
|
||||
'calories' => 'integer',
|
||||
'status' => 'integer',
|
||||
'views_count' => 'integer',
|
||||
'rating_avg' => 'decimal:2',
|
||||
'rating_count' => 'integer',
|
||||
];
|
||||
|
||||
public function ingredients(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Ingredient::class, 'recipe_ingredient')
|
||||
->withPivot('quantity', 'unit', 'notes', 'sort_order')
|
||||
->withTimestamps()
|
||||
->orderBy('recipe_ingredient.sort_order');
|
||||
}
|
||||
|
||||
public function steps(): HasMany
|
||||
{
|
||||
return $this->hasMany(RecipeStep::class)->orderBy('sort_order');
|
||||
}
|
||||
|
||||
public function images(): HasMany
|
||||
{
|
||||
return $this->hasMany(RecipeImage::class)->orderBy('sort_order');
|
||||
}
|
||||
|
||||
public function nutrition(): HasOne
|
||||
{
|
||||
return $this->hasOne(RecipeNutrition::class);
|
||||
}
|
||||
|
||||
public function tags(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Tag::class, 'recipe_tag')->withTimestamps();
|
||||
}
|
||||
|
||||
public function ratings(): HasMany
|
||||
{
|
||||
return $this->hasMany(RecipeRating::class);
|
||||
}
|
||||
|
||||
public function comments(): HasMany
|
||||
{
|
||||
return $this->hasMany(RecipeComment::class)->latest();
|
||||
}
|
||||
|
||||
public function favoritedBy(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'user_favorites')->withTimestamps();
|
||||
}
|
||||
|
||||
public function isFavoritedBy(?User $user): bool
|
||||
{
|
||||
if (!$user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->favoritedBy()->where('user_id', $user->id)->exists();
|
||||
}
|
||||
|
||||
public function userRating(?User $user): ?int
|
||||
{
|
||||
if (!$user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->ratings()->where('user_id', $user->id)->value('rating');
|
||||
}
|
||||
|
||||
public function getRecipeImage()
|
||||
{
|
||||
if ($this->featured_image) {
|
||||
return $this->featured_image;
|
||||
}
|
||||
|
||||
if ($this->images()) {
|
||||
return $this->images()->first()->image_path;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecipeComment extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'recipe_id',
|
||||
'comment',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecipeImage extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'recipe_id',
|
||||
'image_path',
|
||||
'title',
|
||||
'description',
|
||||
'is_featured',
|
||||
'sort_order',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'recipe_id' => 'integer',
|
||||
'is_featured' => 'boolean',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
|
||||
public function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecipeNutrition extends Model
|
||||
{
|
||||
protected $table = 'recipe_nutrition';
|
||||
|
||||
protected $fillable = [
|
||||
'recipe_id',
|
||||
'calories',
|
||||
'protein',
|
||||
'carbohydrates',
|
||||
'fat',
|
||||
'saturated_fat',
|
||||
'fiber',
|
||||
'sugar',
|
||||
'sodium',
|
||||
'cholesterol',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'recipe_id' => 'integer',
|
||||
'calories' => 'integer',
|
||||
'protein' => 'decimal:2',
|
||||
'carbohydrates' => 'decimal:2',
|
||||
'fat' => 'decimal:2',
|
||||
'saturated_fat' => 'decimal:2',
|
||||
'fiber' => 'decimal:2',
|
||||
'sugar' => 'decimal:2',
|
||||
'sodium' => 'decimal:2',
|
||||
'cholesterol' => 'decimal:2',
|
||||
];
|
||||
|
||||
public function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecipeRating extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'recipe_id',
|
||||
'rating',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'rating' => 'integer',
|
||||
];
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class RecipeStep extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'recipe_id',
|
||||
'step_number',
|
||||
'title',
|
||||
'instruction',
|
||||
'image',
|
||||
'time_estimate',
|
||||
'sort_order',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'recipe_id' => 'integer',
|
||||
'step_number' => 'integer',
|
||||
'time_estimate' => 'integer',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
|
||||
public function recipe(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Recipe::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Tag extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
];
|
||||
|
||||
public function recipes(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Recipe::class, 'recipe_tag')->withTimestamps();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
public function favoriteRecipes()
|
||||
{
|
||||
return $this->belongsToMany(Recipe::class, 'user_favorites')->withTimestamps();
|
||||
}
|
||||
|
||||
public function ratings()
|
||||
{
|
||||
return $this->hasMany(RecipeRating::class);
|
||||
}
|
||||
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany(RecipeComment::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Dtos\RecipeDto;
|
||||
use App\Models\Ingredient;
|
||||
use App\Models\Recipe;
|
||||
use App\Models\RecipeImage;
|
||||
use App\Models\RecipeNutrition;
|
||||
use App\Models\RecipeStep;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class RecipeIngestService
|
||||
{
|
||||
/**
|
||||
* Ingest a recipe from DTO - create or update
|
||||
*
|
||||
* @param RecipeDto $recipeDto
|
||||
* @return Recipe
|
||||
*/
|
||||
public function ingest(RecipeDto $recipeDto): Recipe
|
||||
{
|
||||
return DB::transaction(function () use ($recipeDto) {
|
||||
// Find existing recipe by slug or create new
|
||||
$recipe = Recipe::where('slug', $recipeDto->slug)->first();
|
||||
|
||||
if ($recipe) {
|
||||
// Update existing recipe
|
||||
$this->updateRecipe($recipe, $recipeDto);
|
||||
} else {
|
||||
// Create new recipe
|
||||
$recipe = $this->createRecipe($recipeDto);
|
||||
}
|
||||
|
||||
// Sync related data
|
||||
$this->syncIngredients($recipe, $recipeDto);
|
||||
$this->syncSteps($recipe, $recipeDto);
|
||||
$this->syncImages($recipe, $recipeDto);
|
||||
$this->syncNutrition($recipe, $recipeDto);
|
||||
$this->syncTags($recipe, $recipeDto);
|
||||
|
||||
return $recipe->load(['ingredients', 'steps', 'images', 'nutrition', 'tags']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new recipe
|
||||
*
|
||||
* @param RecipeDto $dto
|
||||
* @return Recipe
|
||||
*/
|
||||
protected function createRecipe(RecipeDto $dto): Recipe
|
||||
{
|
||||
return Recipe::create([
|
||||
'title' => $dto->title,
|
||||
'slug' => $dto->slug,
|
||||
'description' => $dto->description,
|
||||
'prep_time' => $dto->prepTime,
|
||||
'cook_time' => $dto->cookTime,
|
||||
'total_time' => $dto->totalTime,
|
||||
'servings' => $dto->servings,
|
||||
'difficulty' => $dto->difficulty,
|
||||
'cuisine' => $dto->cuisine,
|
||||
'category' => $dto->category,
|
||||
'calories' => $dto->calories,
|
||||
'featured_image' => $dto->featuredImage,
|
||||
'video_url' => $dto->videoUrl,
|
||||
'status' => $dto->status ?? 0,
|
||||
'views_count' => $dto->viewsCount ?? 0,
|
||||
'rating_avg' => $dto->ratingAvg,
|
||||
'rating_count' => $dto->ratingCount ?? 0,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing recipe
|
||||
*
|
||||
* @param Recipe $recipe
|
||||
* @param RecipeDto $dto
|
||||
* @return void
|
||||
*/
|
||||
protected function updateRecipe(Recipe $recipe, RecipeDto $dto): void
|
||||
{
|
||||
$updates = [];
|
||||
|
||||
// Check each field for changes
|
||||
if ($dto->title !== null && $dto->title !== $recipe->title) {
|
||||
$updates['title'] = $dto->title;
|
||||
}
|
||||
if ($dto->description !== null && $dto->description !== $recipe->description) {
|
||||
$updates['description'] = $dto->description;
|
||||
}
|
||||
if ($dto->prepTime !== null && $dto->prepTime !== $recipe->prep_time) {
|
||||
$updates['prep_time'] = $dto->prepTime;
|
||||
}
|
||||
if ($dto->cookTime !== null && $dto->cookTime !== $recipe->cook_time) {
|
||||
$updates['cook_time'] = $dto->cookTime;
|
||||
}
|
||||
if ($dto->totalTime !== null && $dto->totalTime !== $recipe->total_time) {
|
||||
$updates['total_time'] = $dto->totalTime;
|
||||
}
|
||||
if ($dto->servings !== null && $dto->servings !== $recipe->servings) {
|
||||
$updates['servings'] = $dto->servings;
|
||||
}
|
||||
if ($dto->difficulty !== null && $dto->difficulty !== $recipe->difficulty) {
|
||||
$updates['difficulty'] = $dto->difficulty;
|
||||
}
|
||||
if ($dto->cuisine !== null && $dto->cuisine !== $recipe->cuisine) {
|
||||
$updates['cuisine'] = $dto->cuisine;
|
||||
}
|
||||
if ($dto->category !== null && $dto->category !== $recipe->category) {
|
||||
$updates['category'] = $dto->category;
|
||||
}
|
||||
if ($dto->calories !== null && $dto->calories !== $recipe->calories) {
|
||||
$updates['calories'] = $dto->calories;
|
||||
}
|
||||
if ($dto->featuredImage !== null && $dto->featuredImage !== $recipe->featured_image) {
|
||||
$updates['featured_image'] = $dto->featuredImage;
|
||||
}
|
||||
if ($dto->videoUrl !== null && $dto->videoUrl !== $recipe->video_url) {
|
||||
$updates['video_url'] = $dto->videoUrl;
|
||||
}
|
||||
if ($dto->status !== null && $dto->status !== $recipe->status) {
|
||||
$updates['status'] = $dto->status;
|
||||
}
|
||||
|
||||
if (!empty($updates)) {
|
||||
$recipe->update($updates);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync recipe ingredients
|
||||
*
|
||||
* @param Recipe $recipe
|
||||
* @param RecipeDto $dto
|
||||
* @return void
|
||||
*/
|
||||
protected function syncIngredients(Recipe $recipe, RecipeDto $dto): void
|
||||
{
|
||||
if (empty($dto->ingredients)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$syncData = [];
|
||||
|
||||
foreach ($dto->ingredients as $ingredientDto) {
|
||||
|
||||
$slug = $ingredientDto->ingredientSlug;
|
||||
|
||||
if (!$slug) {
|
||||
$slug = Str::slug($ingredientDto->ingredientName);
|
||||
}
|
||||
|
||||
// Find or create ingredient
|
||||
$ingredient = Ingredient::firstOrCreate(
|
||||
['slug' => $slug],
|
||||
[
|
||||
'name' => $ingredientDto->ingredientName,
|
||||
'category' => $ingredientDto->ingredientCategory,
|
||||
]
|
||||
);
|
||||
|
||||
// Prepare pivot data
|
||||
$syncData[$ingredient->id] = [
|
||||
'quantity' => $ingredientDto->quantity,
|
||||
'unit' => $ingredientDto->unit,
|
||||
'notes' => $ingredientDto->notes,
|
||||
'sort_order' => $ingredientDto->sortOrder ?? 0,
|
||||
];
|
||||
}
|
||||
|
||||
$recipe->ingredients()->sync($syncData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync recipe steps
|
||||
*
|
||||
* @param Recipe $recipe
|
||||
* @param RecipeDto $dto
|
||||
* @return void
|
||||
*/
|
||||
protected function syncSteps(Recipe $recipe, RecipeDto $dto): void
|
||||
{
|
||||
if (empty($dto->steps)) {
|
||||
// Remove all steps if none provided
|
||||
$recipe->steps()->delete();
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete existing steps and recreate
|
||||
$recipe->steps()->delete();
|
||||
|
||||
foreach ($dto->steps as $stepDto) {
|
||||
RecipeStep::create([
|
||||
'recipe_id' => $recipe->id,
|
||||
'step_number' => $stepDto->stepNumber,
|
||||
'title' => $stepDto->title,
|
||||
'instruction' => $stepDto->instruction,
|
||||
'image' => $stepDto->image,
|
||||
'time_estimate' => $stepDto->timeEstimate,
|
||||
'sort_order' => $stepDto->sortOrder ?? 0,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync recipe images
|
||||
*
|
||||
* @param Recipe $recipe
|
||||
* @param RecipeDto $dto
|
||||
* @return void
|
||||
*/
|
||||
protected function syncImages(Recipe $recipe, RecipeDto $dto): void
|
||||
{
|
||||
if (empty($dto->images)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete existing images and recreate
|
||||
$recipe->images()->delete();
|
||||
|
||||
foreach ($dto->images as $imageDto) {
|
||||
RecipeImage::create([
|
||||
'recipe_id' => $recipe->id,
|
||||
'image_path' => $imageDto->imagePath,
|
||||
'title' => $imageDto->title,
|
||||
'description' => $imageDto->description,
|
||||
'is_featured' => $imageDto->isFeatured ?? false,
|
||||
'sort_order' => $imageDto->sortOrder ?? 0,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync recipe nutrition
|
||||
*
|
||||
* @param Recipe $recipe
|
||||
* @param RecipeDto $dto
|
||||
* @return void
|
||||
*/
|
||||
protected function syncNutrition(Recipe $recipe, RecipeDto $dto): void
|
||||
{
|
||||
if ($dto->nutrition === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
RecipeNutrition::updateOrCreate(
|
||||
['recipe_id' => $recipe->id],
|
||||
[
|
||||
'calories' => $dto->nutrition->calories,
|
||||
'protein' => $dto->nutrition->protein,
|
||||
'carbohydrates' => $dto->nutrition->carbohydrates,
|
||||
'fat' => $dto->nutrition->fat,
|
||||
'saturated_fat' => $dto->nutrition->saturatedFat,
|
||||
'fiber' => $dto->nutrition->fiber,
|
||||
'sugar' => $dto->nutrition->sugar,
|
||||
'sodium' => $dto->nutrition->sodium,
|
||||
'cholesterol' => $dto->nutrition->cholesterol,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync recipe tags
|
||||
*
|
||||
* @param Recipe $recipe
|
||||
* @param RecipeDto $dto
|
||||
* @return void
|
||||
*/
|
||||
protected function syncTags(Recipe $recipe, RecipeDto $dto): void
|
||||
{
|
||||
if (empty($dto->tags)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tagIds = [];
|
||||
|
||||
foreach ($dto->tags as $tagDto) {
|
||||
// Find or create tag
|
||||
$tag = Tag::firstOrCreate(
|
||||
['slug' => $tagDto->slug],
|
||||
['name' => $tagDto->name]
|
||||
);
|
||||
|
||||
$tagIds[] = $tag->id;
|
||||
}
|
||||
|
||||
$recipe->tags()->sync($tagIds);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class AppLayout extends Component
|
||||
{
|
||||
/**
|
||||
* Get the view / contents that represents the component.
|
||||
*/
|
||||
public function render(): View
|
||||
{
|
||||
return view('layouts.app');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\View\Components;
|
||||
|
||||
use Illuminate\View\Component;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class GuestLayout extends Component
|
||||
{
|
||||
/**
|
||||
* Get the view / contents that represents the component.
|
||||
*/
|
||||
public function render(): View
|
||||
{
|
||||
return view('layouts.guest');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user