Added City, Company to get and save data from API.

main
KKlochko 2 years ago
parent 87483cce4e
commit 814b56ca3d

@ -0,0 +1,20 @@
<?php
namespace App\DotsAPI\API\v2;
use App\DotsAPI\Fetcher\v2\ApiFetcher;
abstract class AbstractItemAPI
{
protected ApiFetcher $fetcher;
public function __construct(ApiFetcher $fetcher)
{
$this->fetcher = $fetcher;
}
abstract public function getMap($context = null);
abstract public function saveMap($map);
}

@ -0,0 +1,32 @@
<?php
namespace App\DotsAPI\API\v2;
use App\DotsAPI\API\v2\AbstractItemAPI;
use App\Models\City;
class CityAPI extends AbstractItemAPI
{
public function getMap() {
$endpoint = '/api/v2/cities';
$citiesMap = $this->fetcher->get($endpoint);
return $citiesMap;
}
public function saveMap($cities) {
foreach ($cities as $city) {
$uuid = $city['id'];
$name = $city['name'];
$url = $city['url'];
City::firstOrCreate([
'uuid' => $uuid,
'name' => $name,
'url' => $url
]);
}
}
}

@ -0,0 +1,33 @@
<?php
namespace App\DotsAPI\API\v2;
use App\DotsAPI\API\v2\AbstractItemAPI;
use App\Models\Company;
class CompanyAPI extends AbstractItemAPI
{
public function getMap($city_uuid = null) {
$endpoint = '/api/v2/cities/' . $city_uuid . '/companies/';
$companiesMap = $this->fetcher->get($endpoint);
return $companiesMap;
}
public function saveMap($companies) {
foreach ($companies as $company) {
$uuid = $company['id'];
$name = $company['name'];
$image = $company['image'] ?? '';
$description = $company['description'] ?? '';
Company::firstOrCreate([
'uuid' => $uuid,
'name' => $name,
'image' => $image,
'description' => $description,
]);
}
}
}
Loading…
Cancel
Save