ProductController.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\Import\ImportCatalog;
  4. use App\Models\Product;
  5. use Exception;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Storage;
  9. use Illuminate\Support\Str;
  10. class ProductController extends Controller
  11. {
  12. protected array $data = [
  13. 'active' => 'catalog',
  14. 'title' => 'Каталог',
  15. ];
  16. public function index()
  17. {
  18. $this->data['products'] = Product::query()->paginate();
  19. return view('catalog.index', $this->data);
  20. }
  21. public function show()
  22. {
  23. }
  24. public function import(Request $request)
  25. {
  26. // validate data
  27. $request->validate([
  28. 'year' => 'required|integer|min:2000|max:' . (int)date('Y', strtotime('next year')),
  29. 'import_file' => 'file',
  30. ]);
  31. // load and save file
  32. try {
  33. $path = Str::random(2) . '/' . Str::uuid() . '.' .$request->file('import_file')->getClientOriginalExtension();
  34. Storage::disk('upload')->put($path, $request->file('import_file')->getContent());
  35. } catch (Exception $e) {
  36. Log::error('[UPLOAD ERROR]' . $e->getMessage());
  37. return view('catalog.index')->with(['danger' => 'Не удалось сохранить файл!']);
  38. }
  39. // todo validate header
  40. // dispatch job
  41. ImportCatalog::dispatch($request->year, $path);
  42. Log::info('ImportCatalog job created!');
  43. return redirect()->route('catalog.index')->with(['success' => 'Задача импорта успешно создана!']);
  44. }
  45. }