Skip to content

Commit

Permalink
Set Default Language from .env
Browse files Browse the repository at this point in the history
  • Loading branch information
eusonlito committed Dec 8, 2023
1 parent 46936ad commit 1f9fa02
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ APP_KEY=
APP_DEBUG=false
APP_URL=

APP_LOCALE=es
APP_LOCALE=en

LOG_CHANNEL=stack
LOG_REQUEST=false
Expand Down
2 changes: 2 additions & 0 deletions app/Domains/City/Test/Factory/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Domains\CoreApp\Test\Factory\FactoryAbstract;
use App\Domains\City\Model\City as Model;
use App\Domains\Country\Model\Country as CountryModel;
use App\Domains\State\Model\State as StateModel;

class City extends FactoryAbstract
Expand All @@ -23,6 +24,7 @@ public function definition(): array

'point' => Model::pointFromLatitudeLongitude(42.34818, -7.9126),

'country_id' => $this->firstOrFactory(CountryModel::class),
'state_id' => $this->firstOrFactory(StateModel::class),
];
}
Expand Down
2 changes: 1 addition & 1 deletion app/Domains/Language/Action/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected function rowDefault(): ?Model
{
return Model::query()
->selectSession()
->whereDefault(true)
->whereDefault()
->first();
}

Expand Down
11 changes: 5 additions & 6 deletions app/Domains/Language/Model/Builder/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function byCode(string $code): self
*/
public function list(): self
{
return $this->orderBy('default', 'DESC')->orderBy('name', 'ASC');
return $this->orderByRaw('IF (`code` = ?, TRUE, FALSE) DESC', config('app.locale'))
->orderBy('name', 'ASC');
}

/**
Expand All @@ -39,16 +40,14 @@ public function selectSession(): self
*/
public function whenIdOrDefault(?int $id): self
{
return $this->when($id, static fn ($q) => $q->byId($id), static fn ($q) => $q->whereDefault(true));
return $this->when($id, static fn ($q) => $q->byId($id), static fn ($q) => $q->whereDefault());
}

/**
* @param bool $default = true
*
* @return self
*/
public function whereDefault(bool $default = true): self
public function whereDefault(): self
{
return $this->where($this->addTable('default'), $default);
return $this->where($this->addTable('code'), config('app.locale'));
}
}
1 change: 0 additions & 1 deletion app/Domains/Language/Model/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class Language extends ModelAbstract
* @var array<string, string>
*/
protected $casts = [
'default' => 'boolean',
'enabled' => 'boolean',
];

Expand Down
2 changes: 0 additions & 2 deletions app/Domains/Language/Seeder/data/language.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
"code": "es",
"name": "Castellano",
"locale": "es_ES",
"default": true,
"enabled": true
},

{
"code": "en",
"name": "English",
"locale": "en_US",
"default": false,
"enabled": true
}
]
1 change: 0 additions & 1 deletion app/Domains/Language/Test/Factory/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function definition(): array
'code' => str_slug($name),
'locale' => $this->faker->name(),

'default' => true,
'enabled' => true,
];
}
Expand Down
10 changes: 5 additions & 5 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@
|
*/

'locale' => env('APP_LOCALE', 'es'),
'locales' => ['es', 'en', 'fr'],
'locale' => env('APP_LOCALE', 'en'),
'locales' => ['en', 'es', 'fr'],
'locale_system' => [
'es' => 'es_ES.UTF-8',
'en' => 'en_US.UTF-8',
'es' => 'es_ES.UTF-8',
'fr' => 'fr_FR.UTF-8',
],

Expand All @@ -99,7 +99,7 @@
|
*/

'fallback_locale' => 'es',
'fallback_locale' => 'en',

/*
|--------------------------------------------------------------------------
Expand All @@ -112,7 +112,7 @@
|
*/

'faker_locale' => 'es_ES',
'faker_locale' => 'en_US',

/*
|--------------------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion database/migrations/2021_01_14_000000_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ protected function tables(): void

$this->timestamps($table);

$table->unsignedBigInteger('country_id');
$table->unsignedBigInteger('state_id');
});

Expand Down Expand Up @@ -184,7 +185,6 @@ protected function tables(): void
$table->string('code')->unique();
$table->string('locale')->unique();

$table->boolean('default')->default(0);
$table->boolean('enabled')->default(0);

$this->timestamps($table);
Expand Down Expand Up @@ -254,7 +254,9 @@ protected function tables(): void
$this->timestamps($table);

$table->unsignedBigInteger('city_id')->nullable();
$table->unsignedBigInteger('country_id')->nullable();
$table->unsignedBigInteger('device_id')->nullable();
$table->unsignedBigInteger('state_id')->nullable();
$table->unsignedBigInteger('timezone_id');
$table->unsignedBigInteger('trip_id');
$table->unsignedBigInteger('user_id');
Expand Down Expand Up @@ -443,6 +445,7 @@ protected function keys(): void
Schema::table('city', function (Blueprint $table) {
$table->spatialIndex('point');

$this->foreignOnDeleteCascade($table, 'country');
$this->foreignOnDeleteCascade($table, 'state');
});

Expand Down Expand Up @@ -483,7 +486,9 @@ protected function keys(): void
$table->spatialIndex('point');

$this->foreignOnDeleteSetNull($table, 'city');
$this->foreignOnDeleteSetNull($table, 'country');
$this->foreignOnDeleteSetNull($table, 'device');
$this->foreignOnDeleteSetNull($table, 'state');
$this->foreignOnDeleteCascade($table, 'timezone');
$this->foreignOnDeleteCascade($table, 'trip');
$this->foreignOnDeleteCascade($table, 'user');
Expand Down
54 changes: 54 additions & 0 deletions database/migrations/2023_12_08_133000_language_default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Domains\CoreApp\Migration\MigrationAbstract;

return new class extends MigrationAbstract {
/**
* @return void
*/
public function up(): void
{
if ($this->upMigrated()) {
return;
}

$this->tables();
$this->upFinish();
}

