Refactor project structure to use domain-oriented architecture; add Product and Tenant domains with controllers, models, requests, resources, and routes
This commit is contained in:
parent
507b81b54e
commit
93a2750d33
|
|
@ -0,0 +1,86 @@
|
||||||
|
# Project Conventions
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
This project uses a domain-oriented structure under `app/Domains`.
|
||||||
|
|
||||||
|
Each domain must live in:
|
||||||
|
|
||||||
|
```text
|
||||||
|
app/Domains/<DomainName>/
|
||||||
|
```
|
||||||
|
|
||||||
|
Supported folders inside each domain:
|
||||||
|
|
||||||
|
```text
|
||||||
|
app/Domains/<DomainName>/
|
||||||
|
├── Models/
|
||||||
|
├── Controllers/
|
||||||
|
├── Requests/
|
||||||
|
├── Resources/
|
||||||
|
├── Services/
|
||||||
|
├── Policies/
|
||||||
|
├── Exceptions/
|
||||||
|
└── routes/
|
||||||
|
```
|
||||||
|
|
||||||
|
## Routing
|
||||||
|
|
||||||
|
- Root API routes live in `routes/api.php`.
|
||||||
|
- `routes/api.php` should act as an aggregator only.
|
||||||
|
- Each domain defines its own routes in `app/Domains/<DomainName>/routes/api.php`.
|
||||||
|
|
||||||
|
## Namespaces
|
||||||
|
|
||||||
|
Use this namespace pattern:
|
||||||
|
|
||||||
|
```php
|
||||||
|
App\Domains\<DomainName>\Models
|
||||||
|
App\Domains\<DomainName>\Controllers
|
||||||
|
App\Domains\<DomainName>\Requests
|
||||||
|
App\Domains\<DomainName>\Resources
|
||||||
|
App\Domains\<DomainName>\Services
|
||||||
|
App\Domains\<DomainName>\Policies
|
||||||
|
App\Domains\<DomainName>\Exceptions
|
||||||
|
```
|
||||||
|
|
||||||
|
## Responsibilities
|
||||||
|
|
||||||
|
- `Models/`: Eloquent models and model relationships.
|
||||||
|
- `Controllers/`: HTTP entrypoints only.
|
||||||
|
- `Requests/`: validation and request authorization with `FormRequest`.
|
||||||
|
- `Resources/`: API response transformation with `JsonResource`.
|
||||||
|
- `Services/`: business logic and application workflows.
|
||||||
|
- `Policies/`: authorization rules.
|
||||||
|
- `Exceptions/`: domain-specific exceptions.
|
||||||
|
- `routes/`: domain route definitions.
|
||||||
|
|
||||||
|
## Controller Rules
|
||||||
|
|
||||||
|
- Controllers should stay thin.
|
||||||
|
- Do not validate inline in controllers when a `FormRequest` can be used.
|
||||||
|
- Do not place business logic in controllers.
|
||||||
|
- Prefer returning `JsonResource` or resource collections instead of raw models.
|
||||||
|
|
||||||
|
## Model Rules
|
||||||
|
|
||||||
|
- Models must stay inside their domain.
|
||||||
|
- Cross-domain relations are allowed when the dependency is explicit and necessary.
|
||||||
|
- Preserve existing table names and key mappings when the database uses legacy names.
|
||||||
|
|
||||||
|
## Naming Rules
|
||||||
|
|
||||||
|
- Domains use singular English names such as `Tenant` and `Product`.
|
||||||
|
- Namespace, folder, and class names must stay aligned.
|
||||||
|
- Route files should be named `api.php`.
|
||||||
|
|
||||||
|
## Current Domains
|
||||||
|
|
||||||
|
- `Tenant`
|
||||||
|
- `Product`
|
||||||
|
|
||||||
|
## Notes For Future Changes
|
||||||
|
|
||||||
|
- New features should be added inside the corresponding domain first.
|
||||||
|
- If a concern does not belong to an existing domain, create a new domain under `app/Domains`.
|
||||||
|
- Avoid placing new business code in `app/Http/Controllers` or `app/Models`.
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Controllers;
|
||||||
|
|
||||||
|
use App\Domains\Product\Models\Product;
|
||||||
|
use App\Domains\Product\Requests\StoreProductRequest;
|
||||||
|
use App\Domains\Product\Requests\UpdateProductRequest;
|
||||||
|
use App\Domains\Product\Resources\ProductResource;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
|
||||||
|
class ProductController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): JsonResponse
|
||||||
|
{
|
||||||
|
return ProductResource::collection(Product::query()->latest()->get())->response();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(StoreProductRequest $request): JsonResponse
|
||||||
|
{
|
||||||
|
$product = Product::create($request->validated());
|
||||||
|
|
||||||
|
return ProductResource::make($product)->response()->setStatusCode(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(Product $producto): ProductResource
|
||||||
|
{
|
||||||
|
return ProductResource::make($producto);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(UpdateProductRequest $request, Product $producto): ProductResource
|
||||||
|
{
|
||||||
|
$producto->update($request->validated());
|
||||||
|
|
||||||
|
return ProductResource::make($producto);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(Product $producto): Response
|
||||||
|
{
|
||||||
|
$producto->delete();
|
||||||
|
|
||||||
|
return response()->noContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Brand extends Model
|
||||||
|
{
|
||||||
|
// Placeholder model for the product domain structure.
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Category extends Model
|
||||||
|
{
|
||||||
|
// Placeholder model for the product domain structure.
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Domains\Product\Models;
|
||||||
|
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
@ -15,7 +16,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
'descripcion',
|
'descripcion',
|
||||||
'precio',
|
'precio',
|
||||||
])]
|
])]
|
||||||
class Producto extends Model
|
class Product extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class StoreProductRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'tenant_codigo' => ['required', 'string', 'exists:tenants,codigo'],
|
||||||
|
'categoria_id' => ['required', 'integer'],
|
||||||
|
'slug' => ['required', 'string', 'max:255', Rule::unique('productos', 'slug')],
|
||||||
|
'nombre' => ['required', 'string', 'max:255'],
|
||||||
|
'descripcion' => ['nullable', 'string'],
|
||||||
|
'precio' => ['required', 'numeric', 'min:0'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Requests;
|
||||||
|
|
||||||
|
use App\Domains\Product\Models\Product;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class UpdateProductRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
/** @var Product|null $product */
|
||||||
|
$product = $this->route('producto');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'tenant_codigo' => ['required', 'string', 'exists:tenants,codigo'],
|
||||||
|
'categoria_id' => ['required', 'integer'],
|
||||||
|
'slug' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
'max:255',
|
||||||
|
Rule::unique('productos', 'slug')->ignore($product?->id),
|
||||||
|
],
|
||||||
|
'nombre' => ['required', 'string', 'max:255'],
|
||||||
|
'descripcion' => ['nullable', 'string'],
|
||||||
|
'precio' => ['required', 'numeric', 'min:0'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @mixin \App\Domains\Product\Models\Product
|
||||||
|
*/
|
||||||
|
class ProductResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'tenant_codigo' => $this->tenant_codigo,
|
||||||
|
'categoria_id' => $this->categoria_id,
|
||||||
|
'slug' => $this->slug,
|
||||||
|
'nombre' => $this->nombre,
|
||||||
|
'descripcion' => $this->descripcion,
|
||||||
|
'precio' => $this->precio,
|
||||||
|
'created_at' => $this->created_at,
|
||||||
|
'updated_at' => $this->updated_at,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Services;
|
||||||
|
|
||||||
|
class ProductService
|
||||||
|
{
|
||||||
|
// Reserved for product domain business logic.
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Domains\Product\Controllers\ProductController;
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
Route::apiResource('productos', ProductController::class);
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Controllers;
|
||||||
|
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use App\Domains\Tenant\Requests\StoreTenantRequest;
|
||||||
|
use App\Domains\Tenant\Requests\UpdateTenantRequest;
|
||||||
|
use App\Domains\Tenant\Resources\TenantResource;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
|
||||||
|
class TenantController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): JsonResponse
|
||||||
|
{
|
||||||
|
return TenantResource::collection(Tenant::query()->latest()->get())->response();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(StoreTenantRequest $request): JsonResponse
|
||||||
|
{
|
||||||
|
$tenant = Tenant::create($request->validated());
|
||||||
|
|
||||||
|
return TenantResource::make($tenant)->response()->setStatusCode(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(Tenant $tenant): TenantResource
|
||||||
|
{
|
||||||
|
return TenantResource::make($tenant);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(UpdateTenantRequest $request, Tenant $tenant): TenantResource
|
||||||
|
{
|
||||||
|
$tenant->update($request->validated());
|
||||||
|
|
||||||
|
return TenantResource::make($tenant);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(Tenant $tenant): Response
|
||||||
|
{
|
||||||
|
$tenant->delete();
|
||||||
|
|
||||||
|
return response()->noContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Controllers;
|
||||||
|
|
||||||
|
use App\Domains\Tenant\Models\TenantProp;
|
||||||
|
use App\Domains\Tenant\Requests\StoreTenantPropRequest;
|
||||||
|
use App\Domains\Tenant\Requests\UpdateTenantPropRequest;
|
||||||
|
use App\Domains\Tenant\Resources\TenantPropResource;
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Response;
|
||||||
|
|
||||||
|
class TenantPropController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): JsonResponse
|
||||||
|
{
|
||||||
|
return TenantPropResource::collection(TenantProp::query()->latest()->get())->response();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(StoreTenantPropRequest $request): JsonResponse
|
||||||
|
{
|
||||||
|
$tenantProp = TenantProp::create($request->validated());
|
||||||
|
|
||||||
|
return TenantPropResource::make($tenantProp)->response()->setStatusCode(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(TenantProp $tenantProp): TenantPropResource
|
||||||
|
{
|
||||||
|
return TenantPropResource::make($tenantProp);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(UpdateTenantPropRequest $request, TenantProp $tenantProp): TenantPropResource
|
||||||
|
{
|
||||||
|
$tenantProp->update($request->validated());
|
||||||
|
|
||||||
|
return TenantPropResource::make($tenantProp);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(TenantProp $tenantProp): Response
|
||||||
|
{
|
||||||
|
$tenantProp->delete();
|
||||||
|
|
||||||
|
return response()->noContent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Models;
|
||||||
|
|
||||||
|
use App\Domains\Product\Models\Product;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
|
|
||||||
|
#[Fillable([
|
||||||
|
'codigo',
|
||||||
|
'nombre',
|
||||||
|
'dominio',
|
||||||
|
])]
|
||||||
|
class Tenant extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return HasMany<Product, $this>
|
||||||
|
*/
|
||||||
|
public function productos(): HasMany
|
||||||
|
{
|
||||||
|
return $this->hasMany(Product::class, 'tenant_codigo', 'codigo');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BelongsToMany<TenantProp, $this>
|
||||||
|
*/
|
||||||
|
public function tenantProps(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(TenantProp::class, 'tenant_prop_definitions', 'tenant_codigo', 'prop_codigo', 'codigo', 'codigo')
|
||||||
|
->withPivot('valor')
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
|
#[Fillable([
|
||||||
|
'codigo',
|
||||||
|
'prop_type',
|
||||||
|
])]
|
||||||
|
class TenantProp extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return BelongsToMany<Tenant, $this>
|
||||||
|
*/
|
||||||
|
public function tenants(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Tenant::class, 'tenant_prop_definitions', 'prop_codigo', 'tenant_codigo', 'codigo', 'codigo')
|
||||||
|
->withPivot('valor')
|
||||||
|
->withTimestamps();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class StoreTenantPropRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'codigo' => ['required', 'string', 'max:255', Rule::unique('tenant_props', 'codigo')],
|
||||||
|
'prop_type' => ['required', 'string', 'max:255'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class StoreTenantRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'codigo' => ['required', 'string', 'max:255', Rule::unique('tenants', 'codigo')],
|
||||||
|
'nombre' => ['required', 'string', 'max:255'],
|
||||||
|
'dominio' => ['nullable', 'string', 'max:255'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Requests;
|
||||||
|
|
||||||
|
use App\Domains\Tenant\Models\TenantProp;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class UpdateTenantPropRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
/** @var TenantProp|null $tenantProp */
|
||||||
|
$tenantProp = $this->route('tenantProp');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'codigo' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
'max:255',
|
||||||
|
Rule::unique('tenant_props', 'codigo')->ignore($tenantProp?->id),
|
||||||
|
],
|
||||||
|
'prop_type' => ['required', 'string', 'max:255'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Requests;
|
||||||
|
|
||||||
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class UpdateTenantRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
/** @var Tenant|null $tenant */
|
||||||
|
$tenant = $this->route('tenant');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'codigo' => [
|
||||||
|
'required',
|
||||||
|
'string',
|
||||||
|
'max:255',
|
||||||
|
Rule::unique('tenants', 'codigo')->ignore($tenant?->id),
|
||||||
|
],
|
||||||
|
'nombre' => ['required', 'string', 'max:255'],
|
||||||
|
'dominio' => ['nullable', 'string', 'max:255'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @mixin \App\Domains\Tenant\Models\TenantProp
|
||||||
|
*/
|
||||||
|
class TenantPropResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'codigo' => $this->codigo,
|
||||||
|
'prop_type' => $this->prop_type,
|
||||||
|
'created_at' => $this->created_at,
|
||||||
|
'updated_at' => $this->updated_at,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @mixin \App\Domains\Tenant\Models\Tenant
|
||||||
|
*/
|
||||||
|
class TenantResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'codigo' => $this->codigo,
|
||||||
|
'nombre' => $this->nombre,
|
||||||
|
'dominio' => $this->dominio,
|
||||||
|
'created_at' => $this->created_at,
|
||||||
|
'updated_at' => $this->updated_at,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Domains\Tenant\Controllers\TenantController;
|
||||||
|
use App\Domains\Tenant\Controllers\TenantPropController;
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
Route::apiResource('tenants', TenantController::class);
|
||||||
|
Route::apiResource('tenant-props', TenantPropController::class)
|
||||||
|
->parameters(['tenant-props' => 'tenantProp']);
|
||||||
|
|
@ -1,67 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
|
||||||
|
|
||||||
use App\Models\Producto;
|
|
||||||
use Illuminate\Http\JsonResponse;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Illuminate\Http\Response;
|
|
||||||
use Illuminate\Validation\Rule;
|
|
||||||
|
|
||||||
class ProductController extends Controller
|
|
||||||
{
|
|
||||||
public function index(): JsonResponse
|
|
||||||
{
|
|
||||||
return response()->json(Producto::query()->latest()->get());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function store(Request $request): JsonResponse
|
|
||||||
{
|
|
||||||
$validated = $request->validate($this->rules());
|
|
||||||
|
|
||||||
$producto = Producto::create($validated);
|
|
||||||
|
|
||||||
return response()->json($producto, 201);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function show(Producto $producto): JsonResponse
|
|
||||||
{
|
|
||||||
return response()->json($producto);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function update(Request $request, Producto $producto): JsonResponse
|
|
||||||
{
|
|
||||||
$validated = $request->validate($this->rules($producto->id));
|
|
||||||
|
|
||||||
$producto->update($validated);
|
|
||||||
|
|
||||||
return response()->json($producto);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function destroy(Producto $producto): Response
|
|
||||||
{
|
|
||||||
$producto->delete();
|
|
||||||
|
|
||||||
return response()->noContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
protected function rules(?int $productoId = null): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'tenant_codigo' => ['required', 'string', 'exists:tenants,codigo'],
|
|
||||||
'categoria_id' => ['required', 'integer'],
|
|
||||||
'slug' => [
|
|
||||||
'required',
|
|
||||||
'string',
|
|
||||||
'max:255',
|
|
||||||
Rule::unique('productos', 'slug')->ignore($productoId),
|
|
||||||
],
|
|
||||||
'nombre' => ['required', 'string', 'max:255'],
|
|
||||||
'descripcion' => ['nullable', 'string'],
|
|
||||||
'precio' => ['required', 'numeric', 'min:0'],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,64 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
|
||||||
|
|
||||||
use App\Models\Tenant;
|
|
||||||
use Illuminate\Http\JsonResponse;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Illuminate\Http\Response;
|
|
||||||
use Illuminate\Validation\Rule;
|
|
||||||
|
|
||||||
class TenantController extends Controller
|
|
||||||
{
|
|
||||||
public function index(): JsonResponse
|
|
||||||
{
|
|
||||||
return response()->json(Tenant::query()->latest()->get());
|
|
||||||
}
|
|
||||||
|
|
||||||
public function store(Request $request): JsonResponse
|
|
||||||
{
|
|
||||||
$validated = $request->validate($this->rules());
|
|
||||||
|
|
||||||
$tenant = Tenant::create($validated);
|
|
||||||
|
|
||||||
return response()->json($tenant, 201);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function show(Tenant $tenant): JsonResponse
|
|
||||||
{
|
|
||||||
return response()->json($tenant);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function update(Request $request, Tenant $tenant): JsonResponse
|
|
||||||
{
|
|
||||||
$validated = $request->validate($this->rules($tenant->id));
|
|
||||||
|
|
||||||
$tenant->update($validated);
|
|
||||||
|
|
||||||
return response()->json($tenant);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function destroy(Tenant $tenant): Response
|
|
||||||
{
|
|
||||||
$tenant->delete();
|
|
||||||
|
|
||||||
return response()->noContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
protected function rules(?int $tenantId = null): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'codigo' => [
|
|
||||||
'required',
|
|
||||||
'string',
|
|
||||||
'max:255',
|
|
||||||
Rule::unique('tenants', 'codigo')->ignore($tenantId),
|
|
||||||
],
|
|
||||||
'nombre' => ['required', 'string', 'max:255'],
|
|
||||||
'dominio' => ['nullable', 'string', 'max:255'],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Models;
|
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
||||||
|
|
||||||
#[Fillable([
|
|
||||||
'codigo',
|
|
||||||
'nombre',
|
|
||||||
'dominio',
|
|
||||||
])]
|
|
||||||
class Tenant extends Model
|
|
||||||
{
|
|
||||||
use HasFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return HasMany<Producto, $this>
|
|
||||||
*/
|
|
||||||
public function productos(): HasMany
|
|
||||||
{
|
|
||||||
return $this->hasMany(Producto::class, 'tenant_codigo', 'codigo');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('tenant_props', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('codigo')->unique();
|
||||||
|
$table->string('prop_type');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('tenant_props');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('tenant_prop_definitions', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('tenant_codigo');
|
||||||
|
$table->string('prop_codigo');
|
||||||
|
$table->text('valor')->nullable();
|
||||||
|
$table->timestamps();
|
||||||
|
|
||||||
|
$table->foreign('tenant_codigo')
|
||||||
|
->references('codigo')
|
||||||
|
->on('tenants')
|
||||||
|
->cascadeOnUpdate()
|
||||||
|
->cascadeOnDelete();
|
||||||
|
|
||||||
|
$table->foreign('prop_codigo')
|
||||||
|
->references('codigo')
|
||||||
|
->on('tenant_props')
|
||||||
|
->cascadeOnUpdate()
|
||||||
|
->cascadeOnDelete();
|
||||||
|
|
||||||
|
$table->unique(['tenant_codigo', 'prop_codigo']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('tenant_prop_definitions');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Http\Controllers\ProductController;
|
|
||||||
use App\Http\Controllers\TenantController;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
|
@ -9,5 +7,5 @@ Route::get('/user', function (Request $request) {
|
||||||
return $request->user();
|
return $request->user();
|
||||||
})->middleware('auth:sanctum');
|
})->middleware('auth:sanctum');
|
||||||
|
|
||||||
Route::apiResource('productos', ProductController::class);
|
require __DIR__.'/../app/Domains/Product/routes/api.php';
|
||||||
Route::apiResource('tenants', TenantController::class);
|
require __DIR__.'/../app/Domains/Tenant/routes/api.php';
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue