Skip to content
This repository has been archived by the owner on Apr 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #173 from Cotemero/master
Browse files Browse the repository at this point in the history
feat(API): Adding and modifications through API
  • Loading branch information
miguelantoniosantos committed Aug 14, 2019
2 parents 1deedc6 + 25bb3fb commit 5c5c663
Show file tree
Hide file tree
Showing 5 changed files with 445 additions and 38 deletions.
2 changes: 2 additions & 0 deletions .env.example
Expand Up @@ -53,4 +53,6 @@ ERRORS_SPREADSHEET_LINK=
FUELSTATIONS_SOURCE1=
FUELSTATIONS_SOURCE2=

API_V1_DOWNLOAD_API_CSV=

GOOGLE_RECAPTCHA_V3_SECRET=
188 changes: 181 additions & 7 deletions app/Http/Controllers/APIController.php
Expand Up @@ -6,6 +6,7 @@
use App\ExternalAuth;
use App\FuelStation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class APIController extends Controller
{
Expand Down Expand Up @@ -43,13 +44,20 @@ public function fetch(Request $request)
$output = [];
foreach ($stations as $station) {
$output[] = [
'id' => $station->id,
'name' => $station->name,
'has_gasoline' => $station->has_gasoline,
'has_diesel' => $station->has_diesel,
'has_lpg' => $station->has_lpg,
'lat' => $station->lat,
'long' => $station->long,
'id' => $station->id,
'name' => $station->name,
'brand' => $station->brand,
'sell_gasoline' => $station->sell_gasoline,
'sell_diesel' => $station->sell_diesel,
'sell_lpg' => $station->sell_lpg,
'has_gasoline' => $station->has_gasoline,
'has_diesel' => $station->has_diesel,
'has_lpg' => $station->has_lpg,
'lat' => $station->long,
'long' => $station->lat,
'repa' => $station->repa,
'county' => $station->county,
'district' => $station->district,
];
}

Expand Down Expand Up @@ -106,6 +114,121 @@ public function push(Request $request)
return response()->json($output);
}

public function add(Request $request)
{
$output = ['success' => 0];
if ($request->has('key') && $request->has('secret') && $request->has('name') && $request->has('brand') && $request->has('repa') && $request->has('sell_gasoline') && $request->has('sell_diesel') && $request->has('sell_lpg') && $request->has('county') && $request->has('district') && $request->has('lat') && $request->has('long')) {
$ext_auth = ExternalAuth::where([['key', '=', $request->input('key')],['secret', '=', $request->input('secret')]]);
if ($ext_auth->count() > 0) {
$ext_auth = $ext_auth->first();
if (($request->input('brand') == $ext_auth->brand) || ($ext_auth->brand == 'WRITEREAD')) {
$sell_gasoline = \intval($request->input('sell_gasoline'));
$sell_diesel = \intval($request->input('sell_diesel'));
$sell_lpg = \intval($request->input('sell_lpg'));
if (! \is_nan($sell_gasoline) && ! \is_nan($sell_diesel) && ! \is_nan($sell_lpg)) {
if ($sell_gasoline > 1) {
$sell_gasoline = 1;
} elseif ($sell_gasoline < 0) {
$sell_gasoline = 0;
}
if ($sell_diesel > 1) {
$sell_diesel = 1;
} elseif ($sell_diesel < 0) {
$sell_diesel = 0;
}
if ($sell_lpg > 1) {
$sell_lpg = 1;
} elseif ($sell_lpg < 0) {
$sell_lpg = 0;
}
$data = [
'repa' => $request->input('repa'),
'source_id' => 'api',
'brand' => $request->input('brand'),
'name' => $request->input('name'),
'sell_gasoline' => $sell_gasoline,
'sell_diesel' => $sell_diesel,
'sell_lpg' => $sell_lpg,
'has_gasoline' => $sell_gasoline,
'has_diesel' => $sell_diesel,
'has_lpg' => $sell_lpg,
'long' => \floatval($request->input('lat')),
'lat' => \floatval($request->input('long')),
'county' => $request->input('county'),
'district' => $request->input('district'),
];
$fuel_station = new FuelStation();
$fuel_station->fill($data);
$fuel_station->save();
$cacheController = new CacheController();
$cacheController->updateStations();
$output = ['success' => 1];
}
}
}
}
return response()->json($output);
}

