This commit is contained in:
Michele 2020-11-18 00:01:29 +00:00
parent e988f5c310
commit 1b910adf55
9 changed files with 108 additions and 111 deletions

View file

@ -24,9 +24,8 @@ namespace Sledgemapper.Api.Commands
public class GetMapSnapshotCommandHandler : IRequestHandler<GetMapSnapshotCommand, Sledgemapper.Shared.Entities.Session>
{
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<Sledgemapper.Shared.Entities.Session> Handle(GetMapSnapshotCommand notification, CancellationToken cancellationToken)
{
@ -100,13 +99,7 @@ namespace Sledgemapper.Api.Commands
await _dbcontext.Snapshots.AddAsync(newSnapshot);
await _dbcontext.SaveChangesAsync();
}
return mapSession;
}
}
}

View file

@ -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<bool>
{
public Overlay Overlay { get; private set; }
public SaveNewOverlayCommand(string sessionName, Overlay overlay, int userId) : base(sessionName, userId)
{
Overlay = overlay;
}
}
public class SaveNewOverlayCommandHandler : IRequestHandler<SaveNewOverlayCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public SaveNewOverlayCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(SaveNewOverlayCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Overlay>(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;
}
}
}

View file

@ -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<bool>
{
public Wall Wall { get; private set; }
public SaveNewWallCommand(string sessionName, Wall wall, int userId) : base(sessionName, userId)
{
Wall = wall;
}
}
public class SaveNewWallCommandHandler : IRequestHandler<SaveNewWallCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public SaveNewWallCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(SaveNewWallCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Wall>(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;
}
}
}

View file

@ -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<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);
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();
}
}
}

View file

@ -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<NewTileNotification>
// {
// private readonly MyDbContext _dbcontext;
// public SaveNewTile(MyDbContext dbcontext) => _dbcontext = dbcontext;
// public async Task Handle(NewTileNotification notification, CancellationToken cancellationToken)
// {
// var jsonString = JsonSerializer.Serialize<Tile>(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();
// }
// }
// }

View file

@ -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<NewWallNotification>
{
private readonly MyDbContext _dbcontext;
public SaveNewWall(MyDbContext dbcontext) => _dbcontext = dbcontext;
public async Task Handle(NewWallNotification notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Wall>(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();
}
}
}

View file

@ -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);
}
}
}

View file

@ -10,5 +10,10 @@ namespace Sledgemapper.Api.Handlers
{
Overlay = overlay;
}
public NewOverlayNotification(Models.Session session, Overlay overlay, int userId) : base(session, userId)
{
Overlay = overlay;
}
}
}

View file

@ -10,5 +10,10 @@ namespace Sledgemapper.Api.Handlers
{
Wall = wall;
}
public NewWallNotification(Models.Session session, Wall wall, int userId) : base(session, userId)
{
Wall = wall;
}
}
}