125 lines
5.1 KiB
PHP
125 lines
5.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Concerns\BuildsPortalProps;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\SaveShopCategoryRequest;
|
|
use App\Http\Requests\Admin\SaveShopProductRequest;
|
|
use App\Models\ShopCategory;
|
|
use App\Models\ShopProduct;
|
|
use App\Services\ShopCatalogService;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class ShopController extends Controller
|
|
{
|
|
use BuildsPortalProps;
|
|
|
|
public function index(Request $request, ShopCatalogService $catalog): Response
|
|
{
|
|
$categories = $catalog->categories();
|
|
|
|
return Inertia::render('Admin/Shop', [
|
|
...$this->portalProps($request),
|
|
'categories' => $categories
|
|
->map(fn (ShopCategory $category): array => [
|
|
'id' => $category->id,
|
|
'name' => $category->name,
|
|
'icon' => $category->icon,
|
|
'required_rank' => $category->requiredRank,
|
|
'flags' => $category->flags,
|
|
'enabled' => $category->enabled,
|
|
'products_count' => $category->products_count,
|
|
'update_url' => route('admin.shop.categories.update', $category),
|
|
'destroy_url' => route('admin.shop.categories.destroy', $category),
|
|
])
|
|
->values()
|
|
->all(),
|
|
'products' => $catalog->products()
|
|
->map(fn (ShopProduct $product): array => [
|
|
'id' => $product->id,
|
|
'category_ids' => $product->categories->pluck('id')->values()->all(),
|
|
'category_names' => $product->categories->pluck('name')->values()->all(),
|
|
'type' => (int) $product->type,
|
|
'name' => $product->name,
|
|
'tooltip_name' => $product->tooltipName,
|
|
'tooltip_type' => $product->tooltipType,
|
|
'tooltip_text' => $product->tooltipText,
|
|
'icon' => $product->icon,
|
|
'price' => (int) $product->price,
|
|
'hyperlink_id' => (int) $product->hyperlinkId,
|
|
'creature_entry' => (int) $product->creatureEntry,
|
|
'discount_amount' => (int) $product->discountAmount,
|
|
'flags' => (int) $product->flags,
|
|
'rewards' => collect(range(1, 8))->map(fn (int $slot): array => [
|
|
'slot' => $slot,
|
|
'id' => (int) $product->{'reward_'.$slot},
|
|
'count' => (int) $product->{'rewardcount_'.$slot},
|
|
])->all(),
|
|
'is_new' => (bool) $product->new,
|
|
'enabled' => (bool) $product->enabled,
|
|
'update_url' => route('admin.shop.products.update', $product),
|
|
'destroy_url' => route('admin.shop.products.destroy', $product),
|
|
])
|
|
->values()
|
|
->all(),
|
|
'categoryStoreUrl' => route('admin.shop.categories.store'),
|
|
'productStoreUrl' => route('admin.shop.products.store'),
|
|
'productTypes' => collect([
|
|
1 => 'Предмет',
|
|
3 => 'Маунт',
|
|
4 => 'Питомец',
|
|
5 => 'Бафф',
|
|
7 => 'Услуга персонажа',
|
|
8 => 'Буст',
|
|
9 => 'Титул',
|
|
])->map(fn (string $label, int $value): array => compact('value', 'label'))->values()->all(),
|
|
]);
|
|
}
|
|
|
|
public function storeCategory(SaveShopCategoryRequest $request, ShopCatalogService $catalog): RedirectResponse
|
|
{
|
|
$catalog->createCategory($request->validated());
|
|
|
|
return back()->with('status', 'Категория создана.');
|
|
}
|
|
|
|
public function updateCategory(ShopCategory $category, SaveShopCategoryRequest $request, ShopCatalogService $catalog): RedirectResponse
|
|
{
|
|
$catalog->updateCategory($category, $request->validated());
|
|
|
|
return back()->with('status', 'Категория обновлена.');
|
|
}
|
|
|
|
public function destroyCategory(ShopCategory $category, ShopCatalogService $catalog): RedirectResponse
|
|
{
|
|
$catalog->deleteCategory($category);
|
|
|
|
return back()->with('status', 'Категория удалена.');
|
|
}
|
|
|
|
public function storeProduct(SaveShopProductRequest $request, ShopCatalogService $catalog): RedirectResponse
|
|
{
|
|
$catalog->createProduct($request->validated());
|
|
|
|
return back()->with('status', 'Товар создан.');
|
|
}
|
|
|
|
public function updateProduct(ShopProduct $product, SaveShopProductRequest $request, ShopCatalogService $catalog): RedirectResponse
|
|
{
|
|
$catalog->updateProduct($product, $request->validated());
|
|
|
|
return back()->with('status', 'Товар обновлён.');
|
|
}
|
|
|
|
public function destroyProduct(ShopProduct $product, ShopCatalogService $catalog): RedirectResponse
|
|
{
|
|
$catalog->deleteProduct($product);
|
|
|
|
return back()->with('status', 'Товар удалён.');
|
|
}
|
|
}
|