<?php
namespace Plugin\IkusMobileApi\Controller;
use Doctrine\ORM\NonUniqueResultException;
use Eccube\Controller\AbstractController;
use Exception;
use Plugin\IkusMobileApi\Service\CustomerAddressService;
use Plugin\IkusMobileApi\Service\CustomerFavoriteProductService;
use Plugin\IkusMobileApi\Service\CustomerService;
use Plugin\IkusMobileApi\Util\Paths;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class CustomerController extends AbstractController
{
private CustomerService $customerService;
private CustomerFavoriteProductService $customerFavoriteProductService;
private CustomerAddressService $customerAddressService;
public function __construct(
CustomerService $customerService,
CustomerFavoriteProductService $customerFavoriteProductService,
CustomerAddressService $customerAddressService
)
{
$this->customerService = $customerService;
$this->customerFavoriteProductService = $customerFavoriteProductService;
$this->customerAddressService = $customerAddressService;
}
/**
* Customer API
* @Route("/api/customer", methods={"GET"})
* @param Request $request
* @return JsonResponse
* @throws Exception
*/
public function customer(Request $request): JsonResponse
{
$id = $request->get('id');
$this->customerService->verifyApiKey($request, $id);
$result = $this->customerService->findById($id);
return $this->json($result);
}
/**
* 会員情報更新 API
* @Route("/api/customer", methods={"PUT"})
* @param Request $request
* @return JsonResponse
* @throws Exception
*/
public function updateCustomer(Request $request): JsonResponse
{
$data = json_decode($request->getContent(), true);
$this->customerService->verifyApiKey($request, $data['id']);
$res = $this->customerService->update($data);
return $this->json($res);
}
/**
* お気に入り API
* @Route("/api/customer/favorites", methods={"GET"})
* @param Request $request
* @return JsonResponse
* @throws Exception
*/
public function favorites(Request $request): JsonResponse
{
$customer_id = $request->get('customer_id');
$this->customerService->verifyApiKey($request, $customer_id);
$results = $this->customerFavoriteProductService->findByCustomerId($customer_id);
foreach ($results as &$result) {
$result['min_price'] = intval($result['min_price'] * 1.1);
$result['max_price'] = intval($result['max_price'] * 1.1);
if ($result['image'] !== null) {
$result['image'] = $request->getSchemeAndHttpHost() . Paths::UPLOAD_SAVE_IMAGE . $result['image'];
}
}
unset($result);
return $this->json($results);
}
/**
* お届け先一覧 API
* @Route("/api/customer/addresses", methods={"GET"})
* @param Request $request
* @return JsonResponse
* @throws Exception
*/
public function addresses(Request $request): JsonResponse
{
$customer_id = $request->get('customer_id');
$this->customerService->verifyApiKey($request, $customer_id);
$results = $this->customerAddressService->findByCustomerId($customer_id);
return $this->json($results);
}
/**
* お届け先取得 API
* @Route("/api/customer/address", methods={"GET"})
* @param Request $request
* @return JsonResponse
* @throws NonUniqueResultException
* @throws Exception
*/
public function address(Request $request): JsonResponse
{
$id = $request->get('id');
$customer_id = $request->get('customer_id');
$this->customerService->verifyApiKey($request, $customer_id);
$res = $this->customerAddressService->findById($id);
return $this->json($res);
}
/**
* お届け先登録 API
* @Route("/api/customer/address", methods={"POST"})
* @param Request $request
* @return JsonResponse
* @throws Exception
*/
public function addAddress(Request $request): JsonResponse
{
$data = json_decode($request->getContent(), true);
$this->customerService->verifyApiKey($request, $data['customer_id']);
$res = $this->customerAddressService->create($data);
return $this->json($res);
}
/**
* お届け先更新 API
* @Route("/api/customer/address", methods={"PUT"})
* @param Request $request
* @return JsonResponse
* @throws Exception
*/
public function updateAddress(Request $request): JsonResponse
{
$data = json_decode($request->getContent(), true);
$this->customerService->verifyApiKey($request, $data['customer_id']);
$res = $this->customerAddressService->update($data);
return $this->json($res);
}
/**
* お届け先削除 API
* @Route("/api/customer/address", methods={"DELETE"})
* @param Request $request
* @return JsonResponse
* @throws Exception
*/
public function deleteAddress(Request $request): JsonResponse
{
$id = $request->get('id');
$customer_id = $request->get('customer_id');
$this->customerService->verifyApiKey($request, $customer_id);
$this->customerAddressService->delete($id);
return $this->json([
'result' => true,
]);
}
}