Updated handlers to use ticket connector for all endpoints (get single, get many, get by ticket id)

This commit is contained in:
Philip Dimitratos 2017-11-30 16:31:11 -08:00
Родитель c91ba297e6
Коммит 6654ee0b75
10 изменённых файлов: 110 добавлений и 36 удалений

2
domain

@ -1 +1 @@
Subproject commit c7fe26ddcd6d1a0fa4d89362598a402621639724
Subproject commit 79f76c379a412981abb3e3256b8108895cf1ffa8

Просмотреть файл

@ -1,8 +1,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Sia.Connectors.Tickets;
using Sia.Connectors.Tickets.None;
using System;
using System.Runtime.Loader;

Просмотреть файл

@ -15,6 +15,7 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="6.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.0.1" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -22,16 +22,13 @@ namespace Sia.Gateway.Requests
public long Id { get; }
}
public class GetIncidentHandler : IAsyncRequestHandler<GetIncidentRequest, Incident>
public class GetIncidentHandler : IncidentConnectorHandler<GetIncidentRequest, Incident>
{
private readonly IncidentContext _context;
private readonly Connector _connector;
public GetIncidentHandler(IncidentContext context, Connector connector)
{
_context = context;
_connector = connector;
}
public async Task<Incident> Handle(GetIncidentRequest getIncident)
:base(context, connector){}
public override async Task<Incident> Handle(GetIncidentRequest getIncident)
{
var incidentRecord = await _context
.Incidents
@ -41,9 +38,7 @@ namespace Sia.Gateway.Requests
var incident = Mapper.Map<Incident>(incidentRecord);
incident.Tickets = (await _connector
.GetData(incident.Tickets.AsEnumerable())
).ToList();
await AttachTickets(incident);
return incident;
}

Просмотреть файл

@ -1,6 +1,7 @@
using AutoMapper.QueryableExtensions;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sia.Connectors.Tickets;
using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Shared.Authentication;
@ -19,19 +20,19 @@ namespace Sia.Gateway.Requests
}
public class GetIncidentsHandler
: IncidentContextHandler<GetIncidentsRequest, IEnumerable<Incident>>
: IncidentConnectorHandler<GetIncidentsRequest, IEnumerable<Incident>>
{
public GetIncidentsHandler(IncidentContext context)
:base(context)
{
}
public GetIncidentsHandler(
IncidentContext context,
Connector connector
) : base(context, connector) {}
public override async Task<IEnumerable<Incident>> Handle(GetIncidentsRequest request)
{
var incidentRecords = await _context.Incidents
.WithEagerLoading()
.ProjectTo<Incident>()
.ToListAsync();
AttachTickets(incidentRecords);
return incidentRecords;
}
}

Просмотреть файл

@ -2,6 +2,7 @@
using AutoMapper.QueryableExtensions;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sia.Connectors.Tickets;
using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Domain.ApiModels;
@ -13,10 +14,13 @@ using System.Threading.Tasks;
namespace Sia.Gateway.Requests
{
public class GetIncidentsByTicketCreateIfNeededRequest : AuthenticatedRequest<IEnumerable<Incident>>
public class GetIncidentsByTicketCreateIfNeededRequest
: AuthenticatedRequest<IEnumerable<Incident>>
{
public GetIncidentsByTicketCreateIfNeededRequest(string ticketId, AuthenticatedUserContext userContext)
: base(userContext)
public GetIncidentsByTicketCreateIfNeededRequest(
string ticketId,
AuthenticatedUserContext userContext
) : base(userContext)
{
TicketId = ticketId;
}
@ -25,23 +29,32 @@ namespace Sia.Gateway.Requests
}
public class GetIncidentsByTicketCreateIfNeededRequestHandler : IncidentContextHandler<GetIncidentsByTicketCreateIfNeededRequest, IEnumerable<Incident>>
public class GetIncidentsByTicketCreateIfNeededRequestHandler
: IncidentConnectorHandler<
GetIncidentsByTicketCreateIfNeededRequest,
IEnumerable<Incident>
>
{
public GetIncidentsByTicketCreateIfNeededRequestHandler(IncidentContext context)
:base(context)
{
}
public GetIncidentsByTicketCreateIfNeededRequestHandler(
IncidentContext context,
Connector connector
) :base(context, connector){}
public override async Task<IEnumerable<Incident>> Handle(GetIncidentsByTicketCreateIfNeededRequest message)
public override async Task<IEnumerable<Incident>> Handle(
GetIncidentsByTicketCreateIfNeededRequest message
)
{
var incidents = await _context.Incidents
.WithEagerLoading()
.Where(incident => incident.Tickets.Any(inc => inc.OriginId == message.TicketId))
.Where(incident => incident
.Tickets
.Any(inc => inc.OriginId == message.TicketId))
.ProjectTo<Incident>().ToListAsync();
if (incidents.Any())
{
AttachTickets(incidents);
return incidents;
}
@ -59,7 +72,21 @@ namespace Sia.Gateway.Requests
var result = _context.Incidents.Add(dataIncident);
await _context.SaveChangesAsync();
return new List<Incident> { Mapper.Map<Incident>(result.Entity) };
var incidentDto = Mapper.Map<Incident>(result.Entity);
await AttachTickets(incidentDto);
return new List<Incident> { incidentDto };
}
private async Task<IEnumerable<Incident>> ConnectIncidents(List<Incident> incidents)
{
foreach (var incident in incidents)
{
incident.Tickets = (await _connector
.GetData(incident.Tickets.ToList()
)).ToList();
}
return incidents;
}
}
}

