cleanup
This commit is contained in:
parent
1b910adf55
commit
af441e772b
34 changed files with 279 additions and 292 deletions
|
@ -3,26 +3,17 @@ using System;
|
|||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public abstract class BaseCommand<T> : IRequest<T>
|
||||
public abstract class BaseCommand<T> : IRequest<T>
|
||||
{
|
||||
public double Timestamp { get; private set; }
|
||||
public string SessionName { get; private set; }
|
||||
public int UserId { get; set; }
|
||||
public int SessionId {get;set;}
|
||||
|
||||
public int UserId { get; private set; }
|
||||
|
||||
public BaseCommand(string sessionName, int userId)
|
||||
{
|
||||
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
||||
SessionName = sessionName;
|
||||
UserId=userId;
|
||||
}
|
||||
|
||||
public BaseCommand(int sessionId, int userId)
|
||||
{
|
||||
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
|
||||
SessionId = sessionId;
|
||||
UserId=userId;
|
||||
UserId = userId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
14
Sledgemapper.Api/Commands/DeleteOverlayCommand.cs
Normal file
14
Sledgemapper.Api/Commands/DeleteOverlayCommand.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class DeleteOverlayCommand : BaseCommand<bool>
|
||||
{
|
||||
public Overlay Overlay { get; private set; }
|
||||
|
||||
public DeleteOverlayCommand(string sessionName, Overlay overlay, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Overlay = overlay;
|
||||
}
|
||||
}
|
||||
}
|
14
Sledgemapper.Api/Commands/DeleteTileCommand.cs
Normal file
14
Sledgemapper.Api/Commands/DeleteTileCommand.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class DeleteTileCommand : BaseCommand<bool>
|
||||
{
|
||||
public Tile Tile { get; private set; }
|
||||
|
||||
public DeleteTileCommand(string sessionName, Tile tile, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Tile = tile;
|
||||
}
|
||||
}
|
||||
}
|
14
Sledgemapper.Api/Commands/DeleteWallCommand.cs
Normal file
14
Sledgemapper.Api/Commands/DeleteWallCommand.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class DeleteWallCommand : BaseCommand<bool>
|
||||
{
|
||||
public Wall Wall { get; private set; }
|
||||
|
||||
public DeleteWallCommand(string sessionName, Wall wall, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Wall = wall;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,18 +1,9 @@
|
|||
using System.Transactions;
|
||||
using System.Net.Mail;
|
||||
using MediatR;
|
||||
using Sledgemapper.Api.Data;
|
||||
using Sledgemapper.Shared.Entities;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Sledgemapper.Api.Handlers;
|
||||
using System.Linq;
|
||||
using Sledgemapper.Api.Models;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class GetMapSnapshotCommand : IRequest<Sledgemapper.Shared.Entities.Session>
|
||||
public class GetMapSnapshotCommand : IRequest<Session>
|
||||
{
|
||||
public string SessionName { get; private set; }
|
||||
public GetMapSnapshotCommand(string sessionName)
|
||||
|
@ -20,86 +11,4 @@ namespace Sledgemapper.Api.Commands
|
|||
SessionName = sessionName;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
14
Sledgemapper.Api/Commands/NewOverlayCommand.cs
Normal file
14
Sledgemapper.Api/Commands/NewOverlayCommand.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class NewOverlayCommand : BaseCommand<bool>
|
||||
{
|
||||
public Overlay Overlay { get; private set; }
|
||||
|
||||
public NewOverlayCommand(string sessionName, Overlay overlay, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Overlay = overlay;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,3 @@
|
|||
using MediatR;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class NewSessionCommand : BaseCommand<bool>
|
||||
|
|
13
Sledgemapper.Api/Commands/NewSnapshotCommand.cs
Normal file
13
Sledgemapper.Api/Commands/NewSnapshotCommand.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class NewSnapshotCommand : BaseCommand<bool>
|
||||
{
|
||||
public Session Session { get; private set; }
|
||||
public NewSnapshotCommand(string sessionName, Session session, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Session = session;
|
||||
}
|
||||
}
|
||||
}
|
13
Sledgemapper.Api/Commands/NewTileCommand.cs
Normal file
13
Sledgemapper.Api/Commands/NewTileCommand.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class NewTileCommand : BaseCommand<bool>
|
||||
{
|
||||
public Tile Tile { get; private set; }
|
||||
public NewTileCommand(string sessionName, Tile tile, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Tile = tile;
|
||||
}
|
||||
}
|
||||
}
|
14
Sledgemapper.Api/Commands/NewWallCommand.cs
Normal file
14
Sledgemapper.Api/Commands/NewWallCommand.cs
Normal file
|
@ -0,0 +1,14 @@
|
|||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class NewWallCommand : BaseCommand<bool>
|
||||
{
|
||||
public Wall Wall { get; private set; }
|
||||
|
||||
public NewWallCommand(string sessionName, Wall wall, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Wall = wall;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
using System.Transactions;
|
||||
using System.Net.Mail;
|
||||
using MediatR;
|
||||
using Sledgemapper.Api.Data;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Sledgemapper.Api.Handlers;
|
||||
using System.Linq;
|
||||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class SaveNewSnapshotCommand : BaseCommand<bool>
|
||||
{
|
||||
public Session Session { get; set; }
|
||||
public SaveNewSnapshotCommand(string sessionName, Session session, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Session = session;
|
||||
}
|
||||
}
|
||||
|
||||
public class SaveNewSnapshotCommandHandler : IRequestHandler<SaveNewSnapshotCommand, bool>
|
||||
{
|
||||
private readonly MyDbContext _dbcontext;
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public SaveNewSnapshotCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
|
||||
|
||||
public async Task<bool> Handle(SaveNewSnapshotCommand 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
using System.Transactions;
|
||||
using System.Net.Mail;
|
||||
using MediatR;
|
||||
using Sledgemapper.Api.Data;
|
||||
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Sledgemapper.Api.Handlers;
|
||||
using System.Linq;
|
||||
using Sledgemapper.Shared.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Commands
|
||||
{
|
||||
public class SaveNewTileCommand : BaseCommand<bool>
|
||||
{
|
||||
public Tile Tile { get; set; }
|
||||
public SaveNewTileCommand(string sessionName, Tile tile, int userId) : base(sessionName, userId)
|
||||
{
|
||||
Tile = tile;
|
||||
}
|
||||
}
|
||||
|
||||
public class SaveNewTileCommandHandler : IRequestHandler<SaveNewTileCommand, bool>
|
||||
{
|
||||
private readonly MyDbContext _dbcontext;
|
||||
private readonly IMediator _mediator;
|
||||
|
||||
public SaveNewTileCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
|
||||
|
||||
public async Task<bool> Handle(SaveNewTileCommand 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -1,48 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue