This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: list all addresses in vault (#422)
- Loading branch information
Showing
23 changed files
with
1,178 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesCitiesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageReports\Web\Controllers; | ||
|
||
use App\Domains\Vault\ManageReports\Web\ViewHelpers\ReportCitiesShowViewHelper; | ||
use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Vault; | ||
use Illuminate\Http\Request; | ||
use Inertia\Inertia; | ||
|
||
class ReportAddressesCitiesController extends Controller | ||
{ | ||
public function show(Request $request, int $vaultId, string $city) | ||
{ | ||
$vault = Vault::findOrFail($vaultId); | ||
$city = utf8_decode(urldecode($city)); | ||
|
||
return Inertia::render('Vault/Reports/Address/Cities/Index', [ | ||
'layoutData' => VaultIndexViewHelper::layoutData($vault), | ||
'data' => ReportCitiesShowViewHelper::data($vault, $city), | ||
]); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageReports\Web\Controllers; | ||
|
||
use App\Domains\Vault\ManageReports\Web\ViewHelpers\ReportAddressIndexViewHelper; | ||
use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Vault; | ||
use Illuminate\Http\Request; | ||
use Inertia\Inertia; | ||
|
||
class ReportAddressesController extends Controller | ||
{ | ||
public function index(Request $request, int $vaultId) | ||
{ | ||
$vault = Vault::findOrFail($vaultId); | ||
|
||
return Inertia::render('Vault/Reports/Address/Index', [ | ||
'layoutData' => VaultIndexViewHelper::layoutData($vault), | ||
'data' => ReportAddressIndexViewHelper::data($vault), | ||
]); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
app/Domains/Vault/ManageReports/Web/Controllers/ReportAddressesCountriesController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageReports\Web\Controllers; | ||
|
||
use App\Domains\Vault\ManageReports\Web\ViewHelpers\ReportCountriesShowViewHelper; | ||
use App\Domains\Vault\ManageVault\Web\ViewHelpers\VaultIndexViewHelper; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Vault; | ||
use Illuminate\Http\Request; | ||
use Inertia\Inertia; | ||
|
||
class ReportAddressesCountriesController extends Controller | ||
{ | ||
public function show(Request $request, int $vaultId, string $country) | ||
{ | ||
$vault = Vault::findOrFail($vaultId); | ||
$country = utf8_decode(urldecode($country)); | ||
|
||
return Inertia::render('Vault/Reports/Address/Countries/Index', [ | ||
'layoutData' => VaultIndexViewHelper::layoutData($vault), | ||
'data' => ReportCountriesShowViewHelper::data($vault, $country), | ||
]); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportAddressIndexViewHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageReports\Web\ViewHelpers; | ||
|
||
use App\Models\Vault; | ||
use Illuminate\Support\Str; | ||
|
||
class ReportAddressIndexViewHelper | ||
{ | ||
public static function data(Vault $vault): array | ||
{ | ||
// all the cities in the vault | ||
// the distinct method does not do a case-insensitive search, so we need | ||
// to do it manually. there would be a way to do it with raw SQL but | ||
// it wouldn't work on sqlite | ||
$cities = $vault->addresses() | ||
->select('id', 'city') | ||
->whereNotNull('city') | ||
->withCount('contacts') | ||
->distinct('city') | ||
->get() | ||
->map(fn ($address) => [ | ||
'id' => $address->id, | ||
'name' => Str::ucfirst($address->city), | ||
'contacts' => $address->contacts_count, | ||
'url' => [ | ||
'index' => route('vault.reports.addresses.cities.show', [ | ||
'vault' => $vault->id, | ||
'city' => urlencode(utf8_encode($address->city)), | ||
]), | ||
], | ||
]) | ||
->unique('name'); | ||
|
||
// all the countries in the vault | ||
$countries = $vault->addresses() | ||
->select('id', 'country') | ||
->whereNotNull('country') | ||
->withCount('contacts') | ||
->distinct('country') | ||
->get() | ||
->map(fn ($address) => [ | ||
'id' => $address->id, | ||
'name' => Str::ucfirst($address->country), | ||
'contacts' => $address->contacts_count, | ||
'url' => [ | ||
'index' => route('vault.reports.addresses.countries.show', [ | ||
'vault' => $vault->id, | ||
'country' => urlencode(utf8_encode($address->country)), | ||
]), | ||
], | ||
]) | ||
->unique('name'); | ||
|
||
return [ | ||
'cities' => $cities, | ||
'countries' => $countries, | ||
]; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCitiesShowViewHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageReports\Web\ViewHelpers; | ||
|
||
use App\Helpers\ContactCardHelper; | ||
use App\Helpers\MapHelper; | ||
use App\Helpers\WikipediaHelper; | ||
use App\Models\Contact; | ||
use App\Models\Vault; | ||
use Illuminate\Support\Str; | ||
|
||
class ReportCitiesShowViewHelper | ||
{ | ||
public static function data(Vault $vault, string $city): array | ||
{ | ||
$addresses = $vault->addresses() | ||
->whereNotNull('city') | ||
->where('city', Str::ucfirst($city)) | ||
->orWhere('city', Str::lcfirst($city)) | ||
->with('contacts') | ||
->get() | ||
->map(fn ($address) => [ | ||
'id' => $address->id, | ||
'name' => Str::ucfirst($address->city), | ||
'address' => MapHelper::getAddressAsString($address), | ||
'contacts' => $address->contacts() | ||
->get() | ||
->map(fn (Contact $contact) => ContactCardHelper::data($contact)), | ||
]); | ||
|
||
$wikipediaInformation = WikipediaHelper::getInformation($city); | ||
|
||
return [ | ||
'city' => Str::ucfirst($city), | ||
'addresses' => $addresses, | ||
'wikipedia' => [ | ||
'url' => $wikipediaInformation['url'], | ||
'description' => $wikipediaInformation['description'], | ||
'thumbnail' => $wikipediaInformation['thumbnail'], | ||
], | ||
'url' => [ | ||
'addresses' => route('vault.reports.addresses.index', [ | ||
'vault' => $vault->id, | ||
]), | ||
], | ||
]; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
app/Domains/Vault/ManageReports/Web/ViewHelpers/ReportCountriesShowViewHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Domains\Vault\ManageReports\Web\ViewHelpers; | ||
|
||
use App\Helpers\ContactCardHelper; | ||
use App\Helpers\MapHelper; | ||
use App\Helpers\WikipediaHelper; | ||
use App\Models\Contact; | ||
use App\Models\Vault; | ||
use Illuminate\Support\Str; | ||
|
||
class ReportCountriesShowViewHelper | ||
{ | ||
public static function data(Vault $vault, string $country): array | ||
{ | ||
$addresses = $vault->addresses() | ||
->whereNotNull('country') | ||
->where('country', Str::ucfirst($country)) | ||
->orWhere('country', Str::lcfirst($country)) | ||
->with('contacts') | ||
->get() | ||
->map(fn ($address) => [ | ||
'id' => $address->id, | ||
'name' => Str::ucfirst($address->country), | ||
'address' => MapHelper::getAddressAsString($address), | ||
'contacts' => $address->contacts() | ||
->get() | ||
->map(fn (Contact $contact) => ContactCardHelper::data($contact)), | ||
]); | ||
|
||
$wikipediaInformation = WikipediaHelper::getInformation($country); | ||
|
||
return [ | ||
'country' => Str::ucfirst($country), | ||
'addresses' => $addresses, | ||
'wikipedia' => [ | ||
'url' => $wikipediaInformation['url'], | ||
'description' => $wikipediaInformation['description'], | ||
'thumbnail' => $wikipediaInformation['thumbnail'], | ||
], | ||
'url' => [ | ||
'addresses' => route('vault.reports.addresses.index', [ | ||
'vault' => $vault->id, | ||
]), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Support\Str; | ||
|
||
class WikipediaHelper | ||
{ | ||
/** | ||
* Return the information about the given city or country from Wikipedia. | ||
* All API calls are documented here: | ||
* https://www.mediawiki.org/w/api.php?action=help&modules=query. | ||
* | ||
* @param string $topic | ||
* @return array | ||
*/ | ||
public static function getInformation(string $topic): array | ||
{ | ||
$query = http_build_query([ | ||
'action' => 'query', | ||
'prop' => 'description|pageimages', | ||
'titles' => $topic, | ||
'pithumbsize' => 400, | ||
'format' => 'json', | ||
]); | ||
|
||
$url = 'https://en.wikipedia.org/w/api.php?'.$query; | ||
|
||
$response = Http::get($url)->throw(); | ||
|
||
if ($response->json('query.pages.*.missing')[0] === true) { | ||
return [ | ||
'url' => null, | ||
'description' => null, | ||
'thumbnail' => null, | ||
]; | ||
} | ||
|
||
return [ | ||
'url' => 'https://en.wikipedia.org/wiki/'.Str::slug($topic), | ||
'description' => $response->json('query.pages.*.description')[0], | ||
'thumbnail' => $response->json('query.pages.*.thumbnail.source')[0], | ||
]; | ||
} | ||
} |
Oops, something went wrong.