sledgemapper/Sledgemapper.Api/Commands/SaveNewTileCommand.cs
2020-11-17 00:01:21 +00:00

51 lines
1.7 KiB
C#

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