119 lines
3.6 KiB
PHP
119 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Services;
|
|
|
|
use App\Enums\Product\ProductTypeEnums;
|
|
use App\Enums\Status;
|
|
use App\Repositories\Admin\Product\ProductRepositoryInterface;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
use Exception;
|
|
|
|
class ProductService
|
|
{
|
|
public function __construct(
|
|
protected ProductRepositoryInterface $productRepository
|
|
) {}
|
|
|
|
public function store(array $data)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$typeId = (int) ($data['type_id'] ?? 0);
|
|
$productType = $this->mapTypeIdToEnum($typeId);
|
|
|
|
if (!$productType) {
|
|
throw new Exception('Invalid Product Type');
|
|
}
|
|
|
|
$prepareData = [
|
|
'uuid' => (string) Str::uuid(),
|
|
'name' => $data['name'],
|
|
'slug' => Str::slug($data['name']),
|
|
'product_type' => $productType->value,
|
|
'is_active' => isset($data['status']) && (int)$data['status'] === Status::ACTIVE->value,
|
|
|
|
'price' => $data['price'] ?? 0,
|
|
'price_sale' => $data['price_sale'] ?? null,
|
|
'description' => $data['description'] ?? null,
|
|
|
|
// VPS Columns
|
|
'cpu' => $data['cpu'] ?? null,
|
|
'ram' => $data['ram'] ?? null,
|
|
'disk' => $data['disk'] ?? null,
|
|
'bandwidth' => $data['bandwidth'] ?? null,
|
|
'gpu' => $data['gpu'] ?? null, // if added later
|
|
|
|
// Extra fields
|
|
'sort_order' => $data['sort_order'] ?? 0,
|
|
'billing_cycle' => $data['billing_cycle'] ?? 'monthly',
|
|
];
|
|
|
|
$product = $this->productRepository->create($prepareData);
|
|
|
|
DB::commit();
|
|
return $product;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function update(int $id, array $data)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$prepareData = [];
|
|
|
|
if (isset($data['name'])) {
|
|
$prepareData['name'] = $data['name'];
|
|
$prepareData['slug'] = Str::slug($data['name']);
|
|
}
|
|
|
|
if (isset($data['status'])) {
|
|
$prepareData['is_active'] = (int)$data['status'] === Status::ACTIVE->value;
|
|
}
|
|
|
|
// Map other fields that overlap with fillable
|
|
$fillable = ['price', 'price_sale', 'billing_cycle', 'cpu', 'ram', 'gpu', 'disk', 'bandwidth', 'sort_order', 'description'];
|
|
foreach ($fillable as $field) {
|
|
if (array_key_exists($field, $data)) {
|
|
$prepareData[$field] = $data[$field];
|
|
}
|
|
}
|
|
|
|
$result = $this->productRepository->update($id, $prepareData);
|
|
|
|
DB::commit();
|
|
return $result;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function delete(int $id)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
$this->productRepository->delete($id);
|
|
DB::commit();
|
|
return true;
|
|
} catch (Exception $e) {
|
|
DB::rollBack();
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
private function mapTypeIdToEnum(int $typeId): ?ProductTypeEnums
|
|
{
|
|
return match ($typeId) {
|
|
1 => ProductTypeEnums::ADDON,
|
|
2 => ProductTypeEnums::DOMAIN,
|
|
3 => ProductTypeEnums::HOSTING,
|
|
4 => ProductTypeEnums::VPS,
|
|
default => null
|
|
};
|
|
}
|
|
}
|