Skip to content
Snippets Groups Projects
Commit 072c1b5c authored by Jeremy Guiselin's avatar Jeremy Guiselin
Browse files

save purchase

parent e35975ca
No related branches found
No related tags found
No related merge requests found
...@@ -153,7 +153,28 @@ class DefaultController extends FOSRestController ...@@ -153,7 +153,28 @@ class DefaultController extends FOSRestController
} }
$view->setStatusCode(500); $view->setStatusCode(400);
return $this->handleView($view);
}
public function savePurchaseAction(Request $request)
{
$view = View::create();
if ($request->getMethod() === 'POST') {
$content = (array)\GuzzleHttp\json_decode($request->getContent());
$deviceRepository = $this->getDeviceRepository();
$device = $deviceRepository->findOneBy(['uuid' => $content['uuid']]);
$notificationRepository = $this->getLeagueOnDeviceRepository();
try {
$notificationRepository->savePurchase($device, $content['transactionId'], $content['leagues']);
} catch (\Exception $e) {
$view->setStatusCode(400);
$this->getLogger()->err($e->getMessage());
}
}
$view->setStatusCode(403);
return $this->handleView($view); return $this->handleView($view);
} }
...@@ -230,6 +251,11 @@ class DefaultController extends FOSRestController ...@@ -230,6 +251,11 @@ class DefaultController extends FOSRestController
return $this->getDoctrine()->getRepository('BackendBundle:League'); return $this->getDoctrine()->getRepository('BackendBundle:League');
} }
private function getLeagueOnDeviceRepository()
{
return $this->getDoctrine()->getRepository('BackendBundle:LeagueOnDevice');
}
private function getLogger() private function getLogger()
{ {
return $this->get('logger'); return $this->get('logger');
......
...@@ -9,9 +9,50 @@ ...@@ -9,9 +9,50 @@
namespace BackendBundle\Repository; namespace BackendBundle\Repository;
use BackendBundle\Entity\Device;
use BackendBundle\Entity\LeagueOnDevice;
use BackendBundle\Entity\Transaction;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
class LeagueOnDeviceRepository extends EntityRepository class LeagueOnDeviceRepository extends EntityRepository
{ {
public function savePurchase(Device $device, string $transactionId, array $leagues)
{
$em = $this->getEntityManager();
$qb = $this->getEntityManager()->getRepository('BackendBundle:League')->createQueryBuilder('l');
$ids = array_map(function ($el) {
return $el['id'];
}, $leagues);
$qb
->select('l')
->where('l.id IN (:ids)')
->setParameter('ids', $ids);
$leagues = $qb->getQuery()->getResult();
foreach ($leagues as $league) {
$lod = $this->findOneBy([
'device' => $device,
'league' => $league
]);
if ($lod) {
$lod->setStatus(LeagueOnDevice::STATUS_UNLOCK);
} else {
$lod = new LeagueOnDevice();
$lod->setDevice($device);
$lod->setLeague($league);
$em->persist($lod);
}
}
$transaction = new Transaction();
$transaction->setStoreTransactionId($transactionId);
$transaction->setDate(new \DateTime());
$transaction->setDevice($device);
$em->persist($transaction);
$em->flush();
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment