From 1f9fa0278906b5cec676e7a1ccd54f2695161d02 Mon Sep 17 00:00:00 2001 From: Lito Date: Fri, 8 Dec 2023 13:46:09 +0100 Subject: [PATCH] Set Default Language from .env --- .env.example | 2 +- app/Domains/City/Test/Factory/City.php | 2 + app/Domains/Language/Action/Request.php | 2 +- .../Language/Model/Builder/Language.php | 11 ++-- app/Domains/Language/Model/Language.php | 1 - .../Language/Seeder/data/language.json | 2 - .../Language/Test/Factory/Language.php | 1 - config/app.php | 10 ++-- .../migrations/2021_01_14_000000_base.php | 7 ++- .../2023_12_08_133000_language_default.php | 54 +++++++++++++++++++ database/schema/mysql-schema.sql | 16 +++--- 11 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 database/migrations/2023_12_08_133000_language_default.php diff --git a/.env.example b/.env.example index f1b7217b..df1626c1 100644 --- a/.env.example +++ b/.env.example @@ -4,7 +4,7 @@ APP_KEY= APP_DEBUG=false APP_URL= -APP_LOCALE=es +APP_LOCALE=en LOG_CHANNEL=stack LOG_REQUEST=false diff --git a/app/Domains/City/Test/Factory/City.php b/app/Domains/City/Test/Factory/City.php index 2868e647..53b6274a 100644 --- a/app/Domains/City/Test/Factory/City.php +++ b/app/Domains/City/Test/Factory/City.php @@ -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 @@ -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), ]; } diff --git a/app/Domains/Language/Action/Request.php b/app/Domains/Language/Action/Request.php index 73986980..1b3b610b 100644 --- a/app/Domains/Language/Action/Request.php +++ b/app/Domains/Language/Action/Request.php @@ -71,7 +71,7 @@ protected function rowDefault(): ?Model { return Model::query() ->selectSession() - ->whereDefault(true) + ->whereDefault() ->first(); } diff --git a/app/Domains/Language/Model/Builder/Language.php b/app/Domains/Language/Model/Builder/Language.php index 48cfb598..da031d11 100644 --- a/app/Domains/Language/Model/Builder/Language.php +++ b/app/Domains/Language/Model/Builder/Language.php @@ -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'); } /** @@ -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')); } } diff --git a/app/Domains/Language/Model/Language.php b/app/Domains/Language/Model/Language.php index 143479d4..1ef1c409 100644 --- a/app/Domains/Language/Model/Language.php +++ b/app/Domains/Language/Model/Language.php @@ -31,7 +31,6 @@ class Language extends ModelAbstract * @var array */ protected $casts = [ - 'default' => 'boolean', 'enabled' => 'boolean', ]; diff --git a/app/Domains/Language/Seeder/data/language.json b/app/Domains/Language/Seeder/data/language.json index b6ecbae5..42dc600e 100644 --- a/app/Domains/Language/Seeder/data/language.json +++ b/app/Domains/Language/Seeder/data/language.json @@ -3,7 +3,6 @@ "code": "es", "name": "Castellano", "locale": "es_ES", - "default": true, "enabled": true }, @@ -11,7 +10,6 @@ "code": "en", "name": "English", "locale": "en_US", - "default": false, "enabled": true } ] diff --git a/app/Domains/Language/Test/Factory/Language.php b/app/Domains/Language/Test/Factory/Language.php index 6eee4f4c..91a114a4 100644 --- a/app/Domains/Language/Test/Factory/Language.php +++ b/app/Domains/Language/Test/Factory/Language.php @@ -22,7 +22,6 @@ public function definition(): array 'code' => str_slug($name), 'locale' => $this->faker->name(), - 'default' => true, 'enabled' => true, ]; } diff --git a/config/app.php b/config/app.php index 9da3f65f..2f52a225 100644 --- a/config/app.php +++ b/config/app.php @@ -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', ], @@ -99,7 +99,7 @@ | */ - 'fallback_locale' => 'es', + 'fallback_locale' => 'en', /* |-------------------------------------------------------------------------- @@ -112,7 +112,7 @@ | */ - 'faker_locale' => 'es_ES', + 'faker_locale' => 'en_US', /* |-------------------------------------------------------------------------- diff --git a/database/migrations/2021_01_14_000000_base.php b/database/migrations/2021_01_14_000000_base.php index c88de88c..ec1ea2b9 100644 --- a/database/migrations/2021_01_14_000000_base.php +++ b/database/migrations/2021_01_14_000000_base.php @@ -91,6 +91,7 @@ protected function tables(): void $this->timestamps($table); + $table->unsignedBigInteger('country_id'); $table->unsignedBigInteger('state_id'); }); @@ -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); @@ -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'); @@ -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'); }); @@ -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'); diff --git a/database/migrations/2023_12_08_133000_language_default.php b/database/migrations/2023_12_08_133000_language_default.php new file mode 100644 index 00000000..6ee31157 --- /dev/null +++ b/database/migrations/2023_12_08_133000_language_default.php @@ -0,0 +1,54 @@ +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; + '); + } +}; diff --git a/database/schema/mysql-schema.sql b/database/schema/mysql-schema.sql index bd0aa98e..cb08c1e6 100644 --- a/database/schema/mysql-schema.sql +++ b/database/schema/mysql-schema.sql @@ -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 */; @@ -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, @@ -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, @@ -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);