Skip to content

Commit

Permalink
Merge pull request #5 from cocoders/feature/trm24-parser
Browse files Browse the repository at this point in the history
Refresh available bikes functionality
  • Loading branch information
l3l0 committed Nov 19, 2015
2 parents 2010221 + 740db48 commit 525b799
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 28 deletions.
54 changes: 28 additions & 26 deletions app/Resources/views/default/form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</div>
<br>

<a href=""></a>
{% if app.session.flashbag.has('notice') %}
<div class="row">
<div class="col-md-4 col-md-offset-3">
Expand All @@ -43,30 +44,31 @@
{{ form_end(form) }}

<div class="row">
<div class="col-md-6 col-md-offset-3"></div>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Station name</th>
<th>Latitude</th>
<th>Longitude</th>
<th>Available bikes</th>
</tr>
</thead>
<tbody>
{% for station in stations %}
<tr>
<td>{{ station.id }}</td>
<td>{{ station.name }}</td>
<td>{{ station.position.lat }}</td>
<td>{{ station.position.long }}</td>
<td>{{ station.availableBikes }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Station name</th>
<th>Latitude</th>
<th>Longitude</th>
<th>Available bikes
<br>
<a href="{{ url('refresh_stations') }}" class="btn btn-primary btn-xs">refresh</a>
</th>
</tr>
</thead>
<tbody>
{% for station in stations %}
<tr>
<td>{{ station.id }}</td>
<td>{{ station.name }}</td>
<td>{{ station.position.lat }}</td>
<td>{{ station.position.long }}</td>
<td>{{ station.availableBikes }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>

{% endblock %}
</div>
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$this->progress->finish();
echo "\n";
$output->writeln('');
}

public function addedDockingStation(Response $response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$this->progress->finish();
echo "\nAvailable bikes successfully refreshed! \n";
$output->writeln('');
$output->writeln('Available bikes successfully refreshed!');
}

public function updatedAvailableBikesOnStations(Response $response)
Expand Down
73 changes: 73 additions & 0 deletions src/AppBundle/Controller/UpdateAvailableBikesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace AppBundle\Controller;

use AppBundle\Command\UpdateAvailableBikesFromProviderCommand;
use Cocoders\CityBike\DockingStationsProvider;
use Cocoders\UseCase\UpdateAvailableBikes\Command;
use Cocoders\UseCase\UpdateAvailableBikes\Responder;
use Cocoders\UseCase\UpdateAvailableBikes\Response;
use Cocoders\UseCase\UpdateAvailableBikes\UpdateAvailableBikes;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class UpdateAvailableBikesController extends Controller implements Responder
{
private $response;

/**
* @Route("/app/refresh", name="refresh_stations")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function updateAvailableBikesAction()
{
$stations = $this->parseDockingStations();

foreach ($stations as $station) {
$this->updateAvailableBikes()->execute(new Command(
$station['id'],
$station['availableBikes']), $this);
}
return $this->response;
}

public function updatedAvailableBikesOnStations(Response $response)
{
$this->getFlashMessage(
'notice',
['Available bikes successfully refreshed!']
);

$this->response = $this->redirectToRoute('form');
}

public function invalidDockingStation(Response $response)
{
$this->getFlashMessage(
'notice',
['Available bikes NOT refreshed! Please review docking station repository.']
);

$this->response = $this->redirectToRoute('form');
}

private function parseDockingStations()
{
return $this->get('cocoders.trm24.parser')->getDockingStations();
}

private function updateAvailableBikes()
{
return $this->get('cocoders.use_case.update_available_bikes');
}

private function getFlashMessage($type, $message)
{
return $this->container->get('session')->getFlashBag()->set(
$type,
$message
);
}
}

0 comments on commit 525b799

Please sign in to comment.