sledgemapper/Sledgemapper.Api/Commands/SaveNewTileCommand.cs

47 lines
1.5 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;
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);
_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;
}
}
}