sledgemapper/Sledgemapper.Api/Commands/SaveNewSnapshotCommand.cs
Michele Scandura 0a4b8ebeb2 progress
2020-11-17 15:16:14 +00:00

50 lines
1.5 KiB
C#

using System.Transactions;
using System.Net.Mail;
using MediatR;
using Sledgemapper.Api.Data;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Sledgemapper.Api.Handlers;
using System.Linq;
using Sledgemapper.Shared.Entities;
namespace Sledgemapper.Api.Commands
{
public class SaveNewSnapshotCommand : BaseCommand<bool>
{
public Session Session { get; set; }
public SaveNewSnapshotCommand(string sessionName, Session session, int userId) : base(sessionName, userId)
{
Session = session;
}
}
public class SaveNewSnapshotCommandHandler : IRequestHandler<SaveNewSnapshotCommand, bool>
{
private readonly MyDbContext _dbcontext;
private readonly IMediator _mediator;
public SaveNewSnapshotCommandHandler(IMediator mediator, MyDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(SaveNewSnapshotCommand notification, CancellationToken cancellationToken)
{
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
var newSnapshot = new Sledgemapper.Api.Models.Snapshot{
SessionId=session.SessionId,
Timestamp=notification.Timestamp,
Object = JsonSerializer.Serialize<Sledgemapper.Shared.Entities.Session>(notification.Session)
};
await _dbcontext.Snapshots.AddAsync(newSnapshot);
await _dbcontext.SaveChangesAsync();
return true;
}
}
}