wiring up map updates
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Michele Scandura 2021-09-16 16:34:44 +01:00
parent e18cbb157c
commit 155cb4ea9a
13 changed files with 99 additions and 58 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 GetCampaignMapsCommandHandler : IRequestHandler<GetCampaignMapsCommand, List<Session>>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public GetCampaignMapsCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<List<Session>> Handle(GetCampaignMapsCommand 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.Maps).FirstAsync();
var maps = campaign.Maps.Select(session => new Session { SessionName = session.SessionName, SessionId = session.SessionId }).ToList();
return maps;
}
catch (Exception ex)
{
}
return null;
}
}
}

View file

@ -40,36 +40,4 @@ namespace Sledgemapper.Api.Handlers
return null;
}
}
public class GetCampaignMapsCommandHandler : IRequestHandler<GetCampaignMapsCommand, List<Session>>
{
private readonly IMediator _mediator;
private readonly SledgemapperDbContext _dbcontext;
public GetCampaignMapsCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext)
{
_mediator = mediator;
_dbcontext = dbcontext;
}
public async Task<List<Session>> Handle(GetCampaignMapsCommand 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.Maps).FirstAsync();
var maps = campaign.Maps.Select(session => new Session { SessionName = session.SessionName, SessionId = session.SessionId }).ToList();
return maps;
}
catch (Exception ex)
{
}
return null;
}
}
}

View file

@ -7,6 +7,8 @@ using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
namespace Sledgemapper.Api.Handlers
{
@ -18,21 +20,32 @@ namespace Sledgemapper.Api.Handlers
public NewOverlayCommandHandler(IMediator mediator, SledgemapperDbContext dbcontext) { _dbcontext = dbcontext; _mediator = mediator; }
public async Task<bool> Handle(NewOverlayCommand notification, CancellationToken cancellationToken)
public async Task<bool> Handle(NewOverlayCommand command, CancellationToken cancellationToken)
{
var jsonString = JsonSerializer.Serialize(notification.Overlay);
var session = _dbcontext.Sessions.First(m => m.SessionName == notification.SessionName);
var user = await _dbcontext.Users.FindAsync(command.UserId);
_dbcontext.Attach(user);
var campaign = await _dbcontext.Campaigns.Where(campaign => campaign.CampaignId == new Guid(command.Campaign) && campaign.OwnerId == command.UserId).Include(campaign => campaign.Maps).FirstAsync();
var maps = campaign.Maps.Any(s => s.SessionId == int.Parse(command.SessionName));
if (!maps)
{
throw new Exception("Unauthorized");
}
var jsonString = JsonSerializer.Serialize(command.Overlay);
var session = _dbcontext.Sessions.First(m => m.SessionId == int.Parse(command.SessionName));
_dbcontext.MapLogs.Add(new Models.MapLog
{
Operation = "N",
SessionId = session.SessionId,
Type = "O",
Timestamp = notification.Timestamp,
Timestamp = command.Timestamp,
Object = jsonString,
UserId = notification.UserId,
UserId = command.UserId,
});
await _dbcontext.SaveChangesAsync();
await _mediator.Publish(new NewOverlayNotification(session, notification.Overlay, notification.UserId));
await _mediator.Publish(new NewOverlayNotification(session, command.Overlay, command.UserId));
return true;
}
}