Commit 674add08 authored by Almouhannad's avatar Almouhannad

(B) Add notifications

parent a8c67c7a
...@@ -3,8 +3,8 @@ using API.Options.JWT; ...@@ -3,8 +3,8 @@ using API.Options.JWT;
using API.SeedDatabaseHelper; using API.SeedDatabaseHelper;
using Application.Behaviors; using Application.Behaviors;
using FluentValidation; using FluentValidation;
using Infrastructure.BackgroundServices.Notifications; using Infrastructure;
using Infrastructure.NotificationsService; using Infrastructure.NotificationsHubs;
using MediatR; using MediatR;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
...@@ -43,23 +43,20 @@ builder.Services.AddDbContext<ClinicsDbContext>( ...@@ -43,23 +43,20 @@ builder.Services.AddDbContext<ClinicsDbContext>(
#region Add SignalR #region Add SignalR
builder.Services.AddSignalR(); builder.Services.AddSignalR();
// Background services:
//builder.Services.AddHostedService<ServerTimeNotifier>();
#endregion #endregion
#region Add CORS #region Add CORS
builder.Services.AddCors(); builder.Services.AddCors();
#endregion #endregion
#region Link interfaces implemented in persistence #region Link interfaces implemented in infrastructre
// Using Scrutor library // Using Scrutor library
builder builder
.Services .Services
.Scan( .Scan(
selector => selector selector => selector
.FromAssemblies(Persistence.AssemblyReference.Assembly .FromAssemblies(Persistence.AssemblyReference.Assembly,
// Add other assemblies here Infrastructure.AssemblyReference.Assembly
) )
.AddClasses(false) .AddClasses(false)
.AsImplementedInterfaces() .AsImplementedInterfaces()
...@@ -142,7 +139,8 @@ if (app.Environment.IsDevelopment()) ...@@ -142,7 +139,8 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection(); app.UseHttpsRedirection();
#region Map notification HUB #region Map notification HUB
app.MapHub<NotificationHub>("api/Notifications"); app.MapHub<DoctorsNotificationsHub>("api/Notifications/Doctors");
app.MapHub<ReceptionistsNotificationsHub>("api/Notifications/Receptionists");
#endregion #endregion
#region CORS #region CORS
......
using Application.Abstractions.Notifications.Doctors.NewVisitNotifications;
namespace Application.Abstractions.Notifications.Doctors;
public interface IDoctorsNotificationService
{
public Task SendNewVisitNotification(NewVisitNotification notification);
}
namespace Application.Abstractions.Notifications.Doctors.NewVisitNotifications;
public class NewVisitNotification : INotification
{
private NewVisitNotification(int patientId, string patientFullName, int doctorId, int doctorUserId)
{
PatientId = patientId;
PatientFullName = patientFullName;
DoctorId = doctorId;
DoctorUserId = doctorUserId;
}
public int PatientId { get; set; }
public string PatientFullName { get; set; } = null!;
public int DoctorId { get; set; }
public int DoctorUserId { get; set; }
public static NewVisitNotification Create(int patientId, string patientFullName, int doctorId, int doctorUserId)
{
return new NewVisitNotification(patientId, patientFullName, doctorId, doctorUserId);
}
}
namespace Application.Abstractions.Notifications;
public interface INotification
{
}
using Application.Abstractions.Notifications;
namespace Infrastructure.Abstractions;
public interface INotificationClient
{
Task ReceiveNotification(INotification notification);
}
using System.Reflection;
namespace Infrastructure;
public class AssemblyReference
{
public static readonly Assembly Assembly = typeof(AssemblyReference).Assembly;
}
using Infrastructure.NotificationsService;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Infrastructure.BackgroundServices.Notifications;
public class ServerTimeNotifier : BackgroundService
{
private static readonly TimeSpan Period = TimeSpan.FromSeconds(3);
#region CTOR DI
private readonly ILogger<ServerTimeNotifier> _logger;
private readonly IHubContext<NotificationHub, INotificationClient> _context;
public ServerTimeNotifier(ILogger<ServerTimeNotifier> logger, IHubContext<NotificationHub, INotificationClient> context)
{
_logger = logger;
_context = context;
}
#endregion
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var timer = new PeriodicTimer(Period);
while (!stoppingToken.IsCancellationRequested &&
await timer.WaitForNextTickAsync(stoppingToken))
{
var dateTime = DateTime.Now;
_logger.LogInformation("Executing {Service} {Time}", nameof(ServerTimeNotifier), dateTime);
await _context.Clients.All.ReceiveNotification($"Server time = {dateTime}");
}
}
}
using Microsoft.AspNetCore.SignalR;
namespace Infrastructure.NotificationsService;
public class NotificationHub : Hub<INotificationClient>
{
public override async Task OnConnectedAsync()
{
await Clients.Client(Context.ConnectionId).ReceiveNotification(
$"Connected successfully { Context.User?.Identity?.Name}");
await base.OnConnectedAsync();
}
}
public interface INotificationClient
{
Task ReceiveNotification(string message);
}
\ No newline at end of file
using Infrastructure.Abstractions;
using Microsoft.AspNetCore.SignalR;
namespace Infrastructure.NotificationsHubs;
public class DoctorsNotificationsHub : Hub<INotificationClient>
{
}
using Infrastructure.Abstractions;
using Microsoft.AspNetCore.SignalR;
namespace Infrastructure.NotificationsHubs;
public class ReceptionistsNotificationsHub : Hub<INotificationClient>
{
}
using Application.Abstractions.Notifications.Doctors;
using Application.Abstractions.Notifications.Doctors.NewVisitNotifications;
using Infrastructure.Abstractions;
using Infrastructure.NotificationsHubs;
using Microsoft.AspNetCore.SignalR;
namespace Infrastructure.NotificationsServices.Doctors;
public class DoctorsNotificationsService : IDoctorsNotificationService
{
#region CTOR DI
private readonly IHubContext<DoctorsNotificationsHub, INotificationClient> _context;
public DoctorsNotificationsService(IHubContext<DoctorsNotificationsHub, INotificationClient> context)
{
_context = context;
}
#endregion
public async Task SendNewVisitNotification(NewVisitNotification notification)
{
await _context.Clients.All.ReceiveNotification(notification);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment