Skip to content

Commit

Permalink
Merge pull request #4 from hanisirfan/visitor_delete
Browse files Browse the repository at this point in the history
Visitor delete
  • Loading branch information
hanisirfan committed Nov 20, 2022
2 parents d26a708 + 5c8aaf5 commit 6f3f4be
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 8 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,38 @@

`php artisan install PASSWORD USER_NAME EMAIL_ADDRESS`

## Deleting Visitors By Its Age

You can remove visitors that have a certain age (in seconds) after their addition to the system. You can do that by running the artisan `deleteVisitorByAge` command. Run `php artisan help deleteVisitorByAge` for more info.

To schedule `deleteVisitorByAge` command, you need to define it in `app/Console/Kernel.php` file's schedule method. For example:
```
protected function schedule(Schedule $schedule)
{
$schedule->command('deleteVisitorByAge --days=3')
->daily()
->runInBackground();
}
```

This will schedule the deletion of visitors that are 3 days old every day at midnight.

To running the actual Laravel Scheduler, add a single cron entry to your server to run `schedule:run` command every minute.

```
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
```

Refer [this article](https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/) on how to add Cron entries in Linux.

Refer [Laravel documentation](https://laravel.com/docs/9.x/scheduling#scheduling-artisan-commands) for more info in task scheduling.

### Logs

All logs of `deleteVisitorByAge` command are stored in `storage/logs/deleteVisitorByAgeCommand.log` file.

## Copyright

Copyright (c) 2022 Muhammad Hanis Irfan Bin Mohd Zaid a.k.a. Hanis Irfan.
Copyright (c) 2022 Muhammad Hanis Irfan Bin Mohd Zaid.

Licensed under the MIT license.
106 changes: 106 additions & 0 deletions app/Console/Commands/DeleteVisitorByAge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use App\Models\Visitor;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class DeleteVisitorByAge extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'deleteVisitorByAge
{--S|seconds= : Delete visitors with X minutes of age.}
{--m|minutes= : Delete visitors with X minutes of age.}
{--H|hours= : Delete visitors with X hours of age.}
{--D|days= : Delete visitors with X days of age.}
{--W|weeks= : Delete visitors with X weeks of age.}
';

/**
* The console command description.
*
* @var string
*/
protected $description = 'visitorqr: Delete visitors based on age after they are added to the system.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
if (!empty($this->option('seconds'))) {
$optionSeconds = $this->option('seconds');
} else {
$optionSeconds = 0;
}

if (!empty($this->option('minutes'))) {
$optionMinutes = $this->option('minutes');
$optionMinutes = $optionMinutes * 60;
} else {
$optionMinutes = 0;
}

if (!empty($this->option('hours'))) {
$optionHours = $this->option('hours');
$optionHours = ($optionHours * 60) * 60;
} else {
$optionHours = 0;
}

if (!empty($this->option('days'))) {
$optionDays = $this->option('days');
$optionDays = (($optionDays * 24) * 60) * 60;
} else {
$optionDays = 0;
}

if (!empty($this->option('weeks'))) {
$optionWeeks = $this->option('weeks');
$optionWeeks = ((($optionWeeks * 7) * 24) * 60) * 60;
} else {
$optionWeeks = 0;
}

$totalAgeInSeconds = $optionSeconds + $optionMinutes + $optionHours + $optionDays + $optionWeeks;

$this->info('<fg=black;bg=white>Total age limit in seconds: ' . $totalAgeInSeconds);
Log::channel('deleteVisitorByAgeCommand')->info('Total age limit in seconds: ' . $totalAgeInSeconds);

if (Visitor::first()) {

$visitors = Visitor::select('uuid', 'created_at')->get();

$now = Carbon::now();
foreach ($visitors as $visitor) {
$visitorAgeInSeconds = $visitor->created_at->diffInSeconds($now);
$this->info('-----------------------------------------------------------');
$this->info('Visitor UUID: ' . $visitor->uuid);
$this->info('Visitor Created At: ' . $visitor->created_at);
$this->info('Visitor Age In Seconds: ' . $visitorAgeInSeconds);

// Deleting visitor if the age (in seconds) is more than the limit defined when running the command.
if ($visitorAgeInSeconds > $totalAgeInSeconds) {

Visitor::where('uuid', $visitor->uuid)->delete();
$this->info('<fg=black;bg=white>Deleted visitor with UUID: ' . $visitor->uuid);
Log::channel('deleteVisitorByAgeCommand')->info('Deleted visitor with UUID: ' . $visitor->uuid);
}
$this->info('-----------------------------------------------------------');
}
} else {
$this->info('No visitors found.');
Log::channel('deleteVisitorByAgeCommand')->info('No visitors found.');
}

return Command::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Install extends Command
*
* @var string
*/
protected $description = 'formqr: Use composer compile-project to compile the system assets. Use this command to perform other actions (DB migration, creating admin user etc).';
protected $description = 'visitorqr: Use composer compile-project to compile the system assets. Use this command to perform other actions (DB migration, creating admin user etc).';

/**
* Create a new command instance.
Expand Down
2 changes: 1 addition & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')->hourly();

}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct()
public function index(Request $request)
{
if ($request->isMethod('get')) {
$pagination = 5;
$pagination = 7;

$visitors = Visitor::orderBy('created_at', 'DESC')->paginate($pagination)->withQueryString();

Expand Down
21 changes: 21 additions & 0 deletions app/Http/Controllers/VisitorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,25 @@ public function create(Request $request)
return view('visitors.add');
}
}


public function delete(Request $request) {
if ($request->isMethod('post')) {

$validated = $request->validate([
'delete-visitor-uuid' => ['required', 'uuid'],
]);

$visitorUuid = $request->input('delete-visitor-uuid');
Visitor::destroy($visitorUuid);

$request->session()->flash('deleteVisitorSuccess', 'Visitor successfully deleted!');
$request->session()->flash('uuid', $visitorUuid);

return back();
} else {
return redirect()->route('home');
}
}

}
54 changes: 54 additions & 0 deletions app/View/Components/Visitors/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\View\Components\Visitors;

use App\Models\Visitor;
use DateTime;
use Illuminate\View\Component;

class Delete extends Component
{
/**
* Visitor UUID.
*
* @var uuid
*/
public $visitorUuid;

/**
* Visitor Name.
*
* @var String
*/
public $visitorName;

/**
* Visitor Access Date and Time.
*
* @var DateTime
*/
public $visitorDateTime;

/**
* Create a new component instance.
*
* @param uuid $visitorUuid
* @return void
*/
public function __construct($visitorUuid)
{
$this->visitorUuid = $visitorUuid;
$this->visitorName = Visitor::select('name')->where('uuid', $visitorUuid)->first()->name;
$this->visitorDateTime = Visitor::select('visit_datetime')->where('uuid', $visitorUuid)->first()->date_time;
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.visitors.delete');
}
}
6 changes: 6 additions & 0 deletions config/logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
'level' => env('LOG_LEVEL', 'debug'),
],

