basic player management

This commit is contained in:
Michele 2021-02-21 22:59:43 +00:00
parent 350a283b5e
commit bab170552e
12 changed files with 244 additions and 91 deletions

View file

@ -0,0 +1,43 @@
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;
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(command.UserId);
_dbcontext.Attach(user);
var campaign = await _dbcontext.Campaigns.Where(campaign=>campaign.CampaignName==command.CampaignName && campaign.OwnerId==command.UserId).Include(campaign=>campaign.InvitedUsers).FirstAsync();
var players=campaign.InvitedUsers.Select(user=>new Player{Initials=user.Initials, UserName=user.UserName}).ToList();
return players;
}
catch (Exception ex)
{
}
return null;
}
}
}

View file

@ -0,0 +1,39 @@
using MediatR;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Infrastructure.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Sledgemapper.Api.Handlers
{
public class GetCampaignsCommandHandler : IRequestHandler<GetCampaignsCommand, List<Core.Entities.Campaign>>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public GetCampaignsCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<List<Core.Entities.Campaign>> Handle(GetCampaignsCommand command, CancellationToken cancellationToken)
{
try
{
var user = await _dbcontext.Users.FindAsync(command.UserId);
_dbcontext.Attach(user);
var campaigns = _dbcontext.Campaigns.Where(campaign => campaign.OwnerId == command.UserId || campaign.InvitedUsers.Contains(user));
return campaigns.ToList();
}
catch (Exception ex)
{
}
return new List<Core.Entities.Campaign>();
}
}
}

View file

@ -0,0 +1,44 @@
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;
namespace Sledgemapper.Api.Handlers
{
public class InvitePlayerToCampaignCommandHandler : IRequestHandler<InvitePlayerToCampaignCommand, bool>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public InvitePlayerToCampaignCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<bool> Handle(InvitePlayerToCampaignCommand command, CancellationToken cancellationToken)
{
try
{
var user = await _dbcontext.Users.FindAsync(command.UserId);
var campaign = await _dbcontext.Campaigns.Where(campaign=>campaign.CampaignName==command.CampaignName && 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);
campaign.InvitedUsers.Add(invitedUser);
_dbcontext.Campaigns.Update(campaign);
await _dbcontext.SaveChangesAsync();
return true;
}
catch (Exception ex)
{
}
return false;
}
}
}

View file

@ -1,10 +1,7 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sledgemapper.Api.Commands;
using Sledgemapper.Api.Infrastructure.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -47,32 +44,4 @@ namespace Sledgemapper.Api.Handlers
return false;
}
}
public class GetCampaignsCommandHandler : IRequestHandler<GetCampaignsCommand, List<Core.Entities.Campaign>>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public GetCampaignsCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<List<Core.Entities.Campaign>> Handle(GetCampaignsCommand command, CancellationToken cancellationToken)
{
try
{
var user = await _dbcontext.Users.FindAsync(command.UserId);
_dbcontext.Attach(user);
var campaigns = _dbcontext.Campaigns.Where(campaign => campaign.OwnerId == command.UserId || campaign.InvitedUsers.Contains(user));
return campaigns.ToList();
}
catch (Exception ex)
{
}
return new List<Core.Entities.Campaign>();
}
}
}