sledgemapper/Sledgemapper.Api/Handlers/NewWallCommandHandler.cs
2020-12-03 19:38:03 +00:00

66 lines
2.5 KiB
C#

using MediatR;
using Sledgemapper.Api.Data;
using Sledgemapper.Shared.Entities;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Notifications;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class NewWallCommandHandler : IRequestHandler<NewWallCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewWallCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewWallCommand 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;
}
}
public class NewNoteCommandHandler : IRequestHandler<NewNoteCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public NewNoteCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewNoteCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize<Note>(notification.Note);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
_dbcontext.MapLogs.Add(new Sledgemapper.Api.Models.MapLog
{
Operation = "N",
SessionId = session.SessionId,
Type = "N",
Timestamp = notification.Timestamp,
Object = jsonString,
UserId = notification.UserId,
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewNoteNotification(session, notification.Note, notification.UserId));
return true;
}
}
}