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; 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); 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(notification.SessionName, notification.Tile, notification.UserId)); return true; } } }