ClearBowl/api/app/Http/Controllers/ProductsController.php

42 lines
1.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Helpers\NutritionCalculator;
use App\Http\Requests\AddProductRequest;
use App\Repositories\ProductsRepositoryInterface;
use Illuminate\Http\Request;
class ProductsController extends Controller
{
private $productsRepository;
public function __construct(ProductsRepositoryInterface $productsRepository)
{
$this->productsRepository = $productsRepository;
}
public function add(AddProductRequest $request)
{
$product = $this->productsRepository->create($request->all());
return response()->json(['success' => true, 'data' => ['product' => $product]], 200);
}
public function index(Request $request)
{
$filters = $this->getFilters($request);
$products = $this->productsRepository->paginate($filters);
return response()->json(['success' => true, 'data' => ['products' => $products]], 200);
}
public function calculate(Request $request)
{
$products = $this->productsRepository->fillProductsInfo($request->input('products'));
$nutrition = NutritionCalculator::calculateNutritionForProducts($products);
return response()->json(['success' => true, 'data' => ['nutrition' => $nutrition]]);
}
}