/**
* @return bool
*/
protected function upMigrated(): bool
{
return Schema::hasColumn('language', 'default') === false;
}

/**
* @return void
*/
protected function tables(): void
{
Schema::table('language', function (Blueprint $table) {
$table->dropColumn('default');
});
}

/**
* @return void
*/
public function down(): void
{
Schema::table('language', function (Blueprint $table) {
$table->boolean('default')->default(0);
});

$this->db()->unprepared('
UPDATE `language`
SET `default` = TRUE
LIMIT 1;
');
}
};
16 changes: 8 additions & 8 deletions database/schema/mysql-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ CREATE TABLE `city` (
`point` point NOT NULL /*!80003 SRID 4326 */,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`country_id` bigint unsigned NOT NULL,
`state_id` bigint unsigned NOT NULL,
`country_id` bigint unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `city_name_index` (`name`),
SPATIAL KEY `city_point_spatialindex` (`point`),
KEY `city_country_fk` (`country_id`),
KEY `city_state_fk` (`state_id`),
SPATIAL KEY `city_point_index` (`point`),
KEY `city_country_fk` (`country_id`),
CONSTRAINT `city_country_fk` FOREIGN KEY (`country_id`) REFERENCES `country` (`id`) ON DELETE SET NULL,
CONSTRAINT `city_country_fk` FOREIGN KEY (`country_id`) REFERENCES `country` (`id`) ON DELETE CASCADE,
CONSTRAINT `city_state_fk` FOREIGN KEY (`state_id`) REFERENCES `state` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
Expand Down Expand Up @@ -257,7 +257,6 @@ CREATE TABLE `language` (
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`code` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`locale` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`default` tinyint(1) NOT NULL DEFAULT '0',
`enabled` tinyint(1) NOT NULL DEFAULT '0',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Expand Down Expand Up @@ -353,24 +352,24 @@ CREATE TABLE `position` (
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`city_id` bigint unsigned DEFAULT NULL,
`country_id` bigint unsigned DEFAULT NULL,
`device_id` bigint unsigned DEFAULT NULL,
`state_id` bigint unsigned DEFAULT NULL,
`timezone_id` bigint unsigned NOT NULL,
`trip_id` bigint unsigned NOT NULL,
`user_id` bigint unsigned NOT NULL,
`vehicle_id` bigint unsigned NOT NULL,
`country_id` bigint unsigned DEFAULT NULL,
`state_id` bigint unsigned DEFAULT NULL,
PRIMARY KEY (`id`),
SPATIAL KEY `position_point_spatialindex` (`point`),
KEY `position_city_fk` (`city_id`),
KEY `position_country_fk` (`country_id`),
KEY `position_state_fk` (`state_id`),
KEY `position_timezone_fk` (`timezone_id`),
KEY `position_vehicle_fk` (`vehicle_id`),
SPATIAL KEY `position_point_index` (`point`),
KEY `position_device_id_date_utc_at_index` (`device_id`,`date_utc_at`),
KEY `position_trip_id_date_utc_at_index` (`trip_id`,`date_utc_at`),
KEY `position_user_id_date_utc_at_index` (`user_id`,`date_utc_at`),
KEY `position_country_fk` (`country_id`),
KEY `position_state_fk` (`state_id`),
CONSTRAINT `position_city_fk` FOREIGN KEY (`city_id`) REFERENCES `city` (`id`) ON DELETE SET NULL,
CONSTRAINT `position_country_fk` FOREIGN KEY (`country_id`) REFERENCES `country` (`id`) ON DELETE SET NULL,
CONSTRAINT `position_device_fk` FOREIGN KEY (`device_id`) REFERENCES `device` (`id`) ON DELETE SET NULL,
Expand Down Expand Up @@ -661,3 +660,4 @@ INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (66,'2023_11_23_003
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (67,'2023_11_30_003000_refuel_position_id',1);
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (68,'2023_11_30_230000_city_country_id',1);
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (69,'2023_11_30_230000_position_state_country',1);
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (70,'2023_12_08_133000_language_default',1);

0 comments on commit 1f9fa02

Please sign in to comment.