This commit is contained in:
Michele Scandura 2020-11-17 15:16:14 +00:00
parent 9ef43bb9f6
commit 0a4b8ebeb2
14 changed files with 157 additions and 28 deletions

View file

@ -8,6 +8,8 @@ namespace Sledgemapper.Api.Commands
public double Timestamp { get; private set; }
public string SessionName { get; private set; }
public int UserId { get; set; }
public int SessionId {get;set;}
public BaseCommand(string sessionName, int userId)
{
@ -15,5 +17,12 @@ namespace Sledgemapper.Api.Commands
SessionName = sessionName;
UserId=userId;
}
public BaseCommand(int sessionId, int userId)
{
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
SessionId = sessionId;
UserId=userId;
}
}
}

View file

@ -0,0 +1,50 @@
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;
}
}
}

View file

@ -2,12 +2,13 @@ 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.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
@ -42,7 +43,7 @@ namespace Sledgemapper.Api.Commands
UserId = notification.UserId
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewTileNotification(notification.SessionName, notification.Tile, notification.UserId));
await _mediator.Publish(new NewTileNotification(session, notification.Tile, notification.UserId));
return true;
}