sledgemapper/Sledgemapper.Api/Commands/SaveNewWallCommand.cs
2020-11-18 00:01:29 +00:00

48 lines
1.6 KiB
C#

using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class SaveNewWallCommand : BaseCommand<bool>
{
public Wall Wall { get; private set; }
public SaveNewWallCommand(string sessionName, Wall wall, int userId) : base(sessionName, userId)
{
Wall = wall;
}
}
public class SaveNewWallCommandHandler : IRequestHandler<SaveNewWallCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public SaveNewWallCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(SaveNewWallCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Wall>(notification.Wall);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{
Operation = "N",
SessionId = session.SessionId,
Type = "W",
Timestamp = notification.Timestamp,
Object = jsonString,
UserId = notification.UserId,
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewWallNotification(session, notification.Wall, notification.UserId));
return true;
}
}
}