public function change(Request $request)
{
$output = ['success' => 0];
if ($request->has('key') && $request->has('secret') && $request->has('name') && $request->has('brand') && $request->has('repa') && $request->has('sell_gasoline') && $request->has('sell_diesel') && $request->has('sell_lpg') && $request->has('id') && $request->has('county') && $request->has('district') && $request->has('lat') && $request->has('long')) {
$ext_auth = ExternalAuth::where([['key', '=', $request->input('key')],['secret', '=', $request->input('secret')]]);
if ($ext_auth->count() > 0) {
$ext_auth = $ext_auth->first();
$fuel_station = FuelStation::where('id', '=', $request->input('id'));
if ($fuel_station->count() > 0) {
$fuel_station = $fuel_station->first();
if (($fuel_station->brand == $ext_auth->brand) || ($ext_auth->brand == 'WRITEREAD')) {
$sell_gasoline = \intval($request->input('sell_gasoline'));
$sell_diesel = \intval($request->input('sell_diesel'));
$sell_lpg = \intval($request->input('sell_lpg'));
if (! \is_nan($sell_gasoline) && ! \is_nan($sell_diesel) && ! \is_nan($sell_lpg)) {
if ($sell_gasoline > 1) {
$sell_gasoline = 1;
} elseif ($sell_gasoline < 0) {
$sell_gasoline = 0;
}
if ($sell_diesel > 1) {
$sell_diesel = 1;
} elseif ($sell_diesel < 0) {
$sell_diesel = 0;
}
if ($sell_lpg > 1) {
$sell_lpg = 1;
} elseif ($sell_lpg < 0) {
$sell_lpg = 0;
}
$data = [
'repa' => $request->input('repa'),
'source_id' => 'api',
'brand' => $request->input('brand'),
'name' => $request->input('name'),
'sell_gasoline' => $sell_gasoline,
'sell_diesel' => $sell_diesel,
'sell_lpg' => $sell_lpg,
'has_gasoline' => $sell_gasoline,
'has_diesel' => $sell_diesel,
'has_lpg' => $sell_lpg,
'long' => \floatval($request->input('lat')),
'lat' => \floatval($request->input('long')),
'county' => $request->input('county'),
'district' => $request->input('district'),
];
$fuel_station->fill($data);
$fuel_station->save();
$cacheController = new CacheController();
$cacheController->updateStations();
$output = ['success' => 1];
}
}
}
}
}
return response()->json($output);
}

public function fetch_csv()
{
$headers = [
Expand Down Expand Up @@ -152,4 +275,55 @@ public function fetch_csv()

return response()->stream($cb, 200, $headers);
}

public function fetch_api_csv()
{
$password = Input::get('password');
if ($password != env('API_V1_DOWNLOAD_API_CSV')) {
return response('0', 200)->header('Content-Type', 'text/plain');
}
$headers = [
'Content-type' => 'text/csv',
'Content-Disposition' => 'attachment; filename=file.csv',
'Pragma' => 'no-cache',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Expires' => '0',
];

$columns = [
'name',
'brand',
'lat',
'long',
'repa',
'sell_gasoline',
'sell_diesel',
'sell_lpg',
'has_gasoline',
'has_diesel',
'has_lpg',
'district',
'county',
'source_id',
];

$fuel_stations = FuelStation::all($columns)->where('source_id', '=', 'api');
$cb = function () use ($fuel_stations, $columns) {
$file = \fopen('php://output', 'w');
\fprintf($file, \chr(0xEF).\chr(0xBB).\chr(0xBF));
\fputcsv($file, $columns);

foreach ($fuel_stations as $fuel_station) {
if ($fuel_station['brand'] != 'POSTO ES') {
$long_save = $fuel_station['long'];
$fuel_station['long'] = $fuel_station['lat'];
$fuel_station['lat'] = $long_save;
\fputcsv($file, $fuel_station->toArray());
}
}
\fclose($file);
};

return response()->stream($cb, 200, $headers);
}
}
2 changes: 2 additions & 0 deletions app/Http/Middleware/VerifyCsrfToken.php
Expand Up @@ -23,6 +23,8 @@ class VerifyCsrfToken extends Middleware
'api/v1/fetch',
'api/v1/push',
'api/v1/info',
'api/v1/add',
'api/v1/change',
'panel/entries/add',
'error/push',
];
Expand Down

0 comments on commit 5c5c663

Please sign in to comment.