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

StorePublishController.php 2.2KB

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