feat: implement FeaturedGroup and FeaturedProduct controllers, requests, resources, and routes for managing featured products
This commit is contained in:
51
app/Domains/Product/Controllers/FeaturedGroupController.php
Normal file
51
app/Domains/Product/Controllers/FeaturedGroupController.php
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Controllers;
|
||||||
|
|
||||||
|
use App\Domains\Product\Models\FeaturedGroup;
|
||||||
|
use App\Domains\Product\Requests\StoreFeaturedGroupRequest;
|
||||||
|
use App\Domains\Product\Requests\UpdateFeaturedGroupRequest;
|
||||||
|
use App\Domains\Product\Resources\FeaturedGroupResource;
|
||||||
|
use App\Domains\Product\Services\FeaturedGroupService;
|
||||||
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||||
|
use Illuminate\Routing\Controller;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
||||||
|
class FeaturedGroupController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly FeaturedGroupService $featuredGroupService
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function index(string $tenant): AnonymousResourceCollection
|
||||||
|
{
|
||||||
|
$groups = FeaturedGroup::where('tenant_codigo', $tenant)->get();
|
||||||
|
return FeaturedGroupResource::collection($groups);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(StoreFeaturedGroupRequest $request, string $tenant): FeaturedGroupResource
|
||||||
|
{
|
||||||
|
$group = $this->featuredGroupService->createGroup($tenant, $request->validated());
|
||||||
|
return new FeaturedGroupResource($group);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(string $tenant, FeaturedGroup $featuredGroup): FeaturedGroupResource
|
||||||
|
{
|
||||||
|
abort_if($featuredGroup->tenant_codigo !== $tenant, 404);
|
||||||
|
return new FeaturedGroupResource($featuredGroup->load('featuredProducts'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(UpdateFeaturedGroupRequest $request, string $tenant, FeaturedGroup $featuredGroup): FeaturedGroupResource
|
||||||
|
{
|
||||||
|
abort_if($featuredGroup->tenant_codigo !== $tenant, 404);
|
||||||
|
$group = $this->featuredGroupService->updateGroup($featuredGroup, $request->validated());
|
||||||
|
return new FeaturedGroupResource($group);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(string $tenant, FeaturedGroup $featuredGroup): JsonResponse
|
||||||
|
{
|
||||||
|
abort_if($featuredGroup->tenant_codigo !== $tenant, 404);
|
||||||
|
$this->featuredGroupService->deleteGroup($featuredGroup);
|
||||||
|
return response()->json(null, 204);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Controllers;
|
||||||
|
|
||||||
|
use App\Domains\Product\Models\FeaturedGroup;
|
||||||
|
use App\Domains\Product\Models\FeaturedProduct;
|
||||||
|
use App\Domains\Product\Requests\StoreFeaturedProductRequest;
|
||||||
|
use App\Domains\Product\Requests\UpdateFeaturedProductRequest;
|
||||||
|
use App\Domains\Product\Resources\FeaturedProductResource;
|
||||||
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||||
|
use Illuminate\Routing\Controller;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
||||||
|
class FeaturedProductController extends Controller
|
||||||
|
{
|
||||||
|
public function index(string $tenant, FeaturedGroup $featuredGroup): AnonymousResourceCollection
|
||||||
|
{
|
||||||
|
abort_if($featuredGroup->tenant_codigo !== $tenant, 404);
|
||||||
|
return FeaturedProductResource::collection($featuredGroup->featuredProducts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(StoreFeaturedProductRequest $request, string $tenant, FeaturedGroup $featuredGroup): FeaturedProductResource
|
||||||
|
{
|
||||||
|
abort_if($featuredGroup->tenant_codigo !== $tenant, 404);
|
||||||
|
$product = $featuredGroup->featuredProducts()->create($request->validated());
|
||||||
|
return new FeaturedProductResource($product);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function update(UpdateFeaturedProductRequest $request, string $tenant, FeaturedGroup $featuredGroup, FeaturedProduct $featuredProduct): FeaturedProductResource
|
||||||
|
{
|
||||||
|
abort_if($featuredGroup->tenant_codigo !== $tenant || $featuredProduct->featured_group_id !== $featuredGroup->id, 404);
|
||||||
|
$featuredProduct->update($request->validated());
|
||||||
|
return new FeaturedProductResource($featuredProduct);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroy(string $tenant, FeaturedGroup $featuredGroup, FeaturedProduct $featuredProduct): JsonResponse
|
||||||
|
{
|
||||||
|
abort_if($featuredGroup->tenant_codigo !== $tenant || $featuredProduct->featured_group_id !== $featuredGroup->id, 404);
|
||||||
|
$featuredProduct->delete();
|
||||||
|
return response()->json(null, 204);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
app/Domains/Product/Requests/StoreFeaturedGroupRequest.php
Normal file
21
app/Domains/Product/Requests/StoreFeaturedGroupRequest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class StoreFeaturedGroupRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'group_name' => ['required', 'string', 'max:255'],
|
||||||
|
'product_layout' => ['required', 'string', 'in:row,column_with_image,column_with_cart'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
21
app/Domains/Product/Requests/StoreFeaturedProductRequest.php
Normal file
21
app/Domains/Product/Requests/StoreFeaturedProductRequest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class StoreFeaturedProductRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'product_id' => ['required', 'integer'],
|
||||||
|
'order' => ['nullable', 'integer'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
21
app/Domains/Product/Requests/UpdateFeaturedGroupRequest.php
Normal file
21
app/Domains/Product/Requests/UpdateFeaturedGroupRequest.php
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class UpdateFeaturedGroupRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'group_name' => ['sometimes', 'string', 'max:255'],
|
||||||
|
'product_layout' => ['sometimes', 'string', 'in:row,column_with_image,column_with_cart'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
|
class UpdateFeaturedProductRequest extends FormRequest
|
||||||
|
{
|
||||||
|
public function authorize(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'order' => ['sometimes', 'integer'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
20
app/Domains/Product/Resources/FeaturedGroupResource.php
Normal file
20
app/Domains/Product/Resources/FeaturedGroupResource.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class FeaturedGroupResource extends JsonResource
|
||||||
|
{
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'tenant_codigo' => $this->tenant_codigo,
|
||||||
|
'group_name' => $this->group_name,
|
||||||
|
'product_layout' => $this->product_layout,
|
||||||
|
'featured_products' => FeaturedProductResource::collection($this->whenLoaded('featuredProducts')),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
20
app/Domains/Product/Resources/FeaturedProductResource.php
Normal file
20
app/Domains/Product/Resources/FeaturedProductResource.php
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class FeaturedProductResource extends JsonResource
|
||||||
|
{
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'featured_group_id' => $this->featured_group_id,
|
||||||
|
'product_id' => $this->product_id,
|
||||||
|
'order' => $this->order,
|
||||||
|
// You can add a nested ProductResource here if needed and when loaded
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
25
app/Domains/Product/Services/FeaturedGroupService.php
Normal file
25
app/Domains/Product/Services/FeaturedGroupService.php
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Product\Services;
|
||||||
|
|
||||||
|
use App\Domains\Product\Models\FeaturedGroup;
|
||||||
|
|
||||||
|
class FeaturedGroupService
|
||||||
|
{
|
||||||
|
public function createGroup(string $tenantCodigo, array $data): FeaturedGroup
|
||||||
|
{
|
||||||
|
$data['tenant_codigo'] = $tenantCodigo;
|
||||||
|
return FeaturedGroup::create($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateGroup(FeaturedGroup $group, array $data): FeaturedGroup
|
||||||
|
{
|
||||||
|
$group->update($data);
|
||||||
|
return $group;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function deleteGroup(FeaturedGroup $group): bool|null
|
||||||
|
{
|
||||||
|
return $group->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
15
app/Domains/Product/routes/api.php
Normal file
15
app/Domains/Product/routes/api.php
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Domains\Product\Controllers\FeaturedGroupController;
|
||||||
|
use App\Domains\Product\Controllers\FeaturedProductController;
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
|
Route::prefix('tenants/{tenant:codigo}')->group(function (): void {
|
||||||
|
Route::apiResource('featured-groups', FeaturedGroupController::class);
|
||||||
|
Route::apiResource('featured-groups.products', FeaturedProductController::class)
|
||||||
|
->only(['index', 'store', 'update', 'destroy'])
|
||||||
|
->parameters([
|
||||||
|
'featured-groups' => 'featuredGroup',
|
||||||
|
'products' => 'featuredProduct',
|
||||||
|
]);
|
||||||
|
});
|
||||||
@@ -11,3 +11,4 @@ require __DIR__.'/../app/Domains/Purchase/routes/api.php';
|
|||||||
require __DIR__.'/../app/Domains/Tenant/routes/api.php';
|
require __DIR__.'/../app/Domains/Tenant/routes/api.php';
|
||||||
require __DIR__.'/../app/Domains/Integration/routes/api.php';
|
require __DIR__.'/../app/Domains/Integration/routes/api.php';
|
||||||
require __DIR__.'/../app/Domains/Menu/routes/api.php';
|
require __DIR__.'/../app/Domains/Menu/routes/api.php';
|
||||||
|
require __DIR__.'/../app/Domains/Product/routes/api.php';
|
||||||
|
|||||||
Reference in New Issue
Block a user