sledgemapper/Sledgemapper.Api/Handlers/GetCampaignsCommandHandler.cs
Michele 633c97e0da
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
small fixes
2021-09-24 21:53:00 +01:00

45 lines
No EOL
1.5 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;
namespace Sledgemapper.Api.Handlers
{
public class GetCampaignsCommandHandler : IRequestHandler<GetCampaignsCommand, List<Campaign>>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public GetCampaignsCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<List<Campaign>> Handle(GetCampaignsCommand command, CancellationToken cancellationToken)
{
try
{
var user = await _dbcontext.Users.FindAsync(command.UserId);
_dbcontext.Attach(user);
var campaigns = await _dbcontext.Campaigns
.Include(c => c.InvitedUsers)
.Include(c => c.Owner)
.Where(campaign => campaign.OwnerId == command.UserId || campaign.InvitedUsers.Contains(user)).ToListAsync();
return campaigns.Select(c => new Campaign { Id = c.CampaignId, Name = c.CampaignName }).ToList();
}
catch (Exception ex)
{
}
return new List<Campaign>();
}
}
}