NotificationController.php 1.13 KB
<?php

namespace App\Http\Container\AppSection\Controllers;

use App\Helpers\JsonResource;
use App\Models\Notification;
use App\Models\NotificationUser;
use App\Support\Controller;
use Auth;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    /**
     * @param \App\Models\Notification $notification
     * @return \Illuminate\Contracts\View\View
     */
    public function show(Notification $notification): View
    {
        return view('notification', $notification);
    }

    /**
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(Request $request): JsonResponse
    {
        $request->validate(['ids' => 'required|array', 'ids.*' => 'numeric'], ['*.required' => '参数异常']);

        NotificationUser::query()
            ->where('user_id', Auth::id())->whereIn('notification_id', $request->get('ids'))
            ->whereNull('read_at')->update(['read_at' => now()->toDateTimeString()]);

        return JsonResource::success(JsonResource::UPDATE_SUCCESS);
    }
}