65 lines
1.5 KiB
PHP
65 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Alert;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
class AlertController extends Controller
|
|
{
|
|
/**
|
|
* Get alerts for user
|
|
*
|
|
* `Alert`-lar, dine api dereje gosulyar, `mobile app`-lar dine `GET` edip alyp gorkezip bilyarler `app`-a girende.
|
|
*/
|
|
public function index(): JsonResponse
|
|
{
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder<Alert> */
|
|
$alerstQuery = $user->alerts()->whereNull('seen_at');
|
|
|
|
/** @var Collection<array-key, Alert> */
|
|
$alerts = $alerstQuery->get();
|
|
|
|
$alerstQuery->update([
|
|
'seen_at' => now(),
|
|
]);
|
|
|
|
return response()->json($this->format($alerts));
|
|
}
|
|
|
|
/**
|
|
* All alerts
|
|
*/
|
|
public function all(): JsonResponse
|
|
{
|
|
/** @var \App\Models\User */
|
|
$user = auth()->user();
|
|
|
|
/** @var \Illuminate\Database\Eloquent\Builder<Alert> */
|
|
$alerstQuery = $user->alerts();
|
|
|
|
/** @var Collection<array-key, Alert> */
|
|
$alerts = $alerstQuery->get();
|
|
|
|
return response()->json($this->format($alerts));
|
|
}
|
|
|
|
/**
|
|
* Format
|
|
*
|
|
* @param Collection<array-key, Alert> $alerts
|
|
* @return Collection<array-key, Alert>
|
|
*/
|
|
public function format(Collection $alerts): Collection
|
|
{
|
|
return $alerts->map(fn (Alert $alert) => [
|
|
'name' => $alert->name,
|
|
'value' => $alert->value,
|
|
]);
|
|
}
|
|
}
|