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; namespace Sledgemapper.Api.Commands { public class SaveNewTileCommand:BaseCommand { public Tile Tile {get;set;} public SaveNewTileCommand(string sessionName, Tile tile, int userId):base(sessionName, userId){ Tile=tile; } } public class SaveNewTileCommandHandler : IRequestHandler { private readonly MyDbContext _dbcontext; private readonly IMediator _mediator; public SaveNewTileCommandHandler(IMediator mediator, MyDbContext dbcontext){ _dbcontext = dbcontext; _mediator= mediator;} public async Task Handle(SaveNewTileCommand notification, CancellationToken cancellationToken) { var jsonString = JsonSerializer.Serialize(notification.Tile); _dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog { Operation = "N", SessionName = notification.SessionName, Type = "T", Timestamp = notification.Timestamp, Object = jsonString }); await _dbcontext.SaveChangesAsync(); await _mediator.Publish(new NewTileNotification(notification.SessionName, notification.Tile)); return true; } } }