Adjust class file controller

This commit is contained in:
paoloGuagnano
2024-02-26 16:32:53 +01:00
parent 4471b5e509
commit ac5fdc3909
4 changed files with 18 additions and 20 deletions

View File

@@ -3,28 +3,31 @@
namespace App\Http\Controllers;
use App\Http\Requests\FileRequest;
use App\Http\Requests\StorefileRequest;
use App\Http\Requests\UpdatefileRequest;
use App\Jobs\ImportCSVFileJob;
use App\Models\file;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class FileController extends Controller
{
public function __construct()
{
$this->middleware('auth');
$this->middleware('role:ADMIN');
}
public function uploadFile(FileRequest $request): RedirectResponse
{
//save file in storage/app/public/file_temp
$filename = 'new_file.'.$request->file("filetoinsert")->getClientOriginalExtension();
$request->file("filetoinsert")->storeAs('file_temp', $filename);
$request->file("filetoinsert")->storeAs($filename);
//get file path
$filepath = storage_path('app/public/file_temp/new_file.csv');
$filepath = storage_path('app/public/new_file.csv');
dispatch(new ImportCSVFileJob($filepath));
return redirect('/words');
return redirect('/words')->withSuccess('File saved successfully.');
}
public function insertFile()

View File

@@ -34,7 +34,7 @@ class WordsController extends Controller
$word->translation = $request->translation;
$word->save();
return redirect('/words');
return redirect('/words')->withSuccess('The Italian word and its translation have been successfully saved.');
}
/**
@@ -78,7 +78,7 @@ class WordsController extends Controller
$data->save();
// Redirect to a success route or return a success message
return redirect('/words');
return redirect('/words')->withSuccess('The Italian word and its translation has been successfully changed.');
} else {
// Unique constraint violated
return back()->withInput()->withErrors(['name' => 'Error: words already exists']);
@@ -91,6 +91,6 @@ class WordsController extends Controller
public function delete($id): RedirectResponse
{
Word::destroy($id);
return redirect('/words');
return redirect('/words')->withSuccess('The Italian word and its translation has been successfully deleted.');
}
}

View File

@@ -45,15 +45,10 @@ class ImportCSVFileJob implements ShouldQueue
'name' => $row[$mapping['name']],
'translation' => $row[$mapping['translation']],
);
//Word::updateOrCreate(
// [
// 'name' => $row[$mapping['name']],
// 'translation' => $row[$mapping['translation']],
// ]
//);
}
DB::table('words')->upsert($finalarray, 'name', 'translation');
//fclose($fileStream);
//Storage::delete($this->filepath);
//insert multy-record in DB
DB::table('words')->upsert($finalarray, 'name');
//delete file from storage after upsert
Storage::delete('new_file.csv');
}
}

View File

@@ -12,5 +12,5 @@ class Word extends Model
{
use HasFactory;
protected $fillable = ['name', 'translation'];
public $timestamps = true;
}