feat: Implement product and product category management with dedicated CRUD operations, services, and repositories.

This commit is contained in:
pbao9
2026-02-06 11:05:18 +07:00
parent eef04846a8
commit 92151261b3
61 changed files with 2331 additions and 656 deletions
@@ -0,0 +1,43 @@
<?php
namespace App\Http\Requests\Product;
use App\Enums\Product\ProductTypeEnums;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StoreProductRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
$rules = [
'name' => ['required', 'string', 'max:255'],
'price' => ['required', 'numeric', 'min:0'],
'price_sale' => ['nullable', 'numeric', 'min:0'],
'description' => ['nullable', 'string'],
'status' => ['required', 'integer'], // Value from Status Enum
'type_id' => ['required', 'integer'],
];
// Infer product type from type_id (Legacy mapping needs to be consistent with Controller)
// Controller map: 1=>Addon, 2=>Domain, 3=>Hosting, 4=>VPS
// We can access input 'type_id'
$typeId = (int) $this->input('type_id');
if ($typeId === 4) { // VPS
$rules = array_merge($rules, [
'cpu' => ['required'],
'ram' => ['required'],
'disk' => ['required'],
'bandwidth' => ['required'],
]);
}
return $rules;
}
}
@@ -0,0 +1,41 @@
<?php
namespace App\Http\Requests\Product;
use Illuminate\Foundation\Http\FormRequest;
class UpdateProductRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
// Based on existing controller logic, update rules are similar but allow optional fields if strictly updating parameters
// However, standard update often requires validating presence if they are being updated.
// Controller used specific rules.
// We need to fetch the product to know its type if 'type_id' is not in request (usually it is in hidden field).
// Assuming type_id is present in update form as per Controller logic.
$rules = [
'name' => ['required', 'string', 'max:255'],
'price' => ['required', 'numeric', 'min:0'],
'product_id' => ['required', 'exists:products,id'],
];
$typeId = (int) $this->input('type_id');
if ($typeId === 4) { // VPS
$rules = array_merge($rules, [
'cpu' => ['required'],
'ram' => ['required'],
'disk' => ['required'],
]);
}
return $rules;
}
}
@@ -0,0 +1,87 @@
<?php
namespace App\Http\Requests\ProductCategories;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class ProductCategoryRequest extends FormRequest
{
/**
* Xác định quyền truy cập
*/
public function authorize(): bool
{
return true; // Quan trọng: phải true
}
/**
* Rules validate
*/
public function rules(): array
{
$categoryId = $this->route('id');
// hoặc: $this->route('product_category')
return [
'name' => [
'required',
'string',
'max:255',
],
'slug' => [
'nullable',
'string',
'max:255',
Rule::unique('product_categories', 'slug')
->ignore($categoryId),
],
'parent_id' => [
'nullable',
'integer',
'exists:product_categories,id',
],
'is_active' => [
'nullable',
'boolean',
],
'sort_order' => [
'nullable',
'integer',
'min:0',
],
];
}
/**
* Chuẩn hóa dữ liệu trước khi validate
*/
protected function prepareForValidation(): void
{
$this->merge([
'slug' => $this->slug
? \Str::slug($this->slug)
: \Str::slug($this->name),
'is_active' => $this->has('is_active')
? (bool) $this->is_active
: true,
]);
}
/**
* Custom message (tuỳ chọn)
*/
public function messages(): array
{
return [
'name.required' => 'Tên danh mục không được để trống.',
'slug.unique' => 'Slug đã tồn tại.',
'parent_id.exists' => 'Danh mục cha không tồn tại.',
];
}
}