sledgemapper/Sledgemapper.Api/Handlers/NewNoteCommandHandler.cs
Michele Scandura f15f1c8857
All checks were successful
continuous-integration/drone/push Build is passing
cleanup, refactoring, usual stuff
2021-09-16 11:11:07 +01:00

39 lines
1.4 KiB
C#

using MediatR;
using Sledgemapper.Api.Infrastructure.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 NewNoteCommandHandler : IRequestHandler<NewNoteCommand, bool>
{
private readonly SledgemapperDbContext _dbcontext;
private readonly IMediator _mediator;
public NewNoteCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewNoteCommand notification, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize(notification.Note);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
_dbcontext.MapLogs.Add(new 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;
}
}
}