Skip to content

Commit

Permalink
Added method to get the current ville & controller method today to re…
Browse files Browse the repository at this point in the history
…trieve it
  • Loading branch information
Thombrix committed Oct 29, 2023
1 parent ac0d1f2 commit f53e276
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
15 changes: 15 additions & 0 deletions backend-laravel/app/Http/Controllers/VilleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Cycle;

class VilleController extends Controller
{
public function today()
{
return Cycle::getCurrentVille();
}
}
22 changes: 22 additions & 0 deletions backend-laravel/app/Models/Cycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,26 @@ public static function generateCycle()
'count' => $count
));
}

public static function getCurrentVille()
{
// Get cycle
$cycles = self::get();

// If no cycle, generate one
$cycle = $cycles->isEmpty() ? self::generateCycle() : $cycles->first();

// Decode shuffledList
$villes_ids = explode(self::$delimiter, $cycle->shuffledList);

// Get the ville id according to the index of the cycle
$current_ville_id = $villes_ids[$cycle->index];

// Get the ville
$ville = Ville::where('id', $current_ville_id)->firstOrFail();

// Return it
return $ville;

}
}
4 changes: 4 additions & 0 deletions backend-laravel/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\VilleController;

/*
|--------------------------------------------------------------------------
| API Routes
Expand All @@ -17,3 +19,5 @@
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});

Route::get('/ville/today', [VilleController::class, 'today']);

0 comments on commit f53e276

Please sign in to comment.