Skip to content

Commit

Permalink
Merge branch 'main' into #90-send-e-mail-when-provider-is-added-torem…
Browse files Browse the repository at this point in the history
…oved-from-favorite-city
  • Loading branch information
JakubKermes authored Mar 23, 2024
2 parents bd3205d + a2e5954 commit f765c4c
Show file tree
Hide file tree
Showing 60 changed files with 1,421 additions and 510 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-and-lint-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v4

- name: Cache dependencies
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.os }}-npm-dependencies-${{ hashFiles('package.lock') }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-and-lint-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
run: composer validate

- name: Cache dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: vendor
key: ${{ runner.os }}-composer-dependencies-${{ hashFiles('composer.lock') }}
Expand Down
84 changes: 84 additions & 0 deletions app/Http/Controllers/Admin/ProviderController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\ProviderRequest;
use App\Http\Resources\ProviderResource;
use App\Models\Provider;
use Illuminate\Http\Response as InertiaResponse;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Inertia\Inertia;
use Inertia\Response;

class ProviderController extends Controller
{
public const ITEMS_PER_PAGE = 15;

public function index(): Response
{
$providers = Provider::query()
->search("name")
->orderByName()
->orderByTimeRange()
->paginate(self::ITEMS_PER_PAGE)
->withQueryString();

return Inertia::render("Providers/Index", [
"providers" => ProviderResource::collection($providers),
]);
}

public function store(ProviderRequest $request): void
{
Provider::query()->create($request->validated());

$fileName = $this->getFilename($request);
$fileContents = $request->file("file")->get();

Storage::disk("public")->put("providers/" . $fileName, $fileContents);
}

public function update(ProviderRequest $request, Provider $provider): void
{
$provider->update($request->validated());

$imageName = $this->getFilename($request);
$storageImagePath = storage_path("app/public/providers/" . $imageName);
$resourceImagePath = resource_path("providers/" . $imageName);
$imageContents = $request->file("file")->get();

if (file_exists($resourceImagePath)) {
file_put_contents($resourceImagePath, $imageContents);
Storage::put($storageImagePath, file_get_contents($imageContents));
} else {
Storage::put($storageImagePath, file_get_contents($imageContents));
}
}

public function destroy(Provider $provider): void
{
$provider->delete();
$imagePath = storage_path("app/public/providers/" . strtolower($provider["name"]) . ".png");
File::delete($imagePath);
}

public function showLogo(string $filename): InertiaResponse
{
$imagePath = storage_path("app/public/providers/" . $filename);

if (!file_exists($imagePath)) {
$imagePath = storage_path("app/public/providers/unknown.png");
}

return response(file_get_contents($imagePath), 200, ["Content-Type" => "image/png"]);
}

public function getFilename(ProviderRequest $request): string
{
return strtolower($request["name"]) . "." . $request->file("file")->getClientOriginalExtension();
}
}
35 changes: 35 additions & 0 deletions app/Http/Requests/ProviderRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\File;
use Illuminate\Validation\Rules\Unique;

class ProviderRequest extends FormRequest
{
public function rules(): array
{
return [
"name" => ["required", "string", "regex:/^[A-Z\s]/", "max:100", $this->uniqueRuleForProvider("name")],
"color" => ["required", "string", "size:7"],
"file" => [
"required",
"mimes:png",
File::image()
->max(config("app.provider_logo_size"))
->dimensions(Rule::dimensions()->width(config("app.provider_logo_width"))->height(config("app.provider_logo_height"))),
],
];
}

protected function uniqueRuleForProvider(string $column): Unique
{
$currentProviderId = $this->route(param: "provider");

return Rule::unique(table: "providers", column: $column)->ignore($currentProviderId);
}
}
2 changes: 2 additions & 0 deletions app/Http/Resources/ProviderResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function toArray(Request $request): array
return [
"name" => $this->name,
"url" => $this->url,
"android_url" => $this->android_url,
"ios_url" => $this->ios_url,
"color" => $this->color,
];
}
Expand Down
13 changes: 6 additions & 7 deletions app/Importers/BeamDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function extract(): static
}

$crawler = new Crawler($html);
$this->sections = $crawler->filter("div .find-beam-box");
$this->sections = $crawler->filter("div.find-beam-box");

