107 lines
3.6 KiB
PHP
107 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ShopCategory;
|
|
use App\Models\ShopProduct;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ShopCatalogService
|
|
{
|
|
/** @return Collection<int, ShopCategory> */
|
|
public function categories(): Collection
|
|
{
|
|
return ShopCategory::query()->ordered()->withCount('products')->get();
|
|
}
|
|
|
|
/** @return Collection<int, ShopProduct> */
|
|
public function products(): Collection
|
|
{
|
|
return ShopProduct::query()->with('categories')->ordered()->get();
|
|
}
|
|
|
|
public function createCategory(array $data): ShopCategory
|
|
{
|
|
return ShopCategory::query()->create($this->categoryData($data));
|
|
}
|
|
|
|
public function updateCategory(ShopCategory $category, array $data): ShopCategory
|
|
{
|
|
$category->update($this->categoryData($data));
|
|
|
|
return $category->refresh();
|
|
}
|
|
|
|
public function deleteCategory(ShopCategory $category): void
|
|
{
|
|
DB::connection('store')->transaction(function () use ($category): void {
|
|
$category->products()->detach();
|
|
$category->delete();
|
|
});
|
|
}
|
|
|
|
public function createProduct(array $data): ShopProduct
|
|
{
|
|
return DB::connection('store')->transaction(function () use ($data): ShopProduct {
|
|
$product = ShopProduct::query()->create($this->productData($data));
|
|
$product->categories()->sync($data['category_ids']);
|
|
|
|
return $product->load('categories');
|
|
});
|
|
}
|
|
|
|
public function updateProduct(ShopProduct $product, array $data): ShopProduct
|
|
{
|
|
return DB::connection('store')->transaction(function () use ($product, $data): ShopProduct {
|
|
$product->update($this->productData($data));
|
|
$product->categories()->sync($data['category_ids']);
|
|
|
|
return $product->refresh()->load('categories');
|
|
});
|
|
}
|
|
|
|
public function deleteProduct(ShopProduct $product): void
|
|
{
|
|
DB::connection('store')->transaction(function () use ($product): void {
|
|
$product->categories()->detach();
|
|
$product->delete();
|
|
});
|
|
}
|
|
|
|
private function categoryData(array $data): array
|
|
{
|
|
return [
|
|
'name' => trim($data['name']),
|
|
'icon' => trim((string) ($data['icon'] ?? '')),
|
|
'requiredRank' => (int) $data['requiredRank'],
|
|
'flags' => (int) $data['flags'],
|
|
'enabled' => (int) ($data['enabled'] ?? false),
|
|
];
|
|
}
|
|
|
|
private function productData(array $data): array
|
|
{
|
|
return [
|
|
'type' => (int) $data['type'],
|
|
'name' => trim($data['name']),
|
|
'tooltipName' => trim((string) ($data['tooltipName'] ?? '')),
|
|
'tooltipType' => trim((string) ($data['tooltipType'] ?? '')),
|
|
'tooltipText' => trim((string) ($data['tooltipText'] ?? '')),
|
|
'icon' => trim((string) ($data['icon'] ?? '')),
|
|
'price' => (int) $data['price'],
|
|
'currency' => 1,
|
|
'hyperlinkId' => (int) ($data['hyperlinkId'] ?? 0),
|
|
'creatureEntry' => (int) ($data['creatureEntry'] ?? 0),
|
|
'discountAmount' => (int) ($data['discountAmount'] ?? 0),
|
|
'flags' => (int) ($data['flags'] ?? 0),
|
|
...collect(range(1, 8))->flatMap(fn (int $slot): array => [
|
|
'reward_'.$slot => (int) ($data['reward_'.$slot] ?? 0),
|
|
'rewardcount_'.$slot => (int) ($data['rewardcount_'.$slot] ?? 0),
|
|
])->all(),
|
|
'new' => (int) ($data['new'] ?? false),
|
|
'enabled' => (int) ($data['enabled'] ?? false),
|
|
];
|
|
}
|
|
}
|