From 661b26f043c20de39043dfbe9c0f9c034cf0c917 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 8 Oct 2020 19:56:47 +0330 Subject: [PATCH] #16 moving notification functionality to a new service --- RMuseum/Controllers/ArtifactController.cs | 17 +- RMuseum/RMuseum.xml | 179 ++++++++++-------- RMuseum/Services/IArtifactService.cs | 38 ---- RMuseum/Services/IRNotificationService.cs | 50 +++++ .../Implementation/ArtifactService.cs | 130 +------------ .../Implementation/RNotificationService.cs | 148 +++++++++++++++ RMuseum/Startup.cs | 3 + 7 files changed, 326 insertions(+), 239 deletions(-) create mode 100644 RMuseum/Services/IRNotificationService.cs create mode 100644 RMuseum/Services/Implementation/RNotificationService.cs diff --git a/RMuseum/Controllers/ArtifactController.cs b/RMuseum/Controllers/ArtifactController.cs index 514250f4..7215ee4a 100644 --- a/RMuseum/Controllers/ArtifactController.cs +++ b/RMuseum/Controllers/ArtifactController.cs @@ -1822,7 +1822,7 @@ namespace RMuseum.Controllers public async Task GetUserNotifications() { Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); - RServiceResult res = await _artifactService.GetUserNotifications(loggedOnUserId); + RServiceResult res = await _notificationService.GetUserNotifications(loggedOnUserId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); @@ -1836,7 +1836,7 @@ namespace RMuseum.Controllers public async Task GetUnreadUserNotificationsCount() { Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); - RServiceResult res = await _artifactService.GetUnreadUserNotificationsCount(loggedOnUserId); + RServiceResult res = await _notificationService.GetUnreadUserNotificationsCount(loggedOnUserId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); @@ -1854,7 +1854,7 @@ namespace RMuseum.Controllers [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task SwitchNotificationStatus(Guid notificationId) { - RServiceResult res = await _artifactService.SwitchNotificationStatus(notificationId); + RServiceResult res = await _notificationService.SwitchNotificationStatus(notificationId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); @@ -1872,7 +1872,7 @@ namespace RMuseum.Controllers [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task DeleteNotification(Guid notificationId) { - RServiceResult res = await _artifactService.DeleteNotification(notificationId); + RServiceResult res = await _notificationService.DeleteNotification(notificationId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(); @@ -1886,12 +1886,14 @@ namespace RMuseum.Controllers /// /// /// - public ArtifactController(IArtifactService artifactService, IUserPermissionChecker userPermissionChecker, IMemoryCache memoryCache, ICaptchaService captchaService) + /// + public ArtifactController(IArtifactService artifactService, IUserPermissionChecker userPermissionChecker, IMemoryCache memoryCache, ICaptchaService captchaService, IRNotificationService notificationService) { _artifactService = artifactService; _userPermissionChecker = userPermissionChecker; _memoryCache = memoryCache; _captchaService = captchaService; + _notificationService = notificationService; } /// @@ -1913,5 +1915,10 @@ namespace RMuseum.Controllers /// Captcha service /// protected readonly ICaptchaService _captchaService; + + /// + /// Notification Service + /// + protected readonly IRNotificationService _notificationService; } } diff --git a/RMuseum/RMuseum.xml b/RMuseum/RMuseum.xml index 0952f7ab..9924fbbb 100644 --- a/RMuseum/RMuseum.xml +++ b/RMuseum/RMuseum.xml @@ -590,7 +590,7 @@ - + constructor @@ -599,6 +599,7 @@ + @@ -620,6 +621,11 @@ Captcha service + + + Notification Service + + Gets audio narrations, user must have narration::moderate permission to be able to see all users narrations @@ -3577,43 +3583,6 @@ - - - Add Notification - - - - - - - - - Get User Notifications - - - - - - - Get Unread User Notifications Count - - - - - - - Switch Notification Status - - - updated notification object - - - - Delete Notification - - - - an incomplete prototype for removing artifacts @@ -4369,43 +4338,6 @@ - - - Add Notification - - - - - - - - - Switch Notification Status - - - updated notification object - - - - Delete Notification - - - - - - - Get User Notifications - - - - - - - Get Unread User Notifications Count - - - - Database Contetxt @@ -4431,7 +4363,12 @@ User Service - + + + Messaging service + + + constructor @@ -4440,6 +4377,7 @@ + @@ -4749,6 +4687,53 @@ + + + Internal messaging system implementation + + + + + Add Notification + + + + + + + + + Switch Notification Status + + + updated notification object + + + + Delete Notification + + + + + + + Get User Notifications + + + + + + + Get Unread User Notifications Count + + + + + + + Database Contetxt + + Role Service Implementation @@ -4846,6 +4831,48 @@ Image Storage Path + + + Internal messaging system interface + + + + + Add Notification + + + + + + + + + Get User Notifications + + + + + + + Get Unread User Notifications Count + + + + + + + Switch Notification Status + + + updated notification object + + + + Delete Notification + + + + //https://mattfrear.com/2018/07/21/add-an-authorization-header-to-your-swagger-ui-with-swashbuckle-revisited/ diff --git a/RMuseum/Services/IArtifactService.cs b/RMuseum/Services/IArtifactService.cs index 1d47d527..bc842e2c 100644 --- a/RMuseum/Services/IArtifactService.cs +++ b/RMuseum/Services/IArtifactService.cs @@ -498,44 +498,6 @@ namespace RMuseum.Services /// Task> SynchronizeSuggestedPinterestLink(Guid linkId); - - /// - /// Add Notification - /// - /// - /// - /// - /// - Task> PushNotification(Guid userId, string subject, string htmlText); - - /// - /// Get User Notifications - /// - /// - /// - Task> GetUserNotifications(Guid userId); - - /// - /// Get Unread User Notifications Count - /// - /// - /// - Task> GetUnreadUserNotificationsCount(Guid userId); - - /// - /// Switch Notification Status - /// - /// - /// updated notification object - Task> SwitchNotificationStatus(Guid notificationId); - - /// - /// Delete Notification - /// - /// - /// - Task> DeleteNotification(Guid notificationId); - /// /// an incomplete prototype for removing artifacts /// diff --git a/RMuseum/Services/IRNotificationService.cs b/RMuseum/Services/IRNotificationService.cs new file mode 100644 index 00000000..32cb7963 --- /dev/null +++ b/RMuseum/Services/IRNotificationService.cs @@ -0,0 +1,50 @@ +using RMuseum.Models.Notification; +using RSecurityBackend.Models.Generic; +using System; +using System.Threading.Tasks; + +namespace RMuseum.Services +{ + /// + /// Internal messaging system interface + /// + public interface IRNotificationService + { + /// + /// Add Notification + /// + /// + /// + /// + /// + Task> PushNotification(Guid userId, string subject, string htmlText); + + /// + /// Get User Notifications + /// + /// + /// + Task> GetUserNotifications(Guid userId); + + /// + /// Get Unread User Notifications Count + /// + /// + /// + Task> GetUnreadUserNotificationsCount(Guid userId); + + /// + /// Switch Notification Status + /// + /// + /// updated notification object + Task> SwitchNotificationStatus(Guid notificationId); + + /// + /// Delete Notification + /// + /// + /// + Task> DeleteNotification(Guid notificationId); + } +} diff --git a/RMuseum/Services/Implementation/ArtifactService.cs b/RMuseum/Services/Implementation/ArtifactService.cs index 68149cd1..a84e57f3 100644 --- a/RMuseum/Services/Implementation/ArtifactService.cs +++ b/RMuseum/Services/Implementation/ArtifactService.cs @@ -1965,7 +1965,7 @@ namespace RMuseum.Services.Implementation if(referenceNote.RAppUserId != userId) { RArtifactMasterRecord artificat = await _context.Artifacts.Where(a => a.Id == artifactId).SingleOrDefaultAsync(); - await PushNotification + await _notificationService.PushNotification ( referenceNote.RAppUserId, $"پاسخگویی {userInfo.Result.FirstName} {userInfo.Result.SureName} به یادداشت شما دربارهٔ {artificat.Name}", @@ -2032,7 +2032,7 @@ namespace RMuseum.Services.Implementation { RArtifactItemRecord item = await _context.Items.Where(a => a.Id == itemId).SingleOrDefaultAsync(); RArtifactMasterRecord artificat = await _context.Artifacts.Where(a => a.Id == item.RArtifactMasterRecordId).SingleOrDefaultAsync(); - await PushNotification + await _notificationService.PushNotification ( referenceNote.RAppUserId, $"پاسخگویی {userInfo.Result.FirstName} {userInfo.Result.SureName} به یادداشت شما دربارهٔ {artificat.Name} « {item.Name}", @@ -3050,124 +3050,7 @@ namespace RMuseum.Services.Implementation } } - /// - /// 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(notification); - } - catch (Exception exp) - { - return new RServiceResult(null, exp.ToString()); - } - } - /// - /// Switch Notification Status - /// - /// - /// updated notification object - public async Task> SwitchNotificationStatus(Guid notificationId) - { - try - { - RUserNotification notification = - await _context.Notifications.Where(n => n.Id == notificationId).SingleAsync(); - notification.Status = notification.Status == NotificationStatus.Unread ? NotificationStatus.Read : NotificationStatus.Unread; - _context.Notifications.Update(notification); - await _context.SaveChangesAsync(); - return new RServiceResult(notification); - } - catch (Exception exp) - { - return new RServiceResult(null, exp.ToString()); - } - } - - - /// - /// Delete Notification - /// - /// - /// - public async Task> DeleteNotification(Guid notificationId) - { - try - { - RUserNotification notification = - await _context.Notifications.Where(n => n.Id == notificationId).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) - .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()); - } - } /// @@ -3196,6 +3079,11 @@ namespace RMuseum.Services.Implementation /// protected readonly IAppUserService _userService; + /// + /// Messaging service + /// + protected readonly IRNotificationService _notificationService; + /// /// constructor /// @@ -3204,13 +3092,15 @@ namespace RMuseum.Services.Implementation /// /// /// - public ArtifactService(RMuseumDbContext context, IConfiguration configuration, IPictureFileService pictureFileService, IBackgroundTaskQueue backgroundTaskQueue, IAppUserService userService) + /// + public ArtifactService(RMuseumDbContext context, IConfiguration configuration, IPictureFileService pictureFileService, IBackgroundTaskQueue backgroundTaskQueue, IAppUserService userService, IRNotificationService notificationService) { _context = context; _pictureFileService = pictureFileService; Configuration = configuration; _backgroundTaskQueue = backgroundTaskQueue; _userService = userService; + _notificationService = notificationService; } } } diff --git a/RMuseum/Services/Implementation/RNotificationService.cs b/RMuseum/Services/Implementation/RNotificationService.cs new file mode 100644 index 00000000..df442827 --- /dev/null +++ b/RMuseum/Services/Implementation/RNotificationService.cs @@ -0,0 +1,148 @@ +using Microsoft.EntityFrameworkCore; +using RMuseum.DbContext; +using RMuseum.Models.Notification; +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(notification); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + + /// + /// Switch Notification Status + /// + /// + /// updated notification object + public async Task> SwitchNotificationStatus(Guid notificationId) + { + try + { + RUserNotification notification = + await _context.Notifications.Where(n => n.Id == notificationId).SingleAsync(); + notification.Status = notification.Status == NotificationStatus.Unread ? NotificationStatus.Read : NotificationStatus.Unread; + _context.Notifications.Update(notification); + await _context.SaveChangesAsync(); + return new RServiceResult(notification); + } + catch (Exception exp) + { + return new RServiceResult(null, exp.ToString()); + } + } + + + /// + /// Delete Notification + /// + /// + /// + public async Task> DeleteNotification(Guid notificationId) + { + try + { + RUserNotification notification = + await _context.Notifications.Where(n => n.Id == notificationId).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) + .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; + } + + } +} diff --git a/RMuseum/Startup.cs b/RMuseum/Startup.cs index acb6134c..6c469b0d 100644 --- a/RMuseum/Startup.cs +++ b/RMuseum/Startup.cs @@ -245,6 +245,9 @@ namespace RMuseum //picture file service services.AddTransient(); + //messaging service + services.AddTransient(); + //artifact service services.AddTransient();