AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StoreGenerationController.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Billing\UsageLimits;
  4. use App\Enums\GenerationStatus;
  5. use App\Enums\GenerationType;
  6. use App\Jobs\RunStoreGeneration;
  7. use App\Models\Store;
  8. use App\Models\StoreGeneration;
  9. use Illuminate\Http\RedirectResponse;
  10. use Illuminate\Http\Request;
  11. use Inertia\Inertia;
  12. /**
  13. * Starts AI jobs for a store. The work itself happens in the background (RunStoreGeneration).
  14. */
  15. class StoreGenerationController extends Controller
  16. {
  17. /**
  18. * Design the whole store with AI. The first design is free; later ones count as regenerations.
  19. */
  20. public function generate(Request $request, UsageLimits $usage, string $slug): RedirectResponse
  21. {
  22. $store = $this->ownedStore($request, $slug);
  23. $isRegeneration = $store->plan !== null;
  24. if ($this->isBusy($store)) {
  25. return $this->refuse($store, __('Your store is already being worked on. Please wait for it to finish.'));
  26. }
  27. if ($isRegeneration && ! $usage->canRegenerate($request->user())) {
  28. return $this->refuse($store, __('You have used all the regenerations in your plan.'));
  29. }
  30. $this->start($request, $store, [
  31. 'type' => GenerationType::Generate,
  32. 'is_regeneration' => $isRegeneration,
  33. ]);
  34. return to_route('stores.show', $store->slug);
  35. }
  36. /**
  37. * Ask the AI for one small change, in plain words.
  38. */
  39. public function edit(Request $request, UsageLimits $usage, string $slug): RedirectResponse
  40. {
  41. $store = $this->ownedStore($request, $slug);
  42. abort_if($store->plan === null, 404);
  43. $validated = $request->validate([
  44. 'instruction' => ['required', 'string', 'min:3', 'max:500'],
  45. ]);
  46. if ($this->isBusy($store)) {
  47. return $this->refuse($store, __('Your store is already being worked on. Please wait for it to finish.'));
  48. }
  49. if (! $usage->canEdit($request->user())) {
  50. return $this->refuse($store, __('You have used all the AI edits in your plan.'));
  51. }
  52. $this->start($request, $store, [
  53. 'type' => GenerationType::Edit,
  54. 'instruction' => $validated['instruction'],
  55. ]);
  56. return to_route('stores.show', $store->slug);
  57. }
  58. /**
  59. * @param array<string, mixed> $attributes
  60. */
  61. private function start(Request $request, Store $store, array $attributes): void
  62. {
  63. $generation = $store->generations()->create([
  64. ...$attributes,
  65. 'user_id' => $request->user()->id,
  66. 'status' => GenerationStatus::Queued,
  67. ]);
  68. RunStoreGeneration::dispatch($generation->id);
  69. }
  70. /**
  71. * One AI job per store at a time.
  72. */
  73. private function isBusy(Store $store): bool
  74. {
  75. StoreGeneration::failStale($store->id);
  76. return $store->generations()->active()->exists();
  77. }
  78. private function refuse(Store $store, string $message): RedirectResponse
  79. {
  80. Inertia::flash('toast', ['type' => 'error', 'message' => $message]);
  81. return to_route('stores.show', $store->slug);
  82. }
  83. private function ownedStore(Request $request, string $slug): Store
  84. {
  85. return $request->user()->stores()->where('slug', $slug)->firstOrFail();
  86. }
  87. }