'deleteVisitorByAgeCommand' => [
'driver' => 'single',
'path' => storage_path('logs/deleteVisitorByAgeCommand.log'),
'level' => env('LOG_LEVEL', 'debug'),
],

'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
Expand Down
34 changes: 34 additions & 0 deletions resources/views/components/visitors/delete.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<div>
<a class="btn btn-danger text-light" href="" data-bs-toggle="modal" data-bs-target="#{{ 'delete-visitor-modal-' . $visitorUuid}}"><i class="bi bi-trash"></i></a>

<div class="modal text-dark fade" id="{{ 'delete-visitor-modal-' . $visitorUuid}}" tabindex="-1" aria-labelledby="{{ 'delete-visitor-modal-' . $visitorUuid . '-label'}}" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{ route('visitors.delete') }}" method="post">
@csrf
<div class="modal-header">
<h5 class="modal-title" id="{{ 'delete-user-modal-' . $visitorUuid . '-label'}}">{{ __('Delete Visitor') }}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>{{ __('You sure want to delete this visitors.') }}</p>

<p class="fw-bold">{{ __('UUID') }}: <span class="fw-normal">{{ $visitorUuid }}</span></p>
<p class="fw-bold">{{ __('Name') }}: <span class="fw-normal">{{ $visitorName }}</span></p>
<p class="fw-bold">{{ __('Access Date & Time') }}:
<span class="fw-normal">
<x-carbon :date="$visitorDateTime" format="d/m/Y h:i A" />
</span>
</p>

<input type="text" name="delete-visitor-uuid" id="delete-visitor-uuid" value="{{ $visitorUuid }}" hidden>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{{ __('Cancel') }}</button>
<button type="submit" class="btn btn-danger"><i class="bi bi-trash"></i> {{ __('Yes') }}</button>
</div>
</form>
</div>
</div>
</div>
</div>
17 changes: 13 additions & 4 deletions resources/views/home.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,17 @@
<div class="col-md-8">
<div class="card">
<div class="card-header"><i class="bi bi-list"></i> {{ __('Visitor List') }}</div>

<div class="card-body">

@if(session()->has('deleteVisitorSuccess'))
<span>
<div class="alert alert-success w-100 ml-1">
<p class="fw-bold">{{ session('deleteVisitorSuccess') }}</p>
<p class="fw-bold">{{ __('UUID') }}: <span class="fw-normal">{{ session('uuid') }}</span></p>
</div>
</span>
@endif

<div class="table-responsive">
<table class="table table-light table-bordered table-hover">
@if ($visitors->count() > 0)
Expand Down Expand Up @@ -43,16 +52,16 @@
<a href="{{ route('visitors.view', $visitor->uuid) }}" class="btn btn-dark"><i class="bi bi-eye"></i></a>
</td>
<td>
<a href="" class="btn btn-dark"><i class="bi bi-pen"></i></a>
<a href="" class="btn btn-secondary"><i class="bi bi-pen"></i></a>
</td>
<td>
<a href="" class="btn btn-danger"><i class="bi bi-trash"></i></a>
<x-visitors.delete :visitor-uuid="$visitor->uuid"/>
</td>
</tr>
@endforeach
</tbody>
@else
<p>No visitors added.</p>
<p>{{ __('No visitors found.') }}</p>
@endif
</table>
{{ $visitors->links() }}
Expand Down
2 changes: 2 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
Route::get('/visitors/view/{uuid}', [VisitorController::class, 'index'])->name('visitors.view');
Route::get('/visitors/add', [VisitorController::class, 'create'])->name('visitors.add');
Route::post('/visitors/add', [VisitorController::class, 'create']);
Route::get('/visitors/delete', [VisitorController::class, 'delete'])->name('visitors.delete');
Route::post('/visitors/delete', [VisitorController::class, 'delete']);

Route::get('/scanner', [ScannerController::class, 'index'])->name('scanner');

0 comments on commit 6f3f4be

Please sign in to comment.