sledgemapper/Sledgemapper.Api/Handlers/NewTileCommandHandler.cs

39 lines
1.4 KiB
C#

using MediatR;
using Sledgemapper.Api.Infrastructure.Data;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Notifications;
using System.Linq;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class NewTileCommandHandler : IRequestHandler<NewTileCommand, bool>
{
private readonly SledgemapperDbContext _dbcontext;
private readonly IMediator _mediator;
public NewTileCommandHandler(IMediator mediator, SledgemapperDbContext 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;
}
}
}