sledgemapper/Sledgemapper.Api/Handlers/StartNewSessionHandler.cs
Michele Scandura 195533bce0
All checks were successful
continuous-integration/drone/push Build is passing
migrating session/map it to guid.
2021-09-16 16:51:07 +01:00

37 lines
1.2 KiB
C#

using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Infrastructure.Data;
using Sledgemapper.Api.Models;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class StartNewSessionHandler : IRequestHandler<NewSessionCommand, bool>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public StartNewSessionHandler(IMediator mediator, SledgemapperDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<bool> Handle(NewSessionCommand notification, CancellationToken cancellationToken)
{
var campaign = _dbcontext.Campaigns.First(c => c.CampaignId == notification.Campaign && c.OwnerId == notification.UserId.ToString());
_dbcontext.Sessions.Add(new Session
{
SessionName = notification.SessionName,
OwnerUserId = notification.UserId,
CampaignId = campaign.CampaignId
});
await _dbcontext.SaveChangesAsync();
return true;
}
}
}