sledgemapper/Sledgemapper.Api/Handlers/NewSnapshotCommandHandler.cs
Michele Scandura 333c6c4046
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
stability improvements
2021-09-22 16:16:46 +01:00

40 lines
1.2 KiB
C#

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;
using Sledgemapper.Api.Core.Entities;
namespace Sledgemapper.Api.Handlers
{
public class NewSnapshotCommandHandler : IRequestHandler<NewSnapshotCommand, bool>
{
private readonly SledgemapperDbContext _dbcontext;
private readonly IMediator _mediator;
public NewSnapshotCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewSnapshotCommand notification, CancellationToken cancellationToken)
{
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
var newSnapshot = new Snapshot
{
SessionId = session.SessionId,
Timestamp = notification.Timestamp,
Object = JsonSerializer.Serialize(notification.Session)
};
await _dbcontext.Snapshots.AddAsync(newSnapshot, cancellationToken);
await _dbcontext.SaveChangesAsync(cancellationToken);
return true;
}
}
}