refactoring and cleanup

This commit is contained in:
Michele 2020-11-08 14:39:57 +00:00
parent 777608941b
commit 535c420952
8 changed files with 212 additions and 95 deletions

View file

@ -14,20 +14,38 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class NewTileNotification: INotification
public abstract class BaseNotification : INotification
{
public double Timestamp { get; private set; }
public Tile Tile { get; private set; }
public string SessionName { get; private set; }
public NewTileNotification(string sessionName, Tile tile)
public BaseNotification(string sessionName)
{
Tile = tile;
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
SessionName = sessionName;
}
}
public class NewTileNotification : BaseNotification
{
public Tile Tile { get; private set; }
public NewTileNotification(string sessionName, Tile tile) : base(sessionName)
{
Tile = tile;
}
}
public class NewOverlayNotification : BaseNotification
{
public Overlay Overlay { get; private set; }
public NewOverlayNotification(string sessionName, Overlay overlay) : base(sessionName)
{
Overlay = overlay;
}
}
public class SendNewTileMessage : INotificationHandler<NewTileNotification>
{
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
@ -61,4 +79,39 @@ namespace Sledgemapper.Api.Handlers
await _dbcontext.SaveChangesAsync();
}
}
public class SendNewOverlayMessage : INotificationHandler<NewOverlayNotification>
{
private readonly IHubContext<SledgemapperHub, ISledgemapperClient> _hub;
public SendNewOverlayMessage(IHubContext<SledgemapperHub, ISledgemapperClient> hub) => _hub = hub;
public async Task Handle(NewOverlayNotification notification, CancellationToken cancellationToken)
{
await _hub.Clients.Groups(notification.SessionName).NewOverlay(notification.Overlay);
}
}
public class SaveNewOverlay : INotificationHandler<NewOverlayNotification>
{
private readonly MyDbContext _dbcontext;
public SaveNewOverlay(MyDbContext dbcontext) => _dbcontext = dbcontext;
public async Task Handle(NewOverlayNotification notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Overlay>(notification.Overlay);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{
Operation = "N",
SessionName = notification.SessionName,
Type = "O",
Timestamp = notification.Timestamp,
Object = jsonString
});
await _dbcontext.SaveChangesAsync();
}
}
}