db and auth refactoring
This commit is contained in:
parent
0e1c16ab91
commit
aa472f9e29
45 changed files with 2182 additions and 697 deletions
78
Sledgemapper.Api/Handlers/NewCampaignCommandHandler.cs
Normal file
78
Sledgemapper.Api/Handlers/NewCampaignCommandHandler.cs
Normal file
|
@ -0,0 +1,78 @@
|
|||
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;
|
||||
|
||||
namespace Sledgemapper.Api.Handlers
|
||||
{
|
||||
public class NewCampaignCommandHandler : IRequestHandler<NewCampaignCommand, bool>
|
||||
{
|
||||
private readonly IMediator _mediator;
|
||||
private readonly SledgemapperDbContext _dbcontext;
|
||||
|
||||
public NewCampaignCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
|
||||
{
|
||||
_mediator = mediator;
|
||||
_dbcontext = dbcontext;
|
||||
}
|
||||
|
||||
public async Task<bool> Handle(NewCampaignCommand notification, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var user = await _dbcontext.Users.FindAsync(notification.UserId);
|
||||
_dbcontext.Attach(user);
|
||||
var campaign = new Core.Entities.Campaign
|
||||
{
|
||||
CampaignName = notification.CampaignName,
|
||||
OwnerId = user.Id,
|
||||
InvitedUsers = new System.Collections.Generic.List<Entities.User> { user }
|
||||
};
|
||||
|
||||
_dbcontext.Campaigns.Add(campaign);
|
||||
|
||||
await _dbcontext.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
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>();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue