AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

StoreController.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, UsageLimits $usage): Response
  21. {
  22. return Inertia::render('dashboard', [
  23. 'usage' => $usage->for($request->user()),
  24. 'stores' => $request->user()
  25. ->stores()
  26. ->latest()
  27. ->get()
  28. ->map(fn (Store $store) => $this->present($store)),
  29. ]);
  30. }
  31. /**
  32. * Show the "create a store" form.
  33. */
  34. public function create(Request $request, UsageLimits $usage): Response|RedirectResponse
  35. {
  36. if (! $usage->canCreateStore($request->user())) {
  37. return $this->storeLimitReached($request, $usage);
  38. }
  39. return Inertia::render('stores/create', [
  40. 'categories' => StoreCategory::options(),
  41. 'languages' => StoreLanguage::options(),
  42. ]);
  43. }
  44. /**
  45. * Save a new store for the current user.
  46. */
  47. public function store(CreateStoreRequest $request, UsageLimits $usage): RedirectResponse
  48. {
  49. if (! $usage->canCreateStore($request->user())) {
  50. return $this->storeLimitReached($request, $usage);
  51. }
  52. $store = $request->user()->stores()->create($request->validated());
  53. Inertia::flash('toast', ['type' => 'success', 'message' => __('Store created.')]);
  54. return to_route('stores.show', $store->slug);
  55. }
  56. /**
  57. * Show one store. Looking it up through the user means other people's stores give a 404.
  58. */
  59. public function show(Request $request, UsageLimits $usage, string $slug): Response
  60. {
  61. $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
  62. StoreGeneration::failStale($store->id);
  63. $latest = $store->generations()->first();
  64. return Inertia::render('stores/show', [
  65. 'store' => $this->present($store),
  66. // The newest AI job. The page keeps refreshing while it is queued or running.
  67. 'generation' => $latest ? $this->presentGeneration($latest) : null,
  68. 'edits' => $store->generations()
  69. ->where('type', GenerationType::Edit)
  70. ->limit(5)
  71. ->get()
  72. ->map(fn (StoreGeneration $edit) => $this->presentGeneration($edit)),
  73. 'usage' => $usage->for($request->user()),
  74. ]);
  75. }
  76. /**
  77. * The plan does not allow another store. Send the user to the billing page with the reason.
  78. */
  79. private function storeLimitReached(Request $request, UsageLimits $usage): RedirectResponse
  80. {
  81. Inertia::flash('toast', ['type' => 'error', 'message' => $usage->hasPlan($request->user())
  82. ? __('Your plan does not allow more stores. Upgrade to add another.')
  83. : __('Your free trial has ended. Choose a plan to continue.')]);
  84. return to_route('billing');
  85. }
  86. /**
  87. * The store fields the frontend is allowed to see.
  88. *
  89. * @return array<string, mixed>
  90. */
  91. private function present(Store $store): array
  92. {
  93. return [
  94. 'id' => $store->id,
  95. 'name' => $store->name,
  96. 'slug' => $store->slug,
  97. 'category' => $store->category->value,
  98. 'category_label' => $store->category->label(),
  99. 'description' => $store->description,
  100. 'language' => $store->language->value,
  101. 'language_label' => $store->language->label(),
  102. 'status' => $store->status->value,
  103. 'status_label' => $store->status->label(),
  104. 'has_design' => $store->plan !== null,
  105. 'is_published' => $store->isPublished(),
  106. 'has_unpublished_changes' => $store->hasUnpublishedChanges(),
  107. 'published_at' => $store->published_at?->toIso8601String(),
  108. // Where shoppers open the store once it is published.
  109. 'live_url' => Storefront::liveUrl($store, request()),
  110. // Changes whenever the design is saved, so the preview knows to reload.
  111. 'design_version' => $store->updated_at?->getTimestamp(),
  112. 'created_at' => $store->created_at?->toIso8601String(),
  113. ];
  114. }
  115. /**
  116. * The AI job fields the frontend is allowed to see.
  117. *
  118. * @return array<string, mixed>
  119. */
  120. private function presentGeneration(StoreGeneration $generation): array
  121. {
  122. return [
  123. 'id' => $generation->id,
  124. 'type' => $generation->type->value,
  125. 'status' => $generation->status->value,
  126. 'instruction' => $generation->instruction,
  127. 'reply' => $generation->reply,
  128. 'error' => $generation->error,
  129. 'created_at' => $generation->created_at?->toIso8601String(),
  130. ];
  131. }
  132. }