fix player invitation

This commit is contained in:
Michele Scandura 2021-09-24 11:00:24 +01:00
parent 85e2f699bf
commit 5276055c4d
2 changed files with 36 additions and 15 deletions

View File

@ -29,7 +29,18 @@ namespace Sledgemapper.Api.Handlers
var user = await _dbcontext.Users.FindAsync(command.UserId); var user = await _dbcontext.Users.FindAsync(command.UserId);
_dbcontext.Attach(user); _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(); var players = campaign.InvitedUsers.Select(user => new Player { Initials = user.Initials, UserName = user.UserName , UserId = new Guid(user.Id)}).ToList();
return players; return players;

View File

@ -1,43 +1,53 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Infrastructure.Data;
using System; using System;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Infrastructure.Data;
namespace Sledgemapper.Api.Handlers namespace Sledgemapper.Api.Handlers
{ {
public class InvitePlayerToCampaignCommandHandler : IRequestHandler<InvitePlayerToCampaignCommand, bool> public class InvitePlayerToCampaignCommandHandler : IRequestHandler<InvitePlayerToCampaignCommand, bool>
{ {
private readonly SledgemapperDbContext _dbContext;
private readonly IMediator _mediator; private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public InvitePlayerToCampaignCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext) public InvitePlayerToCampaignCommandHandler(IMediator mediator, SledgemapperDbContext dbContext)
{ {
_mediator = mediator; _mediator = mediator;
_dbcontext = dbcontext; _dbContext = dbContext;
} }
public async Task<bool> Handle(InvitePlayerToCampaignCommand command, CancellationToken cancellationToken) public async Task<bool> Handle(InvitePlayerToCampaignCommand command, CancellationToken cancellationToken)
{ {
try 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 campaign = await _dbContext
var invitedUser = await _dbcontext.Users.FirstOrDefaultAsync(user => user.Email == command.Email); .Campaigns
_dbcontext.Attach(invitedUser); .Where(campaign => campaign.CampaignId == command.CampaignId)
_dbcontext.Attach(campaign); .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); campaign.InvitedUsers.Add(invitedUser);
_dbcontext.Campaigns.Update(campaign);
await _dbcontext.SaveChangesAsync(); await _dbContext.SaveChangesAsync(cancellationToken);
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
{ {
} }
return false; return false;
} }
} }