Просмотреть файл

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sia.Data.Incidents;
using Sia.Connectors.Tickets;
using MediatR;
using Sia.Domain;
namespace Sia.Gateway.Requests
{
public abstract class IncidentConnectorHandler<TRequest, TResult>
: IncidentContextHandler<TRequest, TResult>
where TRequest : IRequest<TResult>
{
protected readonly Connector _connector;
protected IncidentConnectorHandler(
IncidentContext context,
Connector connector
) : base(context)
{
_connector = connector;
}
protected async Task AttachTickets(Incident incident)
=> incident.Tickets = (await _connector
.GetData(incident.Tickets.AsEnumerable())
).ToList();
protected void AttachTickets(List<Incident> incidents)
=> Task.WaitAll(
incidents
.Select(inc => AttachTickets(inc))
.ToArray()
);
}
}

Просмотреть файл

@ -24,8 +24,12 @@ namespace Sia.Gateway.Tests.Requests
Id = expectedIncidentId,
Title = expectedIncidentTitle
};
var serviceUnderTest = new GetIncidentHandler<EmptyTicket>(await MockFactory.IncidentContext("Get"), new NoConnector(new NoClient(), new NoConverter()));
var request = new GetIncidentRequest(expectedIncidentId, new DummyAuthenticatedUserContext());
var serviceUnderTest = new GetIncidentHandler(
await MockFactory.IncidentContext("Get"),
new NoConnector(new NoClient(), null));
var request = new GetIncidentRequest(
expectedIncidentId,
new DummyAuthenticatedUserContext());
var result = await serviceUnderTest.Handle(request);

Просмотреть файл

@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sia.Connectors.Tickets.None;
using Sia.Domain;
using Sia.Gateway.Initialization;
using Sia.Gateway.Requests;
@ -21,7 +22,10 @@ namespace Sia.Gateway.Tests.Requests
[TestMethod]
public async Task Handle_WhenIncidentNotExist_ReturnNewIncident()
{
var serviceUnderTest = new GetIncidentsByTicketCreateIfNeededRequestHandler(await MockFactory.IncidentContext("Get"));
var serviceUnderTest = new GetIncidentsByTicketCreateIfNeededRequestHandler(
await MockFactory.IncidentContext("Get"),
new NoConnector(new NoClient(), null)
);
var request = new GetIncidentsByTicketCreateIfNeededRequest("100", new DummyAuthenticatedUserContext());
@ -36,7 +40,10 @@ namespace Sia.Gateway.Tests.Requests
public async Task Handle_WhenIncidentExists_ReturnCorrectIncidents()
{
var serviceUnderTest = new GetIncidentsByTicketCreateIfNeededRequestHandler(await MockFactory.IncidentContext("Get"));
var serviceUnderTest = new GetIncidentsByTicketCreateIfNeededRequestHandler(
await MockFactory.IncidentContext("Get"),
new NoConnector(new NoClient(), null)
);
var request = new GetIncidentsByTicketCreateIfNeededRequest("44444444", new DummyAuthenticatedUserContext());
var result = (await serviceUnderTest.Handle(request)).ToList();

Просмотреть файл

@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sia.Connectors.Tickets.None;
using Sia.Domain;
using Sia.Gateway.Initialization;
using Sia.Gateway.Requests;
@ -34,7 +35,10 @@ namespace Sia.Gateway.Tests.Requests
Title = expectedIncidentTitles[i]
};
}
var serviceUnderTest = new GetIncidentsHandler(await MockFactory.IncidentContext("Get"));
var serviceUnderTest = new GetIncidentsHandler(
await MockFactory.IncidentContext("Get"),
new NoConnector(new NoClient(), null)
);
var request = new GetIncidentsRequest(new DummyAuthenticatedUserContext());