diff --git a/src/DevBetterWeb.Infrastructure/Handlers/SendEmailVideoAddedHandler.cs b/src/DevBetterWeb.Infrastructure/Handlers/SendEmailVideoAddedHandler.cs new file mode 100644 index 000000000..ac53aff72 --- /dev/null +++ b/src/DevBetterWeb.Infrastructure/Handlers/SendEmailVideoAddedHandler.cs @@ -0,0 +1,35 @@ +using System.Threading.Tasks; +using DevBetterWeb.Core.Events; +using DevBetterWeb.Core.Interfaces; +using DevBetterWeb.Infrastructure.Interfaces; + +namespace DevBetterWeb.Core.Handlers; + +public class SendEmailVideoAddedHandler : IHandle +{ + private readonly IGetUsersHaveRolesService _getUsersHaveRolesService; + private readonly IEmailService _emailService; + + public SendEmailVideoAddedHandler(IGetUsersHaveRolesService getUsersHaveRolesService, IEmailService emailService) + { + _getUsersHaveRolesService = getUsersHaveRolesService; + _emailService = emailService; + } + + public static string ReturnMessageString(VideoAddedEvent domainEvent) + { + return $"Video {domainEvent.Video.Title} is added! " + + $"Check out the video here: https://devbetter.com/Videos/Details/{domainEvent.Video.VideoId}."; + } + + public async Task Handle(VideoAddedEvent domainEvent) + { + var message = ReturnMessageString(domainEvent); + var activeUsers = await _getUsersHaveRolesService.ExecuteAsync(); + + foreach (var activeUser in activeUsers) + { + await _emailService.SendEmailAsync(activeUser.UserName!, "DevBetter New Video", message); + } + } +} diff --git a/src/DevBetterWeb.Infrastructure/Interfaces/IGetUsersHaveRolesService.cs b/src/DevBetterWeb.Infrastructure/Interfaces/IGetUsersHaveRolesService.cs new file mode 100644 index 000000000..e791b89a7 --- /dev/null +++ b/src/DevBetterWeb.Infrastructure/Interfaces/IGetUsersHaveRolesService.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Identity; + +namespace DevBetterWeb.Infrastructure.Interfaces; +public interface IGetUsersHaveRolesService +{ + Task> ExecuteAsync(); +} diff --git a/src/DevBetterWeb.Infrastructure/Services/GetUsersHaveRolesService.cs b/src/DevBetterWeb.Infrastructure/Services/GetUsersHaveRolesService.cs new file mode 100644 index 000000000..bd129bb38 --- /dev/null +++ b/src/DevBetterWeb.Infrastructure/Services/GetUsersHaveRolesService.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using DevBetterWeb.Infrastructure.Interfaces; +using Microsoft.AspNetCore.Identity; + +namespace DevBetterWeb.Infrastructure.Services; + +public class GetUsersHaveRolesService : IGetUsersHaveRolesService +{ + private readonly UserManager _userManager; + + public GetUsersHaveRolesService(UserManager userManager) + { + _userManager = userManager; + } + + public async Task> ExecuteAsync() + { + var usersWithRoles = new List(); + + var allUsers = _userManager.Users.ToList(); + foreach (var user in allUsers) + { + if (user.UserName == null) + { + continue; + } + var roles = await _userManager.GetRolesAsync(user); + + if (roles.Any()) + { + usersWithRoles.Add(user); + } + } + + return usersWithRoles; + } +} diff --git a/src/DevBetterWeb.Web/Program.cs b/src/DevBetterWeb.Web/Program.cs index e7e0b44e7..7afc68378 100644 --- a/src/DevBetterWeb.Web/Program.cs +++ b/src/DevBetterWeb.Web/Program.cs @@ -31,6 +31,7 @@ using DevBetterWeb.Infrastructure; using Microsoft.Extensions.Configuration; using NimblePros.Vimeo.Extensions; +using DevBetterWeb.Infrastructure.Interfaces; // 29 Aug 2023 - Getting a nullref in here somewhere maybe? Also a stack overflow during startup somewhere. @@ -122,6 +123,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); VimeoSettings vimeoSettings = builder.Configuration.GetSection(Constants.ConfigKeys.VimeoSettings)!.Get()!; builder.Services.AddSingleton(vimeoSettings);