diff --git a/app/Http/Requests/Bo/Brands/StoreBrandRequest.php b/app/Http/Requests/Bo/Brands/StoreBrandRequest.php index 74af912..b1b81c0 100644 --- a/app/Http/Requests/Bo/Brands/StoreBrandRequest.php +++ b/app/Http/Requests/Bo/Brands/StoreBrandRequest.php @@ -3,9 +3,12 @@ namespace App\Http\Requests\Bo\Brands; use Illuminate\Foundation\Http\FormRequest; +use Uwil\LaravelCommon\Traits\Requests\HasPicture; class StoreBrandRequest extends FormRequest { + use HasPicture; + /** * Determine if the user is authorized to make this request. * @@ -16,6 +19,16 @@ class StoreBrandRequest extends FormRequest return auth('backend')->check(); } + /** + * Prepare the data for validation. + * + * @return void + */ + protected function prepareForValidation(): void + { + $this->mergePicture('logo'); + } + /** * Get the validation rules that apply to the request. * @@ -23,9 +36,16 @@ class StoreBrandRequest extends FormRequest */ public function rules(): array { - return [ - 'name' => 'required|string|min:5|max:255', + $rules = [ + 'name' => 'required|unique:brands,name|string|min:5|max:255', + 'url' => 'required|url|min:5|max:255', + 'url_claim' => 'required|url|min:5|max:255', + 'domain' => 'required|unique:brands,domain|string|min:5|max:255', ]; + return \array_merge( + $rules, + $this->pictureRules('logo', 550, 550, 550, 550), + ); } /** @@ -36,7 +56,11 @@ class StoreBrandRequest extends FormRequest public function attributes(): array { return [ - 'name' => trans('Nom de la marque'), + 'name' => trans('validation.attributes.name'), + 'logo' => trans('validation.custom.logo'), + 'url' => trans('validation.custom.url'), + 'url_claim' => trans('validation.custom.url_claim'), + 'domain' => trans('validation.custom.domain'), ]; } } diff --git a/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php b/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php index 976fef4..0871ed5 100644 --- a/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php +++ b/app/Http/Requests/Bo/Brands/UpdateBrandRequest.php @@ -4,4 +4,34 @@ namespace App\Http\Requests\Bo\Brands; class UpdateBrandRequest extends StoreBrandRequest { + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(): array + { + /** @var integer $brandModelKey */ + $brandModelKey = request()->route()->parameter('brand')->getKey(); + $rules = [ + 'name' => [ + 'required', + "unique:brands,name,$brandModelKey", + 'string', + 'min:5', + 'max:255', + ], + 'domain' => [ + 'required', + "unique:brands,domain,$brandModelKey", + 'string', + 'min:5', + 'max:255', + ], + ]; + return \array_merge( + parent::rules(), + $rules, + ); + } } diff --git a/app/Models/Brand.php b/app/Models/Brand.php index 5a41323..64267f7 100644 --- a/app/Models/Brand.php +++ b/app/Models/Brand.php @@ -7,6 +7,7 @@ use App\Traits\Models\ActivityLog; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; +use Uwil\LaravelCommon\Helpers\FileHelper; /** * @property integer $id Brand id. @@ -54,14 +55,22 @@ class Brand extends Model */ protected static function booted(): void { + static::creating(function (self $model) { + $model->logo = FileHelper::storeFile($model, $model->logo, true); + }); static::created(function () { self::resetCache(); }); - static::updated(function () { - self::resetCache(); + static::updating(function (self $model) { + $model->logo = FileHelper::storeFile($model, $model->logo, true); }); - static::deleted(function () { + static::updated(function (self $model) { self::resetCache(); + FileHelper::removeOldFile($model, 'logo'); + }); + static::deleted(function (self $model) { + self::resetCache(); + FileHelper::removeOldFile($model, 'logo'); }); } diff --git a/database/factories/BrandFactory.php b/database/factories/BrandFactory.php index b005079..f77185b 100644 --- a/database/factories/BrandFactory.php +++ b/database/factories/BrandFactory.php @@ -22,10 +22,10 @@ class BrandFactory extends Factory public function definition(): array { return [ - 'name' => $this->faker->company, + 'name' => $this->faker->unique()->company, 'url' => $this->faker->url, 'url_claim' => $this->faker->url, - 'domain' => $this->faker->domainName, + 'domain' => $this->faker->unique()->domainName, ]; } } diff --git a/database/migrations/2025_08_07_171943_add_unique_key_on_brands_table.php b/database/migrations/2025_08_07_171943_add_unique_key_on_brands_table.php new file mode 100644 index 0000000..0046b18 --- /dev/null +++ b/database/migrations/2025_08_07_171943_add_unique_key_on_brands_table.php @@ -0,0 +1,52 @@ +get()->map(function (Brand $brandModel) { + $brandModel->logo = FileHelper::storeFile( + $brandModel, + new \SplFileInfo( + \resource_path('../database/factories/assets/brands/default-logo-550x550.jpg') + ) + ); + $urlExample = "https://example-{$brandModel->getKey()}.com"; + $brandModel->url = $urlExample; + $brandModel->url_claim = $urlExample; + $brandModel->domain = parse_url($urlExample, PHP_URL_HOST); + $brandModel->saveOrFail(); + }); + + // Set unique keys. + Schema::table('brands', function (Blueprint $table) { + $table->unique('name', 'brands_name_unique'); + $table->unique('domain', 'brands_domain_unique'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('brands', function (Blueprint $table) { + $table->dropUnique('brands_name_unique'); + $table->dropUnique('brands_domain_unique'); + }); + } +}; diff --git a/resources/lang/en/validation.php b/resources/lang/en/validation.php index d9ca963..60a633d 100644 --- a/resources/lang/en/validation.php +++ b/resources/lang/en/validation.php @@ -233,5 +233,9 @@ return [ 'type' => 'type', 'status' => 'status', 'label' => 'label', + 'domain' => 'domain', + 'logo' => 'logo', + 'url' => 'url', + 'url_claim' => 'claim url', ], ]; diff --git a/resources/lang/fr/validation.php b/resources/lang/fr/validation.php index 5f9da36..5138f0c 100644 --- a/resources/lang/fr/validation.php +++ b/resources/lang/fr/validation.php @@ -233,5 +233,9 @@ return [ 'type' => 'type', 'status' => 'status', 'label' => 'label', + 'domain' => 'domaine', + 'logo' => 'logo', + 'url' => 'lien', + 'url_claim' => 'lien réclamation', ], ]; diff --git a/resources/views/bo/pages/brands/create.blade.php b/resources/views/bo/pages/brands/create.blade.php index 29f92cc..0b6cb2c 100644 --- a/resources/views/bo/pages/brands/create.blade.php +++ b/resources/views/bo/pages/brands/create.blade.php @@ -29,7 +29,7 @@