AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

StoreController.php 3.9KB

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