This commit is contained in:
Michele 2021-01-14 23:34:24 +00:00
parent 24b773d845
commit d3f8fd23f4
21 changed files with 297 additions and 49 deletions

View file

@ -0,0 +1,39 @@
using MediatR;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using Sledgemapper.Api.Commands;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Notifications;
namespace Sledgemapper.Api.Handlers
{
public class NewRoomCommandHandler : IRequestHandler<NewRoomCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewRoomCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewRoomCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Room>(notification.Room);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{
Operation = "N",
SessionId = session.SessionId,
Type = "R",
Timestamp = notification.Timestamp,
Object = jsonString,
UserId = notification.UserId,
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewRoomNotification(session, notification.Room, notification.UserId));
return true;
}
}
}