using Microsoft.EntityFrameworkCore; using eprintServer.Entities; using eprintServer.Responses; using eprintServer.Data; namespace eprintServer.Services; public class NotificationService { private readonly ILogger _logger; private readonly DataContext _context; public NotificationService( ILogger logger, DataContext context) { _logger = logger; _context = context; } public async Task SaveAsync(NotificationModel model) { try { if (model.Id == 0) { await _context.NotificationModels.AddAsync(model); } else { _context.NotificationModels.Update(model); } await _context.SaveChangesAsync(); return model; } catch (Exception ex) { _logger.LogError(ex, "Error saving notification model."); throw new Exception("Error saving data.", ex); } } public async Task ExistsByIdAsync(long id) { return await _context.NotificationModels.AnyAsync(n => n.Id == id); } public async Task FindByIdAsync(long id) { var notification = await _context.NotificationModels .FirstOrDefaultAsync(n => n.Id == id); if (notification == null) { //throw new NotFoundException($"Notification with id {id} not found."); } return notification; } public async Task> AllNotificationByUserAsync(int page, int size, User user) { if (size <= 0) size = 3; var query = _context.NotificationModels .Where(n => n.ReciverId == user.Id) .OrderByDescending(n => n.Timestmp); var totalCount = await query.CountAsync(); var resultlist = await query .Skip(page * size) .Take(size) .ToListAsync(); return new PagedResult(resultlist, totalCount, page, size); } public async Task AllUnreadNotificationByUserAsync(User user) { var unreadStatus = new List { states.WAITING, states.DELIVERED, states.SENDING }; return await _context.NotificationModels .CountAsync(n => n.ReciverId == user.Id && unreadStatus.Contains(n.Status)); } public async Task> GetUnreadNotificationByUserAsync(User user) { var unreadStatus = new List { states.WAITING, states.DELIVERED, states.SENDING }; return await _context.NotificationModels .Where(n => n.ReciverId == user.Id && unreadStatus.Contains(n.Status)) .ToListAsync(); } public async Task> ReadMyWaitingNotificationAsync(List notificationIds, User user) { var updatedNotifications = new List(); List notificationsToUpdate; if (notificationIds != null && notificationIds.Any()) { notificationsToUpdate = await _context.NotificationModels .Where(n => notificationIds.Contains(n.Id) && n.ReciverId == user.Id) .ToListAsync(); if (notificationsToUpdate.Count != notificationIds.Count(id => ExistsByIdAsync(id).Result)) { var invalidId = notificationIds.Except(notificationsToUpdate.Select(n => n.Id)).FirstOrDefault(); } } else { notificationsToUpdate = await GetUnreadNotificationByUserAsync(user); } foreach (var notif in notificationsToUpdate.Where(n => n.Status != states.READ)) { notif.Status = states.READ; updatedNotifications.Add(notif); } if (updatedNotifications.Any()) { await _context.SaveChangesAsync(); } return notificationsToUpdate; } public async Task ReadNotificationAsync(long notifId, User user) { var notif = await FindByIdAsync(notifId); if (notif.ReciverId != user.Id) { throw new UnauthorizedAccessException("User is not the receiver of this notification."); } notif.Status = states.READ; await SaveAsync(notif); return notif; } }