44 lines
1.2 KiB
PHP
44 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\AddRecipeRequest;
|
|
use App\Repositories\RecipesRepositoryInterface;
|
|
use Illuminate\Http\Request;
|
|
|
|
class RecipesController extends Controller
|
|
{
|
|
private $recipesRepository;
|
|
|
|
public function __construct(RecipesRepositoryInterface $recipesRepository)
|
|
{
|
|
$this->recipesRepository = $recipesRepository;
|
|
}
|
|
|
|
public function add(AddRecipeRequest $request)
|
|
{
|
|
$recipe = $this->recipesRepository->create(collect($request->all()));
|
|
|
|
return response()->json(['success' => true, 'data' => ['recipe' => $recipe]], 200);
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$filters = $this->getFilters($request);
|
|
$recipes = $this->recipesRepository->paginate($filters);
|
|
|
|
return response()->json(['success' => true, 'data' => ['recipes' => $recipes]], 200);
|
|
}
|
|
|
|
public function get(int $id)
|
|
{
|
|
$recipe = $this->recipesRepository->get($id);
|
|
|
|
if(empty($recipe['name'])){
|
|
return response()->json(['success' => true, 'error' => 'Recipe not found'], 404);
|
|
}
|
|
|
|
return response()->json(['success' => true, 'data' => ['recipe' => $recipe]], 200);
|
|
}
|
|
}
|