| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace App\Http\Controllers;
- use App\Jobs\Import\ImportCatalog;
- use App\Models\Product;
- use Exception;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Str;
- class ProductController extends Controller
- {
- protected array $data = [
- 'active' => 'catalog',
- 'title' => 'Каталог',
- ];
- public function index()
- {
- $this->data['products'] = Product::query()->paginate();
- return view('catalog.index', $this->data);
- }
- public function show()
- {
- }
- public function import(Request $request)
- {
- // validate data
- $request->validate([
- 'year' => 'required|integer|min:2000|max:' . (int)date('Y', strtotime('next year')),
- 'import_file' => 'file',
- ]);
- // load and save file
- try {
- $path = Str::random(2) . '/' . Str::uuid() . '.' .$request->file('import_file')->getClientOriginalExtension();
- Storage::disk('upload')->put($path, $request->file('import_file')->getContent());
- } catch (Exception $e) {
- Log::error('[UPLOAD ERROR]' . $e->getMessage());
- return view('catalog.index')->with(['danger' => 'Не удалось сохранить файл!']);
- }
- // todo validate header
- // dispatch job
- ImportCatalog::dispatch($request->year, $path);
- Log::info('ImportCatalog job created!');
- return redirect()->route('catalog.index')->with(['success' => 'Задача импорта успешно создана!']);
- }
- }
|