sledgemapper/Sledgemapper.Api/Handlers/NewCampaignCommandHandler.cs
Michele Scandura 73400b5c9f
All checks were successful
continuous-integration/drone/push Build is passing
cleanup
2021-09-30 11:19:38 +01:00

48 lines
No EOL
1.5 KiB
C#

using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Infrastructure.Data;
using System;
using System.Threading;
using System.Threading.Tasks;
using Sentry;
using User = Sledgemapper.Api.Core.Entities.User;
namespace Sledgemapper.Api.Handlers
{
public class NewCampaignCommandHandler : IRequestHandler<NewCampaignCommand, bool>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public NewCampaignCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<bool> Handle(NewCampaignCommand notification, CancellationToken cancellationToken)
{
try
{
var user = await _dbcontext.Users.FindAsync(new object[] { notification.UserId }, cancellationToken);
_dbcontext.Attach(user);
var campaign = new Core.Entities.Campaign
{
CampaignName = notification.CampaignName,
OwnerId = user.Id,
InvitedUsers = new System.Collections.Generic.List<User> { user }
};
_dbcontext.Campaigns.Add(campaign);
await _dbcontext.SaveChangesAsync(cancellationToken);
return true;
}
catch (Exception ex)
{
SentrySdk.CaptureException(ex);
}
return false;
}
}
}