Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add email notification when new video available solve #1329

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<VideoAddedEvent>
{
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace DevBetterWeb.Infrastructure.Interfaces;
public interface IGetUsersHaveRolesService
{
Task<List<IdentityUser>> ExecuteAsync();
}
Original file line number Diff line number Diff line change
@@ -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<IdentityUser> _userManager;

public GetUsersHaveRolesService(UserManager<IdentityUser> userManager)
{
_userManager = userManager;
}

public async Task<List<IdentityUser>> ExecuteAsync()
{
var usersWithRoles = new List<IdentityUser>();

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;
}
}
2 changes: 2 additions & 0 deletions src/DevBetterWeb.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -122,6 +123,7 @@
builder.Services.AddScoped<ILeaderboardService, LeaderboardService>();
builder.Services.AddScoped<IAddCreatedVideoToFolderService, AddCreatedVideoToFolderService>();
builder.Services.AddScoped<ICreateVideoService, CreateVideoService>();
builder.Services.AddScoped<IGetUsersHaveRolesService, GetUsersHaveRolesService>();

VimeoSettings vimeoSettings = builder.Configuration.GetSection(Constants.ConfigKeys.VimeoSettings)!.Get<VimeoSettings>()!;
builder.Services.AddSingleton(vimeoSettings);
Expand Down
Loading