From 1b910adf558aae84319970222f68365b3d97d3b0 Mon Sep 17 00:00:00 2001 From: Michele Date: Wed, 18 Nov 2020 00:01:29 +0000 Subject: [PATCH] cleanup --- .../Commands/GetMapSnapshotCommand.cs | 9 +--- .../Commands/SaveNewOverlayCommand.cs | 48 +++++++++++++++++++ .../Commands/SaveNewWallCommand.cs | 48 +++++++++++++++++++ Sledgemapper.Api/Handlers/SaveNewOverlay.cs | 35 -------------- Sledgemapper.Api/Handlers/SaveNewTile.cs | 34 ------------- Sledgemapper.Api/Handlers/SaveNewWall.cs | 33 ------------- .../Handlers/SendNewWallMessage.cs | 2 +- .../Notifications/NewOverlayNotification.cs | 5 ++ .../Notifications/NewWallNotification.cs | 5 ++ 9 files changed, 108 insertions(+), 111 deletions(-) create mode 100644 Sledgemapper.Api/Commands/SaveNewOverlayCommand.cs create mode 100644 Sledgemapper.Api/Commands/SaveNewWallCommand.cs delete mode 100644 Sledgemapper.Api/Handlers/SaveNewOverlay.cs delete mode 100644 Sledgemapper.Api/Handlers/SaveNewTile.cs delete mode 100644 Sledgemapper.Api/Handlers/SaveNewWall.cs diff --git a/Sledgemapper.Api/Commands/GetMapSnapshotCommand.cs b/Sledgemapper.Api/Commands/GetMapSnapshotCommand.cs index 221c582..2bb6267 100644 --- a/Sledgemapper.Api/Commands/GetMapSnapshotCommand.cs +++ b/Sledgemapper.Api/Commands/GetMapSnapshotCommand.cs @@ -24,9 +24,8 @@ namespace Sledgemapper.Api.Commands public class GetMapSnapshotCommandHandler : IRequestHandler { private readonly MyDbContext _dbcontext; - private readonly IMediator _mediator; - public GetMapSnapshotCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; } + public GetMapSnapshotCommandHandler(MyDbContext dbcontext) { _dbcontext = dbcontext; } public async Task Handle(GetMapSnapshotCommand notification, CancellationToken cancellationToken) { @@ -100,13 +99,7 @@ namespace Sledgemapper.Api.Commands await _dbcontext.Snapshots.AddAsync(newSnapshot); await _dbcontext.SaveChangesAsync(); } - - - - return mapSession; } - - } } diff --git a/Sledgemapper.Api/Commands/SaveNewOverlayCommand.cs b/Sledgemapper.Api/Commands/SaveNewOverlayCommand.cs new file mode 100644 index 0000000..6098097 --- /dev/null +++ b/Sledgemapper.Api/Commands/SaveNewOverlayCommand.cs @@ -0,0 +1,48 @@ +using MediatR; +using Sledgemapper.Api.Commands; +using Sledgemapper.Api.Data; +using Sledgemapper.Shared.Entities; +using System.Linq; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace Sledgemapper.Api.Handlers +{ + public class SaveNewOverlayCommand : BaseCommand + { + public Overlay Overlay { get; private set; } + + public SaveNewOverlayCommand(string sessionName, Overlay overlay, int userId) : base(sessionName, userId) + { + Overlay = overlay; + } + } + + public class SaveNewOverlayCommandHandler : IRequestHandler + { + private readonly MyDbContext _dbcontext; + + private readonly IMediator _mediator; + + public SaveNewOverlayCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; } + + public async Task Handle(SaveNewOverlayCommand notification, CancellationToken cancellationToken) + { + var jsonString = JsonSerializer.Serialize(notification.Overlay); + var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName); + _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog + { + Operation = "N", + SessionId = session.SessionId, + Type = "O", + Timestamp = notification.Timestamp, + Object = jsonString, + UserId = notification.UserId, + }); + await _dbcontext.SaveChangesAsync(); + await _mediator.Publish(new NewOverlayNotification(session, notification.Overlay, notification.UserId)); + return true; + } + } +} diff --git a/Sledgemapper.Api/Commands/SaveNewWallCommand.cs b/Sledgemapper.Api/Commands/SaveNewWallCommand.cs new file mode 100644 index 0000000..5f6dffa --- /dev/null +++ b/Sledgemapper.Api/Commands/SaveNewWallCommand.cs @@ -0,0 +1,48 @@ +using MediatR; +using Sledgemapper.Api.Commands; +using Sledgemapper.Api.Data; +using Sledgemapper.Shared.Entities; +using System.Linq; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; + +namespace Sledgemapper.Api.Handlers +{ + public class SaveNewWallCommand : BaseCommand + { + public Wall Wall { get; private set; } + + public SaveNewWallCommand(string sessionName, Wall wall, int userId) : base(sessionName, userId) + { + Wall = wall; + } + } + + public class SaveNewWallCommandHandler : IRequestHandler + { + private readonly MyDbContext _dbcontext; + + private readonly IMediator _mediator; + + public SaveNewWallCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; } + + public async Task Handle(SaveNewWallCommand notification, CancellationToken cancellationToken) + { + var jsonString = JsonSerializer.Serialize(notification.Wall); + var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName); + _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog + { + Operation = "N", + SessionId = session.SessionId, + Type = "W", + Timestamp = notification.Timestamp, + Object = jsonString, + UserId = notification.UserId, + }); + await _dbcontext.SaveChangesAsync(); + await _mediator.Publish(new NewWallNotification(session, notification.Wall, notification.UserId)); + return true; + } + } +} diff --git a/Sledgemapper.Api/Handlers/SaveNewOverlay.cs b/Sledgemapper.Api/Handlers/SaveNewOverlay.cs deleted file mode 100644 index e965d4f..0000000 --- a/Sledgemapper.Api/Handlers/SaveNewOverlay.cs +++ /dev/null @@ -1,35 +0,0 @@ -using MediatR; -using Sledgemapper.Api.Data; -using Sledgemapper.Shared.Entities; -using System.Linq; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace Sledgemapper.Api.Handlers -{ - public class SaveNewOverlay : INotificationHandler - { - private readonly MyDbContext _dbcontext; - - public SaveNewOverlay(MyDbContext dbcontext) => _dbcontext = dbcontext; - - public async Task Handle(NewOverlayNotification notification, CancellationToken cancellationToken) - { - var jsonString = JsonSerializer.Serialize(notification.Overlay); - var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName); - - _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog - { - Operation = "N", - SessionId = session.SessionId, - Type = "O", - Timestamp = notification.Timestamp, - Object = jsonString, - UserId = notification.UserId - }); - - await _dbcontext.SaveChangesAsync(); - } - } -} diff --git a/Sledgemapper.Api/Handlers/SaveNewTile.cs b/Sledgemapper.Api/Handlers/SaveNewTile.cs deleted file mode 100644 index 842e85e..0000000 --- a/Sledgemapper.Api/Handlers/SaveNewTile.cs +++ /dev/null @@ -1,34 +0,0 @@ -// using MediatR; -// using Sledgemapper.Api.Data; -// using Sledgemapper.Shared.Entities; -// using System.Linq; -// using System.Text.Json; -// using System.Threading; -// using System.Threading.Tasks; - -// namespace Sledgemapper.Api.Handlers -// { -// public class SaveNewTile : INotificationHandler -// { -// private readonly MyDbContext _dbcontext; - -// public SaveNewTile(MyDbContext dbcontext) => _dbcontext = dbcontext; - -// public async Task Handle(NewTileNotification notification, CancellationToken cancellationToken) -// { -// var jsonString = JsonSerializer.Serialize(notification.Tile); -// var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName); - -// _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog -// { -// Operation = "N", -// SessionId = session.SessionId, -// Type = "T", -// Timestamp = notification.Timestamp, -// Object = jsonString, -// UserId=notification.UserId -// }); -// await _dbcontext.SaveChangesAsync(); -// } -// } -// } diff --git a/Sledgemapper.Api/Handlers/SaveNewWall.cs b/Sledgemapper.Api/Handlers/SaveNewWall.cs deleted file mode 100644 index c426346..0000000 --- a/Sledgemapper.Api/Handlers/SaveNewWall.cs +++ /dev/null @@ -1,33 +0,0 @@ -using MediatR; -using Sledgemapper.Api.Data; -using Sledgemapper.Shared.Entities; -using System.Linq; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -namespace Sledgemapper.Api.Handlers -{ - public class SaveNewWall : INotificationHandler - { - private readonly MyDbContext _dbcontext; - - public SaveNewWall(MyDbContext dbcontext) => _dbcontext = dbcontext; - - public async Task Handle(NewWallNotification notification, CancellationToken cancellationToken) - { - var jsonString = JsonSerializer.Serialize(notification.Wall); - var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName); - _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog - { - Operation = "N", - SessionId = session.SessionId, - Type = "W", - Timestamp = notification.Timestamp, - Object = jsonString, - UserId = notification.UserId, - }); - await _dbcontext.SaveChangesAsync(); - } - } -} diff --git a/Sledgemapper.Api/Handlers/SendNewWallMessage.cs b/Sledgemapper.Api/Handlers/SendNewWallMessage.cs index 7d88346..d9d2eb9 100644 --- a/Sledgemapper.Api/Handlers/SendNewWallMessage.cs +++ b/Sledgemapper.Api/Handlers/SendNewWallMessage.cs @@ -15,7 +15,7 @@ namespace Sledgemapper.Api.Handlers public async Task Handle(NewWallNotification notification, CancellationToken cancellationToken) { - await _hub.Clients.Groups(notification.SessionName).NewWall(notification.Wall); + await _hub.Clients.Groups(notification.Session.SessionName).NewWall(notification.Wall); } } } diff --git a/Sledgemapper.Api/Notifications/NewOverlayNotification.cs b/Sledgemapper.Api/Notifications/NewOverlayNotification.cs index fe39ab7..a895cad 100644 --- a/Sledgemapper.Api/Notifications/NewOverlayNotification.cs +++ b/Sledgemapper.Api/Notifications/NewOverlayNotification.cs @@ -10,5 +10,10 @@ namespace Sledgemapper.Api.Handlers { Overlay = overlay; } + + public NewOverlayNotification(Models.Session session, Overlay overlay, int userId) : base(session, userId) + { + Overlay = overlay; + } } } diff --git a/Sledgemapper.Api/Notifications/NewWallNotification.cs b/Sledgemapper.Api/Notifications/NewWallNotification.cs index 8409cc7..b538941 100644 --- a/Sledgemapper.Api/Notifications/NewWallNotification.cs +++ b/Sledgemapper.Api/Notifications/NewWallNotification.cs @@ -10,5 +10,10 @@ namespace Sledgemapper.Api.Handlers { Wall = wall; } + + public NewWallNotification(Models.Session session, Wall wall, int userId) : base(session, userId) + { + Wall = wall; + } } }