Skip to content

Commit

Permalink
Added job to create ville cycle & added command to run it manually fo…
Browse files Browse the repository at this point in the history
…r tests purposes
  • Loading branch information
Thombrix committed Oct 29, 2023
1 parent 5de96bb commit ac0d1f2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
33 changes: 33 additions & 0 deletions backend-laravel/app/Console/Commands/DispatchCycleVille.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Jobs\CycleVille;

class DispatchCycleVille extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:dispatch-cycle-ville';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';

/**
* Execute the console command.
*/
public function handle()
{
CycleVille::dispatch();
$this->info('Cycle ville has been dispatched');
}
}
53 changes: 53 additions & 0 deletions backend-laravel/app/Jobs/CycleVille.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

use App\Models\Cycle;
use App\Models\Ville;

class CycleVille implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*/
public function __construct()
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
$cycles = Cycle::get();

if($cycles->isEmpty())
{
Cycle::generateCycle();
return;
}

$cycle = $cycles->first();

$cycle->index += 1;

if($cycle->index >= $cycle->count)
{
$cycle->delete();
Cycle::generateCycle();
return;
}

$cycle->save();
}
}
21 changes: 20 additions & 1 deletion backend-laravel/app/Models/Cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,31 @@

class Cycle extends Model
{
use HasFactory;

protected $fillable = [
'shuffledList',
'index',
'count'
];

use HasFactory;
private static $delimiter = ";";

public static function generateCycle()
{
$villes_ids = Ville::get()->pluck('id')->toArray();

shuffle($villes_ids);

$formatted_ids = implode(self::$delimiter, $villes_ids);

$count = count($villes_ids);
$index = 0;

return Cycle::create(array(
'shuffledList' => $formatted_ids,
'index' => $index,
'count' => $count
));
}
}

0 comments on commit ac0d1f2

Please sign in to comment.