Added SlidersStatusUpdate job

This commit is contained in:
Noah Lancien 2023-04-20 12:16:20 +00:00
parent 5326578fd2
commit 5e9446af99
3 changed files with 44 additions and 0 deletions

View file

@ -2,6 +2,7 @@
namespace App\Console;
use App\Jobs\SlidersStatusUpdate;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
@ -15,6 +16,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule->job(SlidersStatusUpdate::class)->everyMinute();
}
/**

View file

@ -0,0 +1,41 @@
<?php
namespace App\Jobs;
use App\Models\Slider;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
/**
* Update published and visibility attributes reading their
* publish_at and unpublish_at attributes.
*/
class SlidersStatusUpdate
{
/**
* Called when the job will be executed.
*
* @return void
*/
public function handle()
{
$slidersToPublish = Slider::where('publish_at', '<=', Carbon::now()->toDateTime())
->whereNotNull('publish_at')
->where('status', false)->get();
Log::debug(__CLASS__ . " sliders to publish " . $slidersToPublish->count());
foreach ($slidersToPublish as $slider) {
$slider->publish_at = null;
$slider->status = true;
$slider->saveOrFail();
}
$slidersToUnPublish = Slider::where('unpublish_at', '<=', Carbon::now()->toDateTime())
->whereNotNull('unpublish_at')
->where('status', true)->get();
Log::debug(__CLASS__ . " sliders to unpublish " . $slidersToPublish->count());
foreach ($slidersToUnPublish as $slider) {
$slider->unpublish_at = null;
$slider->status = false;
$slider->saveOrFail();
}
}
}

View file

@ -75,6 +75,7 @@ class Slider extends Model
*/
protected $casts = [
'type' => TypeEnum::class,
'status' => 'bool',
'publish_at' => 'datetime',
'unpublish_at' => 'datetime'
];