| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\UserNotification;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- class UserNotificationController extends Controller
- {
- protected array $data = [
- 'active' => 'notifications',
- 'title' => 'Уведомления',
- 'id' => 'notifications',
- 'header' => [
- 'created_at' => 'Дата',
- 'type' => 'Тип',
- 'event' => 'Событие',
- 'message' => 'Сообщение',
- 'read_at' => 'Прочитано',
- ],
- 'searchFields' => [
- 'message',
- ],
- 'ranges' => [],
- 'filters' => [],
- ];
- public function index(Request $request)
- {
- session(['gp_notifications' => $request->query()]);
- $model = new UserNotification;
- $userId = $request->user()->id;
- $q = $model::query()->where('user_id', $userId);
- $this->data['filters']['type'] = [
- 'title' => 'Тип',
- 'values' => UserNotification::TYPE_NAMES,
- ];
- $this->data['filters']['event'] = [
- 'title' => 'Событие',
- 'values' => UserNotification::EVENT_NAMES,
- ];
- $this->createDateFilters($model, 'created_at');
- $this->acceptFilters($q, $request);
- $this->acceptSearch($q, $request);
- $this->setSortAndOrderBy($model, $request);
- $this->applyStableSorting($q);
- $this->data['notifications'] = $q->paginate($this->data['per_page'])->withQueryString();
- return view('notifications.index', $this->data);
- }
- public function markRead(Request $request, UserNotification $notification): JsonResponse
- {
- if ($notification->user_id !== $request->user()->id) {
- abort(403);
- }
- if (!$notification->isRead()) {
- $notification->update(['read_at' => now()]);
- }
- return response()->json([
- 'ok' => true,
- 'unread' => UserNotification::query()
- ->where('user_id', $request->user()->id)
- ->whereNull('read_at')
- ->count(),
- ]);
- }
- public function unreadCount(Request $request): JsonResponse
- {
- return response()->json([
- 'count' => UserNotification::query()
- ->where('user_id', $request->user()->id)
- ->whereNull('read_at')
- ->count(),
- ]);
- }
- }
|