feat: workouts calendar
This commit is contained in:
parent
2d6e4db390
commit
06c78f6eac
|
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Requests\WorkoutSessionsRequest;
|
use App\Http\Requests\WorkoutSessionsRequest;
|
||||||
|
use App\Http\Requests\WorkoutSessionsCalendarRequest;
|
||||||
use App\Http\Resources\WorkoutSessionsResource;
|
use App\Http\Resources\WorkoutSessionsResource;
|
||||||
use Carbon\CarbonImmutable;
|
use Carbon\CarbonImmutable;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
|
|
@ -32,6 +33,36 @@ class WorkoutSessionController extends Controller
|
||||||
return WorkoutSessionsResource::collection($workoutSessions);
|
return WorkoutSessionsResource::collection($workoutSessions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function calendar(WorkoutSessionsCalendarRequest $request): JsonResponse
|
||||||
|
{
|
||||||
|
$month = $request->validated('month') ?? now()->format('Y-m');
|
||||||
|
$referenceDate = CarbonImmutable::createFromFormat('!Y-m', $month);
|
||||||
|
$startDate = $referenceDate->startOfMonth();
|
||||||
|
$endDate = $referenceDate->endOfMonth();
|
||||||
|
|
||||||
|
$days = $request->user()
|
||||||
|
->workouts()
|
||||||
|
->whereBetween('started_at', [$startDate->startOfDay(), $endDate->endOfDay()])
|
||||||
|
->selectRaw('DATE(started_at) as date, COUNT(*) as workouts_count')
|
||||||
|
->groupByRaw('DATE(started_at)')
|
||||||
|
->orderBy('date')
|
||||||
|
->get()
|
||||||
|
->map(fn ($workoutSession): array => [
|
||||||
|
'date' => CarbonImmutable::parse((string) $workoutSession->date)->toDateString(),
|
||||||
|
'workoutsCount' => (int) $workoutSession->workouts_count,
|
||||||
|
])
|
||||||
|
->values();
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'data' => [
|
||||||
|
'month' => $referenceDate->format('Y-m'),
|
||||||
|
'startDate' => $startDate->toDateString(),
|
||||||
|
'endDate' => $endDate->toDateString(),
|
||||||
|
'days' => $days,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function store(WorkoutSessionsRequest $request): JsonResponse
|
public function store(WorkoutSessionsRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
$workoutSession = $request->user()
|
$workoutSession = $request->user()
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ Route::middleware(['auth:sanctum', 'verified'])->group(function (): void {
|
||||||
|
|
||||||
// Workouts
|
// Workouts
|
||||||
Route::middleware(['auth:sanctum', 'verified'])->group(function (): void {
|
Route::middleware(['auth:sanctum', 'verified'])->group(function (): void {
|
||||||
|
Route::get('workouts/calendar', [WorkoutSessionController::class, 'calendar'])->name('workouts.calendar');
|
||||||
Route::get('workouts', [WorkoutSessionController::class, 'index'])->name('workouts.index');
|
Route::get('workouts', [WorkoutSessionController::class, 'index'])->name('workouts.index');
|
||||||
Route::post('workouts', [WorkoutSessionController::class, 'store'])->name('workouts.store');
|
Route::post('workouts', [WorkoutSessionController::class, 'store'])->name('workouts.store');
|
||||||
Route::get('strava/status', [StravaController::class, 'status'])->name('strava.status');
|
Route::get('strava/status', [StravaController::class, 'status'])->name('strava.status');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue