48 lines
1.7 KiB
C#
48 lines
1.7 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 SaveNewOverlayCommand : BaseCommand<bool>
|
|
{
|
|
public Overlay Overlay { get; private set; }
|
|
|
|
public SaveNewOverlayCommand(string sessionName, Overlay overlay, int userId) : base(sessionName, userId)
|
|
{
|
|
Overlay = overlay;
|
|
}
|
|
}
|
|
|
|
public class SaveNewOverlayCommandHandler : IRequestHandler<SaveNewOverlayCommand, bool>
|
|
{
|
|
private readonly MyDbContext _dbcontext;
|
|
|
|
private readonly IMediator _mediator;
|
|
|
|
public SaveNewOverlayCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
|
|
|
|
public async Task<bool> Handle(SaveNewOverlayCommand notification, CancellationToken cancellationToken)
|
|
{
|
|
var jsonString = JsonSerializer.Serialize<Overlay>(notification.Overlay);
|
|
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
|
|
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
|
|
{
|
|
Operation = "N",
|
|
SessionId = session.SessionId,
|
|
Type = "O",
|
|
Timestamp = notification.Timestamp,
|
|
Object = jsonString,
|
|
UserId = notification.UserId,
|
|
});
|
|
await _dbcontext.SaveChangesAsync();
|
|
await _mediator.Publish(new NewOverlayNotification(session, notification.Overlay, notification.UserId));
|
|
return true;
|
|
}
|
|
}
|
|
}
|