AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

StoreController.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Billing\UsageLimits;
  4. use App\Enums\GenerationType;
  5. use App\Enums\StoreCategory;
  6. use App\Enums\StoreLanguage;
  7. use App\Http\Requests\Stores\CreateStoreRequest;
  8. use App\Models\Store;
  9. use App\Models\StoreGeneration;
  10. use App\Storefront\Storefront;
  11. use Illuminate\Http\RedirectResponse;
  12. use Illuminate\Http\Request;
  13. use Inertia\Inertia;
  14. use Inertia\Response;
  15. class StoreController extends Controller
  16. {
  17. /**
  18. * Show the dashboard with the user's own stores.
  19. */
  20. public function index(Request $request): Response
  21. {
  22. return Inertia::render('dashboard', [
  23. 'stores' => $request->user()
  24. ->stores()
  25. ->latest()
  26. ->get()
  27. ->map(fn (Store $store) => $this->present($store)),
  28. ]);
  29. }
  30. /**
  31. * Show the "create a store" form.
  32. */
  33. public function create(): Response
  34. {
  35. return Inertia::render('stores/create', [
  36. 'categories' => StoreCategory::options(),
  37. 'languages' => StoreLanguage::options(),
  38. ]);
  39. }
  40. /**
  41. * Save a new store for the current user.
  42. */
  43. public function store(CreateStoreRequest $request): RedirectResponse
  44. {
  45. $store = $request->user()->stores()->create($request->validated());
  46. Inertia::flash('toast', ['type' => 'success', 'message' => __('Store created.')]);
  47. return to_route('stores.show', $store->slug);
  48. }
  49. /**
  50. * Show one store. Looking it up through the user means other people's stores give a 404.
  51. */
  52. public function show(Request $request, UsageLimits $usage, string $slug): Response
  53. {
  54. $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
  55. StoreGeneration::failStale($store->id);
  56. $latest = $store->generations()->first();
  57. return Inertia::render('stores/show', [
  58. 'store' => $this->present($store),
  59. // The newest AI job. The page keeps refreshing while it is queued or running.
  60. 'generation' => $latest ? $this->presentGeneration($latest) : null,
  61. 'edits' => $store->generations()
  62. ->where('type', GenerationType::Edit)
  63. ->limit(5)
  64. ->get()
  65. ->map(fn (StoreGeneration $edit) => $this->presentGeneration($edit)),
  66. 'usage' => $usage->for($request->user()),
  67. ]);
  68. }
  69. /**
  70. * The store fields the frontend is allowed to see.
  71. *
  72. * @return array<string, mixed>
  73. */
  74. private function present(Store $store): array
  75. {
  76. return [
  77. 'id' => $store->id,
  78. 'name' => $store->name,
  79. 'slug' => $store->slug,
  80. 'category' => $store->category->value,
  81. 'category_label' => $store->category->label(),
  82. 'description' => $store->description,
  83. 'language' => $store->language->value,
  84. 'language_label' => $store->language->label(),
  85. 'status' => $store->status->value,
  86. 'status_label' => $store->status->label(),
  87. 'has_design' => $store->plan !== null,
  88. 'is_published' => $store->isPublished(),
  89. 'has_unpublished_changes' => $store->hasUnpublishedChanges(),
  90. 'published_at' => $store->published_at?->toIso8601String(),
  91. // Where shoppers open the store once it is published.
  92. 'live_url' => Storefront::liveUrl($store, request()),
  93. // Changes whenever the design is saved, so the preview knows to reload.
  94. 'design_version' => $store->updated_at?->getTimestamp(),
  95. 'created_at' => $store->created_at?->toIso8601String(),
  96. ];
  97. }
  98. /**
  99. * The AI job fields the frontend is allowed to see.
  100. *
  101. * @return array<string, mixed>
  102. */
  103. private function presentGeneration(StoreGeneration $generation): array
  104. {
  105. return [
  106. 'id' => $generation->id,
  107. 'type' => $generation->type->value,
  108. 'status' => $generation->status->value,
  109. 'instruction' => $generation->instruction,
  110. 'reply' => $generation->reply,
  111. 'error' => $generation->error,
  112. 'created_at' => $generation->created_at?->toIso8601String(),
  113. ];
  114. }
  115. }