This commit is contained in:
parent
17d6cd28d6
commit
7e3e645fc9
48 changed files with 1530 additions and 1538 deletions
|
@ -7,31 +7,29 @@ using System.Threading.Tasks;
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Sledgemapper.Api.Models;
|
||||
using System.Collections.Generic;
|
||||
using Sledgemapper.Api.Core.Entities;
|
||||
|
||||
namespace Sledgemapper.Api.Handlers
|
||||
{
|
||||
public abstract class BaseCommandHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse> where TRequest : BaseCommand<TResponse>
|
||||
{
|
||||
protected SledgemapperDbContext Dbcontext { get; }
|
||||
protected SledgemapperDbContext DbContext { get; }
|
||||
|
||||
protected IMediator Mediator { get; }
|
||||
|
||||
|
||||
|
||||
public abstract Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken);
|
||||
|
||||
public BaseCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
|
||||
protected BaseCommandHandler(IMediator mediator, SledgemapperDbContext dbContext)
|
||||
{
|
||||
Dbcontext = dbcontext;
|
||||
DbContext = dbContext;
|
||||
Mediator = mediator;
|
||||
}
|
||||
|
||||
protected async Task CheckAuthorization(TRequest command)
|
||||
{
|
||||
var user = await Dbcontext.Users.FindAsync(command.UserId);
|
||||
Dbcontext.Attach(user);
|
||||
var user = await DbContext.Users.FindAsync(command.UserId);
|
||||
DbContext.Attach(user);
|
||||
|
||||
|
||||
var campaign = await GetCampaignForUser(command);
|
||||
|
@ -46,9 +44,9 @@ namespace Sledgemapper.Api.Handlers
|
|||
|
||||
protected async Task<Campaign> GetCampaignForUser(TRequest command)
|
||||
{
|
||||
var user = await Dbcontext.Users.FindAsync(command.UserId);
|
||||
Dbcontext.Attach(user);
|
||||
var campaign = await Dbcontext
|
||||
var user = await DbContext.Users.FindAsync(command.UserId);
|
||||
DbContext.Attach(user);
|
||||
var campaign = await DbContext
|
||||
.Campaigns
|
||||
.Where(campaign => campaign.CampaignId == command.Campaign)
|
||||
.Include(c => c.InvitedUsers)
|
||||
|
@ -60,8 +58,8 @@ namespace Sledgemapper.Api.Handlers
|
|||
|
||||
protected async Task<Session> SaveLog(TRequest command, string operation, string type, string data, CancellationToken cancellationToken)
|
||||
{
|
||||
var session = Dbcontext.Sessions.First(m => m.SessionId == command.SessionId);
|
||||
Dbcontext.MapLogs.Add(new Models.MapLog
|
||||
var session = DbContext.Sessions.First(m => m.SessionId == command.SessionId);
|
||||
DbContext.MapLogs.Add(new MapLog
|
||||
{
|
||||
Operation = operation,
|
||||
SessionId = session.SessionId,
|
||||
|
@ -70,7 +68,7 @@ namespace Sledgemapper.Api.Handlers
|
|||
Object = data,
|
||||
UserId = command.UserId,
|
||||
});
|
||||
await Dbcontext.SaveChangesAsync(cancellationToken);
|
||||
await DbContext.SaveChangesAsync(cancellationToken);
|
||||
return session;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue