using MediatR; using Sledgemapper.Api.Infrastructure.Data; using System.Text.Json; using System.Threading; using System.Threading.Tasks; using System.Linq; using Sledgemapper.Api.Commands; namespace Sledgemapper.Api.Handlers { public class NewSnapshotCommandHandler : IRequestHandler { private readonly SledgemapperDbContext _dbcontext; private readonly IMediator _mediator; public NewSnapshotCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; } public async Task Handle(NewSnapshotCommand notification, CancellationToken cancellationToken) { var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName); var newSnapshot = new Models.Snapshot { SessionId = session.SessionId, Timestamp = notification.Timestamp, Object = JsonSerializer.Serialize(notification.Session) }; await _dbcontext.Snapshots.AddAsync(newSnapshot); await _dbcontext.SaveChangesAsync(); return true; } } }