Skip to content

Commit

Permalink
Delete action done, ready for testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCelavi committed May 24, 2017
1 parent aa9a866 commit b91f6a9
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 9 deletions.
Expand Up @@ -30,6 +30,7 @@ class CreateController extends Controller
* Main controller action
*
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
Expand Down Expand Up @@ -105,10 +106,11 @@ protected function getForm()
/**
* Save rate.
*
* @param ExchangeRate $rate
* @param RateInterface $rate
*
* @return TRUE if successful.
*/
protected function save(ExchangeRate $rate)
protected function save(RateInterface $rate)
{
try {
$this->get('runopencode.exchange_rate.repository')->save([$rate]);
Expand Down
100 changes: 100 additions & 0 deletions src/RunOpenCode/Bundle/ExchangeRate/Controller/DeleteController.php
Expand Up @@ -9,16 +9,116 @@
*/
namespace RunOpenCode\Bundle\ExchangeRate\Controller;

use RunOpenCode\Bundle\ExchangeRate\Security\AccessVoter;
use RunOpenCode\ExchangeRate\Contract\RateInterface;
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;

/**
* Class DeleteController
*
* @package RunOpenCode\Bundle\ExchangeRate\Controller
*/
class DeleteController extends Controller
{
/**
* Main controller action
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
{
return $this->render('', [
'rate' => $this->getRateFromRequest($request)
]);
}

/**
* Execute delete action.
*
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function deleteAction(Request $request)
{
if (!$this->isCsrfTokenValid($request->getRequestUri(), $request->get('_csrf_token'))) {
throw new InvalidCsrfTokenException();
}

/**
* @var \RunOpenCode\ExchangeRate\Contract\RateInterface $rate
*/
$rate = $this->getRateFromRequest($request);

if (!$this->delete($rate)) {
return $this->indexAction($request);
}

return $this->redirectAfterSuccess();
}

/**
* Get rate from request
*
* @param Request $request
*
* @return \RunOpenCode\ExchangeRate\Contract\RateInterface
*/
protected function getRateFromRequest(Request $request)
{
$source = $request->get('source');
$rateType = $request->get('rate_type');
$currencyCode = $request->get('currency_code');
$date = \DateTime::createFromFormat('Y-m-d', $request->get('date'));

/**
* @var RepositoryInterface $repository
*/
$repository = $this->get('runopencode.exchange_rate.repository');

if (!$repository->has($source, $currencyCode, $date, $rateType)) {
throw $this->createNotFoundException();
}

$rate = $repository->get($source, $currencyCode, $date, $rateType);

if (!$this->isGranted(AccessVoter::DELETE, $rate)) {
throw $this->createAccessDeniedException();
}

return $rate;
}

/**
* Save rate.
*
* @param RateInterface $rate
*
* @return TRUE if successful.
*/
protected function delete(RateInterface $rate)
{
try {
$this->get('runopencode.exchange_rate.repository')->delete([$rate]);
$this->addFlash('success', $this->get('translator')->trans('flash.delete.success', [], 'runopencode_exchange_rate'));
return true;
} catch (\Exception $e) {
$this->addFlash('error', $this->get('translator')->trans('flash.delete.error.unknown', [], 'runopencode_exchange_rate'));
return false;
}
}

/**
* Redirect after success.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
protected function redirectAfterSuccess()
{
return $this->redirectToRoute('runopencode_exchange_rate_list');
}
}
Expand Up @@ -30,10 +30,7 @@ class EditController extends Controller
* Main controller action
*
* @param Request $request
* @param $source
* @param $rateType
* @param $currencyCode
* @param \DateTime $date
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|\Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function indexAction(Request $request)
Expand Down Expand Up @@ -75,7 +72,7 @@ public function indexAction(Request $request)
*
* @param Form $form
* @param Request $request
* @param RateInterface $rate
* @param RateInterface $exchangeRate
*
* @return bool TRUE if successful
*/
Expand Down Expand Up @@ -123,10 +120,10 @@ protected function getForm(RateInterface $rate)
/**
* Save rate.
*
* @param Rate $rate
* @param RateInterface $rate
* @return TRUE if successful.
*/
protected function save(Rate $rate)
protected function save(RateInterface $rate)
{
try {
$this->get('runopencode.exchange_rate.repository')->save([$rate]);
Expand Down
Expand Up @@ -23,6 +23,13 @@
*/
class ListController extends Controller
{
/**
* Main controller action.
*
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
{
if (!$this->isGranted(AccessVoter::VIEW, RateInterface::class)) {
Expand Down
Expand Up @@ -19,4 +19,8 @@
<route id="runopencode_exchange_rate_delete" path="/exchange-rates/delete/{source}/{rate_type}/{currency_code}/{date}">
<default key="_controller">RunOpenCode\Bundle\ExchangeRate\Controller\DeleteController::indexAction</default>
</route>

<route id="runopencode_exchange_rate_execute_delete" path="/exchange-rates/execute/delete/{source}/{rate_type}/{currency_code}/{date}">
<default key="_controller">RunOpenCode\Bundle\ExchangeRate\Controller\DeleteController::deleteAction</default>
</route>
</routes>
@@ -1,6 +1,10 @@
list.title: Exchange rates
edit.title: Edit exchange rate
create.title: Create new exchange rate
delete:
title: Delete exchange rate
confirm: Are you sure that you want to delete this exchange rate?

table:
heading:
source: Source
Expand Down Expand Up @@ -54,6 +58,9 @@ flash:
edit:
success: You have successfully modified exchange rate.
error.unknown: Could not save exchange rate for unknown reason. Contact administrator.
delete:
success: You have successfully deleted exchange rate.
error.unknown: Could not delete exchange rate for unknown reason. Contact administrator.

validator:
rate.invalid: Unknown rate provided ({{ rate_type }}, {{ currency_code }}, {{ source_name }}).
Expand Down
@@ -0,0 +1,68 @@
{% extends '@ExchangeRate/layout.html.twig' %}

{% block title %}{{ 'delete.title' | trans({}, 'runopencode_exchange_rate') }}{% endblock title %}

{% block body %}

{% block form %}

{% block advisory %}

{{ 'delete.confirm' | trans({}, 'runopencode_exchange_rate') }}

{% endblock advisory %}

{% block rate %}
<ul>
<li>{{ 'table.heading.source' | trans({}, 'runopencode_exchange_rate') }}: {{ rate.sourceName }}</li>
<li>{{ 'table.heading.rateType' | trans({}, 'runopencode_exchange_rate') }}: {{ rate.rateType }}</li>
<li>{{ 'table.heading.currencyCode' | trans({}, 'runopencode_exchange_rate') }}: {{ rate.currencyCode }}</li>
<li>{{ 'table.heading.date' | trans({}, 'runopencode_exchange_rate') }}: {{ rate.date | date }}</li>
<li>{{ 'table.heading.value' | trans({}, 'runopencode_exchange_rate') }}: {{ rate.value }}</li>
</ul>
{% endblock rate %}

{% endblock form %}

{% block generic_action_delete %}

<a
href="{{ path('runopencode_exchange_rate_execute_delete', {
date: rate.date|date('Y-m-d'),
currency_code: rate.currencyCode,
rate_type: rate.rateType,
source: rate.sourceName,
_csrf_token: csrf_token(path('runopencode_exchange_rate_execute_delete')) }) }}"
>
{{ 'actions.generic.delete' | trans({}, 'runopencode_exchange_rate') }}
</a>

{% endblock generic_action_delete %}

{% if is_granted('VIEW', '\\RunOpenCode\\ExchangeRate\\Contract\\RateInterface') %}

{% block generic_action_list %}

<a href="{{ path('runopencode_exchange_rate_list') }}">
{{ 'actions.generic.list' | trans({}, 'runopencode_exchange_rate') }}
</a>

{% endblock generic_action_list %}

{% endif %}

{% block generic_action_edit %}

<a href="{{ path('runopencode_exchange_rate_edit', {
date: rate.date|date('Y-m-d'),
currency_code: rate.currencyCode,
rate_type: rate.rateType,
source: rate.sourceName
}) }}">
{{ 'actions.generic.edit' | trans({}, 'runopencode_exchange_rate') }}
</a>

{% endblock generic_action_edit %}

{% endblock body %}

0 comments on commit b91f6a9

Please sign in to comment.