using Microsoft.EntityFrameworkCore;
using RMuseum.DbContext;
using RMuseum.Models.Notification;
using RMuseum.Models.Notification.ViewModels;
using RSecurityBackend.Models.Generic;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace RMuseum.Services.Implementation
{
///
/// Internal messaging system implementation
///
public class RNotificationService : IRNotificationService
{
///
/// Add Notification
///
///
///
///
///
public async Task> PushNotification(Guid userId, string subject, string htmlText)
{
try
{
RUserNotification notification =
new RUserNotification()
{
UserId = userId,
DateTime = DateTime.Now,
Status = NotificationStatus.Unread,
Subject = subject,
HtmlText = htmlText
};
_context.Notifications.Add(notification);
await _context.SaveChangesAsync();
return new RServiceResult(new RUserNotificationViewModel(notification));
}
catch (Exception exp)
{
return new RServiceResult(null, exp.ToString());
}
}
///
/// Switch Notification Status
///
///
///
/// updated notification object
public async Task> SwitchNotificationStatus(Guid notificationId, Guid userId)
{
try
{
RUserNotification notification =
await _context.Notifications.Where(n => n.Id == notificationId && n.UserId == userId).SingleAsync();
notification.Status = notification.Status == NotificationStatus.Unread ? NotificationStatus.Read : NotificationStatus.Unread;
_context.Notifications.Update(notification);
await _context.SaveChangesAsync();
return new RServiceResult(new RUserNotificationViewModel(notification));
}
catch (Exception exp)
{
return new RServiceResult(null, exp.ToString());
}
}
///
/// Set All User Notifications Status
///
///
///
///
public async Task>SetAllNotificationsStatus(Guid userId, NotificationStatus status)
{
try
{
var notifications = await _context.Notifications.Where(n => n.UserId == userId).ToListAsync();
foreach(var notification in notifications)
{
notification.Status = status;
}
_context.UpdateRange(notifications);
await _context.SaveChangesAsync();
return new RServiceResult(true);
}
catch (Exception exp)
{
return new RServiceResult(false, exp.ToString());
}
}
///
/// Delete Notification
///
/// if empty deletes all read notifications
///
///
public async Task> DeleteNotification(Guid notificationId, Guid userId)
{
try
{
if (notificationId == Guid.Empty)
{
var notifications = await _context.Notifications.Where(n => n.UserId == userId && n.Status == NotificationStatus.Read).ToListAsync();
_context.Notifications.RemoveRange(notifications);
await _context.SaveChangesAsync();
}
else
{
RUserNotification notification =
await _context.Notifications.Where(n => n.Id == notificationId && n.UserId == userId).SingleAsync();
_context.Notifications.Remove(notification);
await _context.SaveChangesAsync();
}
return new RServiceResult(true);
}
catch (Exception exp)
{
return new RServiceResult(false, exp.ToString());
}
}
///
/// Get User Notifications
///
///
///
public async Task> GetUserNotifications(Guid userId)
{
try
{
return new RServiceResult
(
await _context.Notifications
.Where(n => n.UserId == userId)
.OrderByDescending(n => n.DateTime)
.Select(n => new RUserNotificationViewModel(n))
.ToArrayAsync()
);
}
catch (Exception exp)
{
return new RServiceResult(null, exp.ToString());
}
}
///
/// Get Unread User Notifications Count
///
///
///
public async Task> GetUnreadUserNotificationsCount(Guid userId)
{
try
{
return new RServiceResult
(
await _context.Notifications
.Where(n => n.UserId == userId && n.Status == NotificationStatus.Unread)
.CountAsync()
);
}
catch (Exception exp)
{
return new RServiceResult(0, exp.ToString());
}
}
///
/// Database Contetxt
///
protected readonly RMuseumDbContext _context;
public RNotificationService(RMuseumDbContext context)
{
_context = context;
}
}
}