feat: Implement pagination for active services and introduce ServiceListResource for formatted API output.
This commit is contained in:
@@ -3,9 +3,11 @@
|
||||
namespace App\Http\Controllers\Api\Service;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\Service\ServiceListResource;
|
||||
use App\Http\Resources\Api\Service\ServiceResource;
|
||||
use App\Models\Product;
|
||||
use App\Services\Api\Service\ServiceService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ServiceController extends Controller
|
||||
{
|
||||
@@ -19,15 +21,15 @@ class ServiceController extends Controller
|
||||
/**
|
||||
* Lấy danh sách dịch vụ (sản phẩm) đang hoạt động
|
||||
*/
|
||||
public function getService()
|
||||
public function getService(Request $request)
|
||||
{
|
||||
$services = $this->serviceService->getActiveServices();
|
||||
$limit = $request->get('limit', 10);
|
||||
$services = $this->serviceService->getActiveServices($limit);
|
||||
|
||||
return response()->json([
|
||||
return ServiceListResource::collection($services)->additional([
|
||||
'error' => false,
|
||||
'message' => 'Lấy dữ liệu thành công',
|
||||
'page_type' => 'service',
|
||||
'data' => ServiceResource::collection($services),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources\Api\Service;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class ServiceListResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'uuid' => $this->uuid,
|
||||
'name' => $this->name,
|
||||
'slug' => $this->slug,
|
||||
'short_description' => $this->short_description,
|
||||
'description' => $this->description,
|
||||
'thumbnail' => $this->thumbnail ? asset($this->thumbnail) : null,
|
||||
'product_type' => $this->product_type,
|
||||
'specs' => [
|
||||
'cpu' => $this->cpu,
|
||||
'ram' => $this->ram,
|
||||
'disk' => $this->disk,
|
||||
'bandwidth' => $this->bandwidth,
|
||||
'gpu' => $this->gpu
|
||||
],
|
||||
'stock' => $this->stock,
|
||||
'price' => $this->price,
|
||||
'price_sale' => $this->price_sale,
|
||||
'group' => $this->category->name,
|
||||
'os' => $this->osVersions->groupBy('os_group_id')->map(function ($versions) {
|
||||
$group = $versions->first()->osGroup;
|
||||
return [
|
||||
'name' => $group?->name,
|
||||
'thumbnail' => $group?->thumbnail,
|
||||
'versions' => $versions->map(fn($os) => [
|
||||
'version' => $os->version,
|
||||
'name' => $os->name,
|
||||
])->values(),
|
||||
];
|
||||
})->values(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -15,13 +15,13 @@ class ServiceRepository extends BaseApiRepository implements ServiceRepositoryIn
|
||||
/**
|
||||
* Lấy danh sách sản phẩm đang hoạt động
|
||||
*/
|
||||
public function getActiveServices()
|
||||
public function getActiveServices(int $limit = 10)
|
||||
{
|
||||
return $this->model
|
||||
->with(['osVersions.osGroup', 'category'])
|
||||
->where('is_active', 1)
|
||||
->orderBy('sort_order', 'asc')
|
||||
->get();
|
||||
->paginate($limit);
|
||||
}
|
||||
|
||||
public function findActiveBySlug(string $slug)
|
||||
|
||||
@@ -9,7 +9,7 @@ interface ServiceRepositoryInterface extends BaseRepositoryInterface
|
||||
/**
|
||||
* Lấy danh sách sản phẩm đang hoạt động
|
||||
*/
|
||||
public function getActiveServices();
|
||||
public function getActiveServices(int $limit = 10);
|
||||
|
||||
public function findActiveBySlug(string $slug);
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@ class ServiceService
|
||||
/**
|
||||
* Lấy danh sách dịch vụ đang hoạt động
|
||||
*/
|
||||
public function getActiveServices()
|
||||
public function getActiveServices(int $limit = 10)
|
||||
{
|
||||
return $this->serviceRepository->getActiveServices();
|
||||
return $this->serviceRepository->getActiveServices($limit);
|
||||
}
|
||||
|
||||
public function findActiveBySlug(string $slug)
|
||||
|
||||
Reference in New Issue
Block a user