Add job for function upload record to csv
This commit is contained in:
@@ -5,6 +5,7 @@ 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;
|
||||
@@ -14,28 +15,14 @@ class FileController extends Controller
|
||||
{
|
||||
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);
|
||||
|
||||
// Leggi il contenuto del file CSV
|
||||
$contenutoCSV = Storage::get('file_temp/new_file.csv');
|
||||
|
||||
// Analizza il contenuto CSV
|
||||
$righeCSV = str_getcsv($contenutoCSV, "\n");
|
||||
|
||||
// Converte le righe CSV in un array associativo
|
||||
$dati = [];
|
||||
foreach ($righeCSV as $riga) {
|
||||
$dati[] = str_getcsv($riga);
|
||||
}
|
||||
|
||||
// Converte l'array in formato JSON
|
||||
$json = json_encode($dati);
|
||||
|
||||
// Stampa il JSON
|
||||
echo $json;
|
||||
//get file path
|
||||
$filepath = storage_path('app/public/file_temp/new_file.csv');
|
||||
|
||||
dispatch(new ImportCSVFileJob($filepath));
|
||||
|
||||
return redirect('/words');
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ class FileRequest extends FormRequest
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'filetoinsert' => ['required', 'mimes:txt']//, File::types(['csv'])]
|
||||
'filetoinsert' => ['required', 'mimes:csv']//, File::types(['csv'])]
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
52
app/Jobs/ImportCSVFileJob.php
Normal file
52
app/Jobs/ImportCSVFileJob.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Bus\Dispatchable;
|
||||
use Illuminate\Queue\InteractsWithQueue;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
use App\Models\Word;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ImportCSVFileJob implements ShouldQueue
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $filepath;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*/
|
||||
public function __construct($filepath)
|
||||
{
|
||||
$this->filepath = $filepath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*/
|
||||
public function handle(): void
|
||||
{
|
||||
$mapping = [
|
||||
'name' => 0, 'translation' => 1,
|
||||
];
|
||||
$fileStream = fopen($this->filepath, 'r');
|
||||
$skipHeader = true;
|
||||
while ($row = fgetcsv($fileStream)) {
|
||||
if ($skipHeader) {
|
||||
$skipHeader = false;
|
||||
continue;
|
||||
}
|
||||
Word::updateOrCreate(
|
||||
[
|
||||
'name' => $row[$mapping['name']],
|
||||
'translation' => $row[$mapping['translation']],
|
||||
]
|
||||
);
|
||||
}
|
||||
fclose($fileStream);
|
||||
Storage::delete($this->filepath);
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ return [
|
||||
'driver' => 'database',
|
||||
'table' => 'jobs',
|
||||
'queue' => 'default',
|
||||
'retry_after' => 90,
|
||||
'retry_after' => 1250,
|
||||
'after_commit' => false,
|
||||
],
|
||||
|
||||
|
||||
@@ -15,9 +15,9 @@ class AdminUserSeeder extends Seeder
|
||||
public function run(): void
|
||||
{
|
||||
$superAdmin = User::create([
|
||||
'name' => 'Kagir',
|
||||
'email' => 'kagir.dev@gmail.com ',
|
||||
'password' => Hash::make('Prova123!')
|
||||
'name' => 'paolo',
|
||||
'email' => 'paolo.guagnano1986@gmail.com',
|
||||
'password' => Hash::make('paolo220186')
|
||||
]);
|
||||
$superAdmin->assignRole('ADMIN');
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
@props(['value'])
|
||||
|
||||
<label {{ $attributes->merge(['class' => 'block font-medium text-sm text-gray-700 dark:text-gray-300']) }}>
|
||||
<label {{ $attributes->merge(['class' => 'col-md-4 col-form-label text-md-end text-start']) }}>
|
||||
{{ $value ?? $slot }}
|
||||
</label>
|
||||
|
||||
{{--'block font-medium text-sm text-gray-700 dark:text-gray-300'--}}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
<button {{ $attributes->merge(['type' => 'submit', 'class' => 'inline-flex items-center px-4 py-2 bg-gray-800 dark:bg-gray-200 border border-transparent rounded-md font-semibold text-xs text-white dark:text-gray-800 uppercase tracking-widest hover:bg-gray-700 dark:hover:bg-white focus:bg-gray-700 dark:focus:bg-white active:bg-gray-900 dark:active:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 dark:focus:ring-offset-gray-800 transition ease-in-out duration-150']) }}>
|
||||
<button {{ $attributes->merge(['type' => 'submit', 'class' => 'col-md-3 offset-md-5 btn btn-primary']) }}>
|
||||
{{ $slot }}
|
||||
</button>
|
||||
|
||||
{{--inline-flex items-center px-4 py-2 bg-gray-800 dark:bg-gray-200 border border-transparent rounded-md font-semibold text-xs text-white dark:text-gray-800 uppercase tracking-widest hover:bg-gray-700 dark:hover:bg-white focus:bg-gray-700 dark:focus:bg-white active:bg-gray-900 dark:active:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 dark:focus:ring-offset-gray-800 transition ease-in-out duration-150--}}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
@props(['items' => []])
|
||||
@props(['fields' => []])
|
||||
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Italian Word</th>
|
||||
<th>Dialect Translation</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
||||
@forelse ($items as $item)
|
||||
<tr>
|
||||
<td>{{ $item->{$fields[0]} }}</td>
|
||||
<td>{{ $item->{$fields[1]} }}</td>
|
||||
<td> <a href="edit_record/{{ $item->id }}"><button class="btn btn-primary">Edit</button> </a> </td>
|
||||
<td> <a href="delete_record/{{ $item->id }}"><button class="btn btn-danger">Delete</button> </a> </td>
|
||||
</tr>
|
||||
@empty
|
||||
<tr>
|
||||
<td class="text-center" colspan="2">No words</td>
|
||||
</tr>
|
||||
@endforelse
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -1,5 +1,7 @@
|
||||
<section class="space-y-6">
|
||||
<header>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<div>
|
||||
<h2 class="text-lg font-medium text-gray-900 dark:text-gray-100">
|
||||
{{ __('Delete Account') }}
|
||||
</h2>
|
||||
@@ -7,8 +9,10 @@
|
||||
<p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
|
||||
{{ __('Once your account is deleted, all of its resources and data will be permanently deleted. Before deleting your account, please download any data or information that you wish to retain.') }}
|
||||
</p>
|
||||
</header>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card-body">
|
||||
<x-danger-button
|
||||
x-data=""
|
||||
x-on:click.prevent="$dispatch('open-modal', 'confirm-user-deletion')"
|
||||
@@ -52,4 +56,5 @@
|
||||
</div>
|
||||
</form>
|
||||
</x-modal>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
<section>
|
||||
<header>
|
||||
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<div>
|
||||
<h2 class="text-lg font-medium text-gray-900 dark:text-gray-100">
|
||||
{{ __('Update Password') }}
|
||||
</h2>
|
||||
@@ -7,30 +9,38 @@
|
||||
<p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
|
||||
{{ __('Ensure your account is using a long, random password to stay secure.') }}
|
||||
</p>
|
||||
</header>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card-body">
|
||||
<form method="post" action="{{ route('password.update') }}" class="mt-6 space-y-6">
|
||||
@csrf
|
||||
@method('put')
|
||||
|
||||
|
||||
<div>
|
||||
<div class="mb-3 row">
|
||||
<x-input-label for="update_password_current_password" :value="__('Current Password')" />
|
||||
<div class="col-md-6">
|
||||
<x-text-input :notvalid="$errors->updatePassword->has('current_password')" id="update_password_current_password" name="current_password" type="password" class="mt-1 block w-full" autocomplete="current-password" />
|
||||
<x-input-error :messages="$errors->updatePassword->get('current_password')" class="mt-2" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div class="mb-3 row">
|
||||
<x-input-label for="update_password_password" :value="__('New Password')" />
|
||||
<div class="col-md-6">
|
||||
<x-text-input :notvalid="$errors->updatePassword->has('password')" id="update_password_password" name="password" type="password" class="mt-1 block w-full" autocomplete="new-password" />
|
||||
<x-input-error :messages="$errors->updatePassword->get('password')" class="mt-2" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div class="mb-3 row">
|
||||
<x-input-label for="update_password_password_confirmation" :value="__('Confirm Password')" />
|
||||
<div class="col-md-6">
|
||||
<x-text-input :notvalid="$errors->updatePassword->has('password_confirmation')" id="update_password_password_confirmation" name="password_confirmation" type="password" class="mt-1 block w-full" autocomplete="new-password" />
|
||||
<x-input-error :messages="$errors->updatePassword->get('password_confirmation')" class="mt-2" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<x-primary-button>{{ __('Save') }}</x-primary-button>
|
||||
@@ -46,4 +56,5 @@
|
||||
@endif
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<section>
|
||||
<header>
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<div>
|
||||
<h2 class="text-lg font-medium text-gray-900 dark:text-gray-100">
|
||||
{{ __('Profile Information') }}
|
||||
</h2>
|
||||
@@ -7,24 +8,32 @@
|
||||
<p class="mt-1 text-sm text-gray-600 dark:text-gray-400">
|
||||
{{ __("Update your account's profile information and email address.") }}
|
||||
</p>
|
||||
</header>
|
||||
</div>
|
||||
<div class="float-end">
|
||||
<a href="{{ route('words.index') }}" class="btn btn-primary"> Back</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form id="send-verification" method="post" action="{{ route('verification.send') }}">
|
||||
@csrf
|
||||
</form>
|
||||
|
||||
<div class="card-body">
|
||||
<form method="post" action="{{ route('profile.update') }}" class="mt-6 space-y-6">
|
||||
@csrf
|
||||
@method('patch')
|
||||
|
||||
<div>
|
||||
<div class="mb-3 row">
|
||||
<x-input-label for="name" :value="__('Name')" />
|
||||
<div class="col-md-6">
|
||||
<x-text-input id="name" name="name" type="text" class="mt-1 block w-full" :value="old('name', $user->name)" required autofocus autocomplete="name" />
|
||||
<x-input-error class="mt-2" :messages="$errors->get('name')" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div class="mb-3 row">
|
||||
<x-input-label for="email" :value="__('Email')" />
|
||||
<div class="col-md-6">
|
||||
<x-text-input id="email" name="email" type="email" class="mt-1 block w-full" :value="old('email', $user->email)" required autocomplete="username" />
|
||||
<x-input-error class="mt-2" :messages="$errors->get('email')" />
|
||||
|
||||
@@ -46,6 +55,7 @@
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-4">
|
||||
<x-primary-button>{{ __('Save') }}</x-primary-button>
|
||||
@@ -61,4 +71,5 @@
|
||||
@endif
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user