| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Enums\StoreCategory;
- use App\Enums\StoreLanguage;
- use App\Http\Requests\Stores\CreateStoreRequest;
- use App\Models\Store;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Inertia\Inertia;
- use Inertia\Response;
-
- class StoreController extends Controller
- {
- /**
- * Show the dashboard with the user's own stores.
- */
- public function index(Request $request): Response
- {
- return Inertia::render('dashboard', [
- 'stores' => $request->user()
- ->stores()
- ->latest()
- ->get()
- ->map(fn (Store $store) => $this->present($store)),
- ]);
- }
-
- /**
- * Show the "create a store" form.
- */
- public function create(): Response
- {
- return Inertia::render('stores/create', [
- 'categories' => StoreCategory::options(),
- 'languages' => StoreLanguage::options(),
- ]);
- }
-
- /**
- * Save a new store for the current user.
- */
- public function store(CreateStoreRequest $request): RedirectResponse
- {
- $store = $request->user()->stores()->create($request->validated());
-
- Inertia::flash('toast', ['type' => 'success', 'message' => __('Store created.')]);
-
- return to_route('stores.show', $store->slug);
- }
-
- /**
- * Show one store. Looking it up through the user means other people's stores give a 404.
- */
- public function show(Request $request, string $slug): Response
- {
- $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
-
- return Inertia::render('stores/show', [
- 'store' => $this->present($store),
- ]);
- }
-
- /**
- * The store fields the frontend is allowed to see.
- *
- * @return array<string, mixed>
- */
- private function present(Store $store): array
- {
- return [
- 'id' => $store->id,
- 'name' => $store->name,
- 'slug' => $store->slug,
- 'category' => $store->category->value,
- 'category_label' => $store->category->label(),
- 'description' => $store->description,
- 'language' => $store->language->value,
- 'language_label' => $store->language->label(),
- 'status' => $store->status->value,
- 'status_label' => $store->status->label(),
- 'has_design' => $store->plan !== null,
- 'created_at' => $store->created_at?->toIso8601String(),
- ];
- }
- }
|