53 lines
No EOL
2 KiB
C#
53 lines
No EOL
2 KiB
C#
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Sledgemapper.Api.Commands;
|
|
using Sledgemapper.Api.Infrastructure.Data;
|
|
using Sledgemapper.Shared.Entities;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Sentry;
|
|
|
|
namespace Sledgemapper.Api.Handlers
|
|
{
|
|
public class GetCampaignPlayersCommandHandler : IRequestHandler<GetCampaignPlayersCommand, List<Player>>
|
|
{
|
|
private readonly IMediator _mediator;
|
|
private readonly SledgemapperDbContext _dbcontext;
|
|
|
|
public GetCampaignPlayersCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
|
|
{
|
|
_mediator = mediator;
|
|
_dbcontext = dbcontext;
|
|
}
|
|
|
|
public async Task<List<Player>> Handle(GetCampaignPlayersCommand command, CancellationToken cancellationToken)
|
|
{
|
|
try
|
|
{
|
|
var user = await _dbcontext.Users.FindAsync(new[] { command.UserId }, cancellationToken: cancellationToken);
|
|
_dbcontext.Attach(user);
|
|
|
|
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(cancellationToken);
|
|
|
|
//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;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
SentrySdk.CaptureException(ex);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
} |