Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#214 - restructure database for different types of vehicles #224

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/Exceptions/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function register(): void
});
}

/**
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
*/
public function render($request, Throwable $exception)
{
if ($exception instanceof ValidationException) {
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/CityProviderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function index(): JsonResponse
->sortByDesc(fn(City $city): int => $city->cityProviders->count()),
);

$providers = ProviderResource::collection(Provider::all()->sortBy("name"));
$providers = ProviderResource::collection(Provider::all());
$countries = Country::whereHas("cities.cityProviders")
->with(["cities.cityAlternativeNames", "cities.cityProviders"])
->get()
Expand Down
36 changes: 28 additions & 8 deletions app/Importers/BeamDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public function extract(): static

public function transform(): void
{
$url = "https://assets-global.website-files.com/63c4acbedbab5dea8b1b98cd/63d8a5b60da91e7d71298637_map-vehicle-saturn.png";
$escooterImageUrl = "https://assets-global.website-files.com/63c4acbedbab5dea8b1b98cd/63d8a5b60da91e7d71298637_map-vehicle-saturn.png";
$bikeImageUrl = "https://assets-global.website-files.com/63c4acbedbab5dea8b1b98cd/63d8a5b7f06e4a42de02241b_map-vehicle-saturn-apollo.png";

if ($this->stopExecution) {
return;
Expand All @@ -55,22 +56,31 @@ public function transform(): void
foreach ($node->childNodes as $div) {
if ($div->nodeName === "div") {
foreach ($div->childNodes as $city) {
if ($city->nodeName === "img" && $city->getAttribute("src") === $url) {
if (!$city->nodeName) {
continue;
}

$hasEscooters = false;
$hasBikes = false;

if ($city->getAttribute("src") === $escooterImageUrl) {
$hasEscooters = true;
} elseif ($city->nodeName === "img" && $city->getAttribute("src") !== $url) {
$hasEscooters = false;
}

if ($city->nodeName === "p" && $hasEscooters === true) {
if ($city->getAttribute("src") === $bikeImageUrl) {
$hasBikes = true;
}

if ($city->nodeName === "p") {
$search = ["\u{00A0}", "\u{200D}", "Prefecture"];
$valueToDelete = "Selangor";
$cityName = preg_replace('/[\p{Hiragana}\p{Katakana}\p{Han}]+/u', "", $city->nodeValue);
$cityName = str_replace($search, "", $cityName);
$cityName = preg_replace('/(?<=[^\s_\-])(?=[A-Z])/', " ", $city->nodeValue);
$cityName = preg_replace('/(?<=[^\s_\-])(?=[A-Z])/', " ", $cityName);
$arrayOfCitiesNames = explode(" ", $cityName);
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($value) => $value !== $valueToDelete);
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($record) => strlen($record) > 1);
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($record) => strpos($record, "•") === false);
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($record) => !str_contains($record, "•"));

foreach ($arrayOfCitiesNames as $cityName) {
if ($cityName !== "Selangor") {
Expand All @@ -79,7 +89,17 @@ public function transform(): void
if ($countryName === "Korea") {
$countryName = "South Korea";
}
$provider = $this->load($cityName, $countryName);
$services = [];

if ($hasBikes) {
$services[] = "bike";
}

if ($hasEscooters) {
$services[] = "escooter";
}
$services = ["escooter", "bike"];
$provider = $this->load($cityName, $countryName, $lat = "", $long = "", $services);

if ($provider !== "") {
$existingCityProviders[] = $provider;
Expand Down
8 changes: 4 additions & 4 deletions app/Importers/BirdDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function transform(): void

protected function parseData(string $html): array
{
$pattern = '/let features = \[([\s\S]*?)\];/';
$pattern = '/let features = \[([\s\S]*?)];/';

if (preg_match($pattern, $html, $matches)) {
$fetchedData = $matches[1];
Expand All @@ -75,15 +75,15 @@ protected function parseData(string $html): array
return array_map("trim", $coordinates);
}

protected function load(string $cityName, string $countryName, string $lat = "", string $long = ""): string
protected function load(string $cityName, string $countryName, string $lat = "", string $long = "", array $services = ["escooter"]): string
{
$city = City::query()->where("name", $cityName)->first();
$alternativeCityName = CityAlternativeName::query()->where("name", $cityName)->first();

if ($city || $alternativeCityName) {
$cityId = $city ? $city->id : $alternativeCityName->city_id;

$this->createProvider($cityId, self::getProviderName());
$this->createProvider($cityId, self::getProviderName(), $services);

return strval($cityId);
}
Expand All @@ -97,7 +97,7 @@ protected function load(string $cityName, string $countryName, string $lat = "",
"country_id" => $country->id,
]);

$this->createProvider($city->id, self::getProviderName());
$this->createProvider($city->id, self::getProviderName(), $services);

return strval($city->id);
}
Expand Down
26 changes: 16 additions & 10 deletions app/Importers/DataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Models\CityWithoutAssignedCountry;
use App\Models\Country;
use App\Models\ImportInfoDetail;
use App\Models\Service;
use App\Services\MapboxGeocodingService;
use GuzzleHttp\Client;
use Stichoza\GoogleTranslate\GoogleTranslate;
Expand Down Expand Up @@ -74,13 +75,18 @@ protected function countryNotFound(string $cityName, string $countryName): void
}
}

protected function createProvider(int $cityId, string $providerName): void
protected function createProvider(int $cityId, string $providerName, array $services): void
{
CityProvider::query()->updateOrCreate([
"city_id" => $cityId,
"provider_name" => $providerName,
"created_by" => "scrapper",
]);
foreach ($services as $service) {
$service = Service::query()->where("type", $service)->first();

CityProvider::query()->updateOrCreate([
"provider_name" => $providerName,
"city_id" => $cityId,
"created_by" => "scraper",
"service_id" => $service->id,
]);
}
}

protected function deleteMissingProviders(string $providerName, array $existingCityProviders): void
Expand Down Expand Up @@ -110,7 +116,7 @@ protected function createImportInfoDetails(string $code, string $providerName):
);
}

protected function load(string $cityName, string $countryName, string $lat = "", string $long = ""): string
protected function load(string $cityName, string $countryName, string $lat = "", string $long = "", array $services = ["escooter"]): string
{
$country = Country::query()->where("name", $countryName)->orWhere("alternative_name", $countryName)->first();

Expand All @@ -120,15 +126,15 @@ protected function load(string $cityName, string $countryName, string $lat = "",

if ($city) {
$cityId = $city->id;
$this->createProvider($cityId, self::getProviderName());
$this->createProvider($cityId, self::getProviderName(), $services);

return strval($cityId);
} elseif ($alternativeCityName) {
$cityId = $alternativeCityName->city_id;
$city = City::query()->where("id", $cityId)->first();

if ($city->country_id === $country->id) {
$this->createProvider($cityId, self::getProviderName());
$this->createProvider($cityId, self::getProviderName(), $services);
}
}

Expand All @@ -146,7 +152,7 @@ protected function load(string $cityName, string $countryName, string $lat = "",
"country_id" => $country->id,
]);

$this->createProvider($city->id, self::getProviderName());
$this->createProvider($city->id, self::getProviderName(), $services);

return strval($city->id);
}
Expand Down
6 changes: 3 additions & 3 deletions app/Importers/LimeDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ public function transform(): void
$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}

protected function load(string $cityName, string $countryName, string $lat = "", string $long = ""): string
protected function load(string $cityName, string $countryName, string $lat = "", string $long = "", array $services = ["escooter"]): string
{
$city = City::query()->where("name", $cityName)->first();
$alternativeCityName = CityAlternativeName::query()->where("name", $cityName)->first();

if ($city || $alternativeCityName) {
$cityId = $city ? $city->id : $alternativeCityName->city_id;

$this->createProvider($cityId, self::getProviderName());
$this->createProvider($cityId, self::getProviderName(), $services);

return strval($cityId);
}
Expand Down Expand Up @@ -98,7 +98,7 @@ protected function load(string $cityName, string $countryName, string $lat = "",
"country_id" => $country->id,
]);

$this->createProvider($city->id, self::getProviderName());
$this->createProvider($city->id, self::getProviderName(), $services);

return strval($city->id);
}
Expand Down
6 changes: 3 additions & 3 deletions app/Importers/SpinDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ public function transform(): void
$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}

protected function load(string $cityName, string $countryName, string $lat = "", string $long = ""): string
protected function load(string $cityName, string $countryName, string $lat = "", string $long = "", array $services = ["escooter"]): string
{
$city = City::query()->where("name", $cityName)->first();
$alternativeCityName = CityAlternativeName::query()->where("name", $cityName)->first();

if ($city || $alternativeCityName) {
$cityId = $city ? $city->id : $alternativeCityName->city_id;

$this->createProvider($cityId, self::getProviderName());
$this->createProvider($cityId, self::getProviderName(), $services);

return strval($cityId);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function load(string $cityName, string $countryName, string $lat = "",
"country_id" => $country->id,
]);

$this->createProvider($city->id, self::getProviderName());
$this->createProvider($city->id, self::getProviderName(), $services);

return strval($city->id);
}
Expand Down
8 changes: 8 additions & 0 deletions app/Models/CityProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
* @property int $id
* @property string $provider_name
* @property int $city_id
* @property string $created_by
* @property int $service_id
*/
class CityProvider extends Model
{
protected $fillable = [
"provider_name",
"city_id",
"created_by",
"service_id",
];

public function city(): BelongsTo
Expand All @@ -30,4 +33,9 @@ public function providers(): HasMany
{
return $this->hasMany(Provider::class);
}

public function services(): HasMany
{
return $this->hasMany(Service::class);
}
}
19 changes: 19 additions & 0 deletions app/Models/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
* @property int $id
* @property string $type
*/

class Service extends Model
{
protected $fillable = [
"type",
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function up(): void
->references("name")
->on("providers")
->onDelete("cascade");

$table->unsignedBigInteger("city_id");
$table->foreign("city_id")
->references("id")
Expand Down
23 changes: 23 additions & 0 deletions database/migrations/2024_04_04_152429_create_services_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::create("services", function (Blueprint $table): void {
$table->id();
$table->string("type");
$table->timestamps();
});
}

public function down(): void
{
Schema::dropIfExists("services");
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
public function up(): void
{
Schema::table("city_providers", function (Blueprint $table): void {
$table->unsignedBigInteger("service_id")->nullable();
$table->foreign("service_id")
->references("id")
->on("services")
->onDelete("cascade");
});
}

public function down(): void
{
Schema::table("city_providers", function (Blueprint $table): void {
$table->dropForeign(["service_id"]);
$table->dropColumn("service_id");
});
}
};
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function run(): void
CitiesAndCountriesSeeder::class,
ProviderSeeder::class,
CodeSeeder::class,
ServiceSeeder::class,
]);
}
}
23 changes: 23 additions & 0 deletions database/seeders/ServiceSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\Service;
use Illuminate\Database\Seeder;

class ServiceSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
Service::create(["type" => "bike"]);
Service::create(["type" => "cargo"]);
Service::create(["type" => "emoped"]);
Service::create(["type" => "escooter"]);
Service::create(["type" => "motorscooter"]);
}
}
Loading
Loading