backend notifications implementation

This commit is contained in:
Čarodej
2022-03-10 11:49:02 +01:00
parent 70d7f2f5bd
commit 64e80d387b
20 changed files with 617 additions and 280 deletions
@@ -0,0 +1,23 @@
<?php
namespace Domain\Notifications\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
class FlushUserNotificationsController extends Controller
{
public function __invoke(): Response|Application|ResponseFactory
{
if (is_demo_account()) {
return response('Done', 204);
}
// Delete all notifications
auth()->user()->notifications()->delete();
return response('Done', 204);
}
}
@@ -0,0 +1,16 @@
<?php
namespace Domain\Notifications\Controllers;
use App\Http\Controllers\Controller;
use Domain\Notifications\Resources\NotificationCollection;
class GetUserNotificationsController extends Controller
{
public function __invoke(): NotificationCollection
{
return new NotificationCollection(
auth()->user()->notifications
);
}
}
@@ -0,0 +1,23 @@
<?php
namespace Domain\Notifications\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
class MarkUserNotificationsAsReadController extends Controller
{
public function __invoke(): Response|Application|ResponseFactory
{
if (is_demo_account()) {
return response('Done', 204);
}
// Mark all notifications as read
auth()->user()->unreadNotifications()->update(['read_at' => now()]);
return response('Done', 204);
}
}
@@ -0,0 +1,16 @@
<?php
namespace Domain\Notifications\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class NotificationCollection extends ResourceCollection
{
public $collects = NotificationResource::class;
public function toArray($request): array
{
return [
'data' => $this->collection,
];
}
}
@@ -0,0 +1,32 @@
<?php
namespace Domain\Notifications\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class NotificationResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'data' => [
'id' => $this->id,
'type' => $this->type,
'attributes' => [
'type' => $this->data['type'],
'title' => $this->data['title'],
'description' => $this->data['description'],
'action' => $this->data['action'] ?? null,
'created_at' => format_date($this->created_at, 'd. M. Y h:i'),
'read_at' => $this->read_at ? format_date($this->read_at, 'd. M. Y h:i') : null,
],
],
];
}
}