Compare commits

...

2 Commits

Author SHA1 Message Date
Michele Scandura
099fca8ceb small fixes
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2021-09-24 11:02:22 +01:00
Michele Scandura
5276055c4d fix player invitation 2021-09-24 11:00:24 +01:00
4 changed files with 38 additions and 16 deletions

View File

@ -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;

View File

@ -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;
}
}

View File

@ -231,6 +231,7 @@ namespace Sledgemapper.UI
MenuCampaingNew.Enabled = true;
MenuMapNew.Enabled = false;
MenuMapOpen.Enabled = false;
MenuCampaignPlayers.Enabled = false;
lblCampaign.Text = "n/a";
lblMap.Text = "n/a";
CommunicationManager.SessionData.MapEntityAdded -= OnMapEntityAdded;

View File

@ -45,7 +45,7 @@ namespace Sledgemapper.UI
grid1.Widgets.Add(TxtCampaign);
BtnNewCampaign = new TextButton();
BtnNewCampaign.Text = "New";
BtnNewCampaign.Text = "Invite";
BtnNewCampaign.Padding = new Thickness(10, 5);
BtnNewCampaign.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;
BtnNewCampaign.Id = "BtnNewCampaign";