This commit is contained in:
Michele Scandura 2020-11-18 11:09:26 +00:00
parent 1b910adf55
commit af441e772b
34 changed files with 279 additions and 292 deletions

View file

@ -1,6 +1,8 @@
using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using Sledgemapper.Api.Notifications;
using System.Linq;
using System.Text.Json;
using System.Threading;
@ -8,13 +10,15 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class SaveDeleteOverlay : INotificationHandler<DeleteOverlayNotification>
public class DeleteOverlayCommandHandler : IRequestHandler<DeleteOverlayCommand, bool>
{
private readonly MyDbContext _dbcontext;
public SaveDeleteOverlay(MyDbContext dbcontext) => _dbcontext = dbcontext;
private readonly IMediator _mediator;
public async Task Handle(DeleteOverlayNotification notification, CancellationToken cancellationToken)
public DeleteOverlayCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(DeleteOverlayCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Overlay>(notification.Overlay);
var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName);
@ -30,6 +34,8 @@ namespace Sledgemapper.Api.Handlers
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new DeleteOverlayNotification(session, notification.Overlay, notification.UserId));
return true;
}
}
}

View file

@ -1,4 +1,6 @@
using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using System.Linq;
@ -8,13 +10,15 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class SaveDeleteTile : INotificationHandler<DeleteTileNotification>
public class DeleteTileCommandHandler : IRequestHandler<DeleteTileCommand,bool>
{
private readonly MyDbContext _dbcontext;
private readonly MyDbContext _dbcontext;
public SaveDeleteTile(MyDbContext dbcontext) => _dbcontext = dbcontext;
private readonly IMediator _mediator;
public async Task Handle(DeleteTileNotification notification, CancellationToken cancellationToken)
public DeleteTileCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(DeleteTileCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Tile>(notification.Tile);
var session = _dbcontext.Sessions.First(m=>m.SessionName== notification.SessionName);
@ -29,6 +33,8 @@ namespace Sledgemapper.Api.Handlers
UserId = notification.UserId
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new DeleteTileNotification(session, notification.Tile, notification.UserId));
return true;
}
}
}

View file

@ -1,6 +1,8 @@
using MediatR;
using Microsoft.AspNetCore.Http.Features;
using Sledgemapper.Api.Data;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Shared.Entities;
using System.Collections.Generic;
using System.Linq;
@ -11,13 +13,16 @@ using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class SaveDeleteWall : INotificationHandler<DeleteWallNotification>
public class DeleteWallCommandHandler : IRequestHandler<DeleteWallCommand, bool>
{
private readonly MyDbContext _dbcontext;
public SaveDeleteWall(MyDbContext dbcontext) => _dbcontext = dbcontext;
private readonly IMediator _mediator;
public async Task Handle(DeleteWallNotification notification, CancellationToken cancellationToken)
public DeleteWallCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(DeleteWallCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Wall>(notification.Wall);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
@ -32,6 +37,9 @@ namespace Sledgemapper.Api.Handlers
UserId = notification.UserId
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new DeleteWallNotification(session, notification.Wall, notification.UserId));
return true;
}
}
}

View file

@ -0,0 +1,93 @@
using MediatR;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using Sledgemapper.Api.Models;
namespace Sledgemapper.Api.Commands
{
public class GetMapSnapshotCommandHandler : IRequestHandler<GetMapSnapshotCommand, Sledgemapper.Shared.Entities.Session>
{
private readonly MyDbContext _dbcontext;
public GetMapSnapshotCommandHandler(MyDbContext dbcontext) { _dbcontext = dbcontext; }
public async Task<Sledgemapper.Shared.Entities.Session> Handle(GetMapSnapshotCommand notification, CancellationToken cancellationToken)
{
Snapshot snapshot;
double timestamp;
Sledgemapper.Shared.Entities.Session mapSession;
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
snapshot = _dbcontext.Snapshots.OrderByDescending(s => s.Timestamp).FirstOrDefault(m => m.SessionId == session.SessionId);
if (snapshot is null)
{
timestamp = 0;
mapSession = new Shared.Entities.Session();
}
else
{
mapSession = JsonSerializer.Deserialize<Sledgemapper.Shared.Entities.Session>(snapshot.Object);
timestamp = snapshot.Timestamp;
}
var mapUpdates = _dbcontext.MapLogs.Where(m => m.SessionId == session.SessionId && m.Timestamp > timestamp).OrderBy(m => m.Timestamp).ToList();
foreach (var mapUpdate in mapUpdates)
{
if (mapUpdate.Operation == "N")
{
switch (mapUpdate.Type)
{
case "T":
var tile = JsonSerializer.Deserialize<Tile>(mapUpdate.Object);
mapSession.NewTile(tile, tile.ID);
break;
case "W":
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
mapSession.NewWall(wall, wall.ID);
break;
case "O":
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
mapSession.NewOverlay(overlay, overlay.ID);
break;
}
}
else if (mapUpdate.Operation == "D")
{
switch (mapUpdate.Type)
{
case "T":
var tile = JsonSerializer.Deserialize<Tile>(mapUpdate.Object);
mapSession.DeleteTile(tile);
break;
case "W":
var wall = JsonSerializer.Deserialize<Wall>(mapUpdate.Object);
mapSession.DeleteWall(wall);
break;
case "O":
var overlay = JsonSerializer.Deserialize<Overlay>(mapUpdate.Object);
mapSession.DeleteOverlay(overlay);
break;
}
}
}
if (mapUpdates.Any())
{
var newSnapshot = new Snapshot
{
SessionId = session.SessionId,
Timestamp = mapUpdates.Max(mapSession => mapSession.Timestamp),
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(mapSession)
};
await _dbcontext.Snapshots.AddAsync(newSnapshot);
await _dbcontext.SaveChangesAsync();
}
return mapSession;
}
}
}

View file

@ -0,0 +1,39 @@
using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
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 NewOverlayCommandHandler : IRequestHandler<NewOverlayCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewOverlayCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewOverlayCommand 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,37 @@
using MediatR;
using Sledgemapper.Api.Data;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
namespace Sledgemapper.Api.Commands
{
public class NewSnapshotCommandHandler : IRequestHandler<NewSnapshotCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewSnapshotCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewSnapshotCommand notification, CancellationToken cancellationToken)
{
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
var newSnapshot = new Sledgemapper.Api.Models.Snapshot{
SessionId=session.SessionId,
Timestamp=notification.Timestamp,
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(notification.Session)
};
await _dbcontext.Snapshots.AddAsync(newSnapshot);
await _dbcontext.SaveChangesAsync();
return true;
}
}
}

View file

@ -0,0 +1,43 @@
using MediatR;
using Sledgemapper.Api.Data;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Api.Handlers;
using System.Linq;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class NewTileCommandHandler : IRequestHandler<NewTileCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewTileCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewTileCommand 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();
await _mediator.Publish(new NewTileNotification(session, notification.Tile, notification.UserId));
return true;
}
}
}

View file

@ -0,0 +1,39 @@
using MediatR;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class NewWallCommandHandler : IRequestHandler<NewWallCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewWallCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewWallCommand 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,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View file

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View file

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View file

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View file

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;

View file

@ -1,6 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.SignalR;
using SignalRChat.Hubs;
using Sledgemapper.Api.Notifications;
using Sledgemapper.Clients;
using System.Threading;
using System.Threading.Tasks;