feat: добавил личные уведомления для пользователей
🚀 Create and publish a Docker image / Detect changes in backend and frontend (push) Successful in 9s
🚀 Create and publish a Docker image / Build & publish backend image (push) Successful in 26s
🚀 Create and publish a Docker image / Build & publish frontend image (push) Successful in 19s
🚀 Create and publish a Docker image / Update stack on Portainer (push) Successful in 8s
🚀 Create and publish a Docker image / Detect changes in backend and frontend (push) Successful in 9s
🚀 Create and publish a Docker image / Build & publish backend image (push) Successful in 26s
🚀 Create and publish a Docker image / Build & publish frontend image (push) Successful in 19s
🚀 Create and publish a Docker image / Update stack on Portainer (push) Successful in 8s
Реализовал хранение, получение и отметку прочитанными пользовательских уведомлений. Обновил фронтенд для отображения и управления уведомлениями в профиле студента.
This commit is contained in:
@@ -1,20 +1,27 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UniVerse.Application.DTOs.Common;
|
||||
using UniVerse.Application.DTOs.Notifications;
|
||||
using UniVerse.Application.Interfaces;
|
||||
using UniVerse.Domain.Entities;
|
||||
using UniVerse.Infrastructure.Data;
|
||||
|
||||
namespace UniVerse.Infrastructure.Notifications;
|
||||
|
||||
public class NotificationService : INotificationService
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
private readonly IEnumerable<INotificationProvider> _providers;
|
||||
private readonly INotificationScheduler _scheduler;
|
||||
private readonly ILogger<NotificationService> _logger;
|
||||
|
||||
public NotificationService(
|
||||
AppDbContext db,
|
||||
IEnumerable<INotificationProvider> providers,
|
||||
INotificationScheduler scheduler,
|
||||
ILogger<NotificationService> logger)
|
||||
{
|
||||
_db = db;
|
||||
_providers = providers;
|
||||
_scheduler = scheduler;
|
||||
_logger = logger;
|
||||
@@ -46,4 +53,68 @@ public class NotificationService : INotificationService
|
||||
|
||||
return _scheduler.ScheduleAsync(message, request.SendAt, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<UserNotificationDto> CreateUserNotificationAsync(
|
||||
int userId,
|
||||
string type,
|
||||
string title,
|
||||
string body,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(type);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(title);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(body);
|
||||
|
||||
var notification = new UserNotification
|
||||
{
|
||||
UserId = userId,
|
||||
Type = type,
|
||||
Title = title,
|
||||
Body = body
|
||||
};
|
||||
|
||||
_db.UserNotifications.Add(notification);
|
||||
await _db.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return ToDto(notification);
|
||||
}
|
||||
|
||||
public async Task<PagedResult<UserNotificationDto>> GetUserNotificationsAsync(
|
||||
int userId,
|
||||
PaginationRequest pagination,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var query = _db.UserNotifications.Where(n => n.UserId == userId);
|
||||
var total = await query.CountAsync(cancellationToken);
|
||||
var items = await query
|
||||
.OrderByDescending(n => n.CreatedAt)
|
||||
.Skip((pagination.Page - 1) * pagination.PageSize)
|
||||
.Take(pagination.PageSize)
|
||||
.Select(n => new UserNotificationDto(
|
||||
n.Id,
|
||||
n.Type,
|
||||
n.Title,
|
||||
n.Body,
|
||||
n.IsRead,
|
||||
n.CreatedAt))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return PagedResult<UserNotificationDto>.Create(items, total, pagination.Page, pagination.PageSize);
|
||||
}
|
||||
|
||||
public async Task MarkAllReadAsync(int userId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _db.UserNotifications
|
||||
.Where(n => n.UserId == userId && !n.IsRead)
|
||||
.ExecuteUpdateAsync(setters => setters.SetProperty(n => n.IsRead, true), cancellationToken);
|
||||
}
|
||||
|
||||
private static UserNotificationDto ToDto(UserNotification notification) => new(
|
||||
notification.Id,
|
||||
notification.Type,
|
||||
notification.Title,
|
||||
notification.Body,
|
||||
notification.IsRead,
|
||||
notification.CreatedAt
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user