fix player invitation
This commit is contained in:
parent
85e2f699bf
commit
5276055c4d
@ -29,7 +29,18 @@ namespace Sledgemapper.Api.Handlers
|
||||
var user = await _dbcontext.Users.FindAsync(command.UserId);
|
||||
_dbcontext.Attach(user);
|
||||
|
||||
var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignId == command.CampaignId && campaign.OwnerId == command.UserId).Include(campaign => campaign.InvitedUsers).FirstAsync();
|
||||
|
||||
var campaign = await _dbcontext
|
||||
.Campaigns
|
||||
.Where(campaign => campaign.CampaignId == command.CampaignId)
|
||||
.Include(c => c.InvitedUsers)
|
||||
|
||||
.Include(c => c.Owner)
|
||||
.Where(campaign => campaign.OwnerId == command.UserId || campaign.InvitedUsers.Contains(user)).FirstAsync();
|
||||
|
||||
|
||||
|
||||
//var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignId == command.CampaignId && campaign.OwnerId == command.UserId).Include(campaign => campaign.InvitedUsers).FirstAsync();
|
||||
|
||||
var players = campaign.InvitedUsers.Select(user => new Player { Initials = user.Initials, UserName = user.UserName , UserId = new Guid(user.Id)}).ToList();
|
||||
return players;
|
||||
|
@ -1,43 +1,53 @@
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sledgemapper.Api.Commands;
|
||||
using Sledgemapper.Api.Infrastructure.Data;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sledgemapper.Api.Commands;
|
||||
using Sledgemapper.Api.Infrastructure.Data;
|
||||
|
||||
namespace Sledgemapper.Api.Handlers
|
||||
{
|
||||
public class InvitePlayerToCampaignCommandHandler : IRequestHandler<InvitePlayerToCampaignCommand, bool>
|
||||
{
|
||||
private readonly SledgemapperDbContext _dbContext;
|
||||
private readonly IMediator _mediator;
|
||||
private readonly SledgemapperDbContext _dbcontext;
|
||||
|
||||
public InvitePlayerToCampaignCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
|
||||
public InvitePlayerToCampaignCommandHandler(IMediator mediator, SledgemapperDbContext dbContext)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_dbcontext = dbcontext;
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<bool> Handle(InvitePlayerToCampaignCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await _dbcontext.Users.FindAsync(command.UserId);
|
||||
var user = await _dbContext.Users.FindAsync(command.UserId);
|
||||
|
||||
var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignId == command.CampaignId && campaign.OwnerId == command.UserId).Include(campaign => campaign.InvitedUsers).FirstAsync();
|
||||
var invitedUser = await _dbcontext.Users.FirstOrDefaultAsync(user => user.Email == command.Email);
|
||||
_dbcontext.Attach(invitedUser);
|
||||
_dbcontext.Attach(campaign);
|
||||
var campaign = await _dbContext
|
||||
.Campaigns
|
||||
.Where(campaign => campaign.CampaignId == command.CampaignId)
|
||||
.Include(c => c.InvitedUsers)
|
||||
.Where(campaign => campaign.OwnerId == command.UserId || campaign.InvitedUsers.Contains(user))
|
||||
.FirstAsync(cancellationToken);
|
||||
|
||||
|
||||
var invitedUser =
|
||||
await _dbContext.Users.FirstOrDefaultAsync(iUser => iUser.Email == command.Email,
|
||||
cancellationToken);
|
||||
_dbContext.Attach(invitedUser);
|
||||
_dbContext.Attach(campaign);
|
||||
campaign.InvitedUsers.Add(invitedUser);
|
||||
_dbcontext.Campaigns.Update(campaign);
|
||||
await _dbcontext.SaveChangesAsync();
|
||||
|
||||
await _dbContext.SaveChangesAsync(cancellationToken);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user