if (count($this->sections) === 0) {
$this->createImportInfoDetails("204", self::getProviderName());
Expand All @@ -37,7 +37,7 @@ public function extract(): static

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

if ($this->stopExecution) {
return;
Expand Down Expand Up @@ -73,18 +73,17 @@ public function transform(): void
$arrayOfCitiesNames = array_filter($arrayOfCitiesNames, fn($record) => strpos($record, "") === false);

foreach ($arrayOfCitiesNames as $cityName) {
if ($cityName === "Selangor") {
} else {
if ($cityName !== "Selangor") {
$cityName = trim($cityName);

if ($countryName === "Korea") {
$countryName = "South Korea";
}
$provider = $this->load($cityName, $countryName);
}

if ($provider !== "") {
$existingCityProviders[] = $provider;
if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion app/Importers/BinBinDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function transform(): void
$existingCityProviders[] = $provider;
}
}

$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}
}
2 changes: 1 addition & 1 deletion app/Importers/BitMobilityDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function transform(): void
}
$existingCityProviders = [];
$crawler = new Crawler($this->html);
$this->sections = $crawler->filter(".wpb_content_element > .wpb_wrapper > p > a");
$this->sections = $crawler->filter("div.e-hotspot__tooltip");

if (count($this->sections) === 0) {
$this->createImportInfoDetails("204", self::getProviderName());
Expand Down
66 changes: 38 additions & 28 deletions app/Importers/LinkDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
class LinkDataImporter extends DataImporter
{
protected Crawler $sections;
private string $countryName = "";

public function extract(): static
{
Expand All @@ -26,7 +25,7 @@ public function extract(): static
}

$crawler = new Crawler($html);
$this->sections = $crawler->filter(".Main-content .sqs-row.row > .col p > strong");
$this->sections = $crawler->filter(".Main-content .sqs-row.row > .col p");

if (count($this->sections) === 0) {
$this->createImportInfoDetails("204", self::getProviderName());
Expand All @@ -39,48 +38,59 @@ public function extract(): static

public function transform(): void
{
$countryName = "";

$cityName = "";

$states = [
"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida",
"Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine",
"Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska",
"Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota",
"Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee",
"Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming",
];

if ($this->stopExecution) {
return;
}

$existingCityProviders = [];

foreach ($this->sections as $section) {
foreach ($section->childNodes as $node) {
$countryName = trim($node->nodeValue);

foreach ($node->parentNode->parentNode->parentNode->childNodes as $i => $cityName) {
if ($i === 0 || !trim($cityName->nodeValue)) {
continue;
}

$name = $cityName->nodeValue;
$skipFirstIteration = true;

$cities = [];
foreach ($this->sections as $section) {
if ($skipFirstIteration) {
$skipFirstIteration = false;

if (str_contains($name, "(") && str_contains($name, ")")) {
$names = explode("(", $name)[1];
$names = explode(")", $names)[0];
$names = explode(", ", $names);
continue;
}

foreach ($names as $name) {
$cities[] = str_replace("*", "", $name);
}
foreach ($section->childNodes as $node) {
if ($node->nodeName === "strong") {
if (!in_array($node->nodeValue, $states, true)) {
$countryName = trim($node->nodeValue);
} else {
$cities[] = $name;
$countryName = "United States";
}
}

foreach ($cities as $name) {
$provider = $this->load($name, $countryName);
if ($node->nodeName === "#text") {
$cityName = $node->nodeValue;
} else if ($node->nodeName === "a") {
$cityName = $node->nodeValue;
}

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
if ($cityName === " ") {
continue;
}
$provider = $this->load($cityName, $countryName);

if ($provider !== "") {
$existingCityProviders[] = $provider;
}
}
}

$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}
}
16 changes: 14 additions & 2 deletions app/Models/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Models;

use App\QueryBuilders\SortQuery;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
Expand All @@ -16,9 +18,19 @@ class Provider extends Model
public $incrementing = false;
protected $primaryKey = "name";
protected $keyType = "string";
protected $fillable = [
"name",
"url",
"color",
];

public function cityProvider()
public static function query(): Builder
{
return $this->belongsTo(CityProvider::class);
return parent::query();
}

public function newEloquentBuilder($query): SortQuery
{
return new SortQuery($query);
}
}
Loading

0 comments on commit f765c4c

Please sign in to comment.