AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

StorePublishController.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Billing\UsageLimits;
  4. use App\Enums\StoreStatus;
  5. use App\Models\Store;
  6. use App\StorePlan\PlanValidator;
  7. use Illuminate\Http\RedirectResponse;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Validation\ValidationException;
  10. use Inertia\Inertia;
  11. /**
  12. * Puts a store live for shoppers, updates the live copy, or takes the store offline.
  13. */
  14. class StorePublishController extends Controller
  15. {
  16. /**
  17. * Publish the store, or publish the latest design changes of a store that is already live.
  18. * Shoppers see a saved copy of the design, taken at this moment.
  19. */
  20. public function publish(Request $request, PlanValidator $validator, UsageLimits $usage, string $slug): RedirectResponse
  21. {
  22. $store = $this->ownedStore($request, $slug);
  23. abort_if($store->plan === null, 404);
  24. if (! $usage->hasPlan($request->user())) {
  25. return $this->flash($store, 'error', __('Your free trial has ended. Choose a plan to publish your store.'));
  26. }
  27. try {
  28. // Checked once more, so nothing broken can ever go live.
  29. $plan = $validator->validate($store->plan);
  30. } catch (ValidationException) {
  31. return $this->flash($store, 'error', __('This design has a problem and cannot be published. Please regenerate it.'));
  32. }
  33. $wasLive = $store->isPublished();
  34. $store->published_plan = $plan;
  35. $store->published_at = now();
  36. $store->status = StoreStatus::Published;
  37. $store->save();
  38. return $this->flash($store, 'success', $wasLive ? __('Your changes are live.') : __('Your store is live.'));
  39. }
  40. /**
  41. * Take the store offline. The design, products and orders are all kept.
  42. */
  43. public function unpublish(Request $request, string $slug): RedirectResponse
  44. {
  45. $store = $this->ownedStore($request, $slug);
  46. $store->status = StoreStatus::Draft;
  47. $store->published_plan = null;
  48. $store->published_at = null;
  49. $store->save();
  50. return $this->flash($store, 'success', __('Your store is offline.'));
  51. }
  52. private function flash(Store $store, string $type, string $message): RedirectResponse
  53. {
  54. Inertia::flash('toast', ['type' => $type, 'message' => $message]);
  55. return to_route('stores.show', $store->slug);
  56. }
  57. private function ownedStore(Request $request, string $slug): Store
  58. {
  59. return $request->user()->stores()->where('slug', $slug)->firstOrFail();
  60. }
  61. }