Switched broke repositories up into separate handlers

This commit is contained in:
Philip Dimitratos 2017-10-18 12:12:36 -07:00
Родитель b4d5561f19
Коммит 7a0d52af91
12 изменённых файлов: 168 добавлений и 162 удалений

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

@ -13,13 +13,9 @@ using Sia.Connectors.Tickets.TicketProxy;
using Sia.Data.Incidents;
using Sia.Gateway.Authentication;
using Sia.Gateway.Requests;
using Sia.Gateway.ServiceRepositories;
using Sia.Shared.Authentication;
using Sia.Shared.Validation;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Runtime.Loader;
using Sia.Domain;
@ -39,9 +35,6 @@ namespace Sia.Gateway.Initialization
services.AddTicketingConnector(env, config);
services.AddScoped<IEventRepository, EventRepository>();
services.AddScoped<IEngagementRepository, EngagementRepository>();
services.AddSingleton<IConfigurationRoot>(i => config);
services.AddSingleton<AzureActiveDirectoryAuthenticationInfo>(i => incidentAuthConfig);
}

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

@ -1,9 +1,13 @@
using MediatR;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Gateway.Authentication;
using Sia.Gateway.ServiceRepositories;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Sia.Gateway.Requests
{
public class GetEngagementRequest : AuthenticatedRequest, IRequest<Engagement>
@ -17,4 +21,24 @@ namespace Sia.Gateway.Requests
public long Id { get; }
public long IncidentId { get; }
}
public class GetEngagementHandler
: IAsyncRequestHandler<GetEngagementRequest, Engagement>
{
private readonly IncidentContext _context;
public GetEngagementHandler(IncidentContext context)
{
_context = context;
}
public async Task<Engagement> Handle(GetEngagementRequest request)
{
var EngagementRecord = await _context.Engagements
.Include(en => en.Participant)
.FirstOrDefaultAsync(ev => ev.IncidentId == request.IncidentId && ev.Id == request.Id);
if (EngagementRecord == null) throw new KeyNotFoundException();
return Mapper.Map<Engagement>(EngagementRecord);
}
}
}

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

@ -1,8 +1,12 @@
using MediatR;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Domain.ApiModels;
using Sia.Gateway.Authentication;
using Sia.Gateway.ServiceRepositories;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Sia.Gateway.Requests
@ -19,4 +23,33 @@ namespace Sia.Gateway.Requests
public NewEngagement NewEngagement { get; }
public long IncidentId { get; }
}
public class PostEngagementHandler
: IAsyncRequestHandler<PostEngagementRequest, Engagement>
{
private readonly IncidentContext _context;
public PostEngagementHandler(IncidentContext context)
{
_context = context;
}
public async Task<Engagement> Handle(PostEngagementRequest request)
{
if (request.NewEngagement == null) throw new ArgumentNullException(nameof(request.NewEngagement));
var dataIncident = await _context.Incidents
.Include(cr => cr.Engagements)
.ThenInclude(en => en.Participant)
.FirstOrDefaultAsync(x => x.Id == request.IncidentId);
if (dataIncident == null) throw new KeyNotFoundException();
var dataEngagement = Mapper.Map<Data.Incidents.Models.Engagement>(request.NewEngagement);
dataEngagement.TimeEngaged = DateTime.UtcNow;
dataIncident.Engagements.Add(dataEngagement);
await _context.SaveChangesAsync();
return Mapper.Map<Engagement>(dataEngagement);
}
}
}

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

@ -1,10 +1,13 @@
using MediatR;
using Sia.Data.Incidents.Models;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sia.Data.Incidents;
using Sia.Domain.ApiModels;
using Sia.Gateway.Authentication;
using Sia.Gateway.ServiceRepositories;
using System;
using System.Threading.Tasks;
namespace Sia.Gateway.Requests
{
public class PutEngagementRequest : AuthenticatedRequest, IRequest
@ -20,4 +23,26 @@ namespace Sia.Gateway.Requests
public long EngagementId { get; }
public long IncidentId { get; }
}
public class PutEngagementHandler
: IAsyncRequestHandler<PutEngagementRequest>
{
private readonly IncidentContext _context;
public PutEngagementHandler(IncidentContext context)
{
_context = context;
}
public async Task Handle(PutEngagementRequest request)
{
if (request.UpdateEngagement == null) throw new ArgumentNullException(nameof(UpdateEngagement));
var existingRecord = await _context.Engagements
.Include(en => en.Participant)
.FirstOrDefaultAsync(engagement => engagement.IncidentId == request.IncidentId && engagement.Id == request.EngagementId);
var updatedModel = Mapper.Map(request.UpdateEngagement, existingRecord);
await _context.SaveChangesAsync();
}
}
}

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

@ -1,8 +1,10 @@
using MediatR;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Gateway.Authentication;
using Sia.Gateway.Requests.Events;
using Sia.Gateway.ServiceRepositories;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Sia.Gateway.Requests
@ -19,4 +21,25 @@ namespace Sia.Gateway.Requests
public long Id { get; }
public long IncidentId { get; }
}
public class GetEventHandler : IAsyncRequestHandler<GetEventRequest, Event>
{
private readonly IncidentContext _context;
public GetEventHandler(IncidentContext context)
{
_context = context;
}
public async Task<Event> Handle(GetEventRequest request)
{
var eventRecord = await _context
.Events
.FirstOrDefaultAsync( ev
=> ev.IncidentId == request.IncidentId
&& ev.Id == request.Id);
if (eventRecord == null) throw new KeyNotFoundException();
return Mapper.Map<Event>(eventRecord);
}
}
}

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

@ -1,12 +1,13 @@
using MediatR;
using Sia.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sia.Gateway.Authentication;
using Sia.Gateway.Protocol;
using Sia.Gateway.ServiceRepositories;
using Sia.Data.Incidents;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;
namespace Sia.Gateway.Requests.Events
{
@ -22,4 +23,21 @@ namespace Sia.Gateway.Requests.Events
public long IncidentId { get; }
public PaginationMetadata Pagination { get; }
}
public class GetEventsHandler
: IAsyncRequestHandler<GetEventsRequest, IEnumerable<Event>>
{
private readonly IncidentContext _context;
public GetEventsHandler(IncidentContext context)
{
_context = context;
}
public async Task<IEnumerable<Event>> Handle(GetEventsRequest request)
=> await _context.Events
.Where(ev => ev.IncidentId == request.IncidentId)
.WithPagination(request.Pagination)
.ProjectTo<Event>()
.ToListAsync();
}
}

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

@ -1,8 +1,12 @@
using MediatR;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Domain.ApiModels;
using Sia.Gateway.Authentication;
using Sia.Gateway.ServiceRepositories;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Sia.Gateway.Requests
@ -19,4 +23,31 @@ namespace Sia.Gateway.Requests
public NewEvent NewEvent { get; }
public long IncidentId { get; }
}
public class PostEventHandler : IAsyncRequestHandler<PostEventRequest, Event>
{
private readonly IncidentContext _context;
public PostEventHandler(IncidentContext context)
{
_context = context;
}
public async Task<Event> Handle(PostEventRequest request)
{
if (request.NewEvent == null) throw new ArgumentNullException(nameof(request.NewEvent));
var dataCrisis = await _context
.Incidents
.Include(cr => cr.Events)
.FirstOrDefaultAsync(x => x.Id == request.IncidentId);
if (dataCrisis == null) throw new KeyNotFoundException();
var dataEvent = Mapper.Map<Data.Incidents.Models.Event>(request.NewEvent);
dataCrisis.Events.Add(dataEvent);
await _context.SaveChangesAsync();
return Mapper.Map<Event>(dataEvent);
}
}
}

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

@ -4,7 +4,6 @@ using Sia.Connectors.Tickets;
using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Gateway.Authentication;
using Sia.Gateway.ServiceRepositories;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

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

@ -4,7 +4,6 @@ using Microsoft.EntityFrameworkCore;
using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Gateway.Authentication;
using Sia.Gateway.ServiceRepositories;
using System.Collections.Generic;
using System.Threading.Tasks;

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

@ -4,10 +4,8 @@ using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Domain.ApiModels;
using Sia.Gateway.Authentication;
using Sia.Gateway.ServiceRepositories;
using Sia.Shared.Exceptions;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Sia.Gateway.Requests

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

@ -1,71 +0,0 @@
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Domain.ApiModels;
using Sia.Gateway.Requests;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Sia.Gateway.ServiceRepositories
{
public interface IEngagementRepository
:IAsyncRequestHandler<GetEngagementRequest, Engagement>,
IAsyncRequestHandler<PostEngagementRequest, Engagement>,
IAsyncRequestHandler<PutEngagementRequest>
{
}
public class EngagementRepository : IEngagementRepository
{
private readonly IncidentContext _context;
public EngagementRepository(IncidentContext context)
{
_context = context;
}
public async Task<Engagement> Handle(GetEngagementRequest request)
{
var EngagementRecord = await _context.Engagements
.Include(en => en.Participant)
.FirstOrDefaultAsync(ev => ev.IncidentId == request.IncidentId && ev.Id == request.Id);
if (EngagementRecord == null) throw new KeyNotFoundException();
return Mapper.Map<Engagement>(EngagementRecord);
}
public async Task<Engagement> Handle (PostEngagementRequest request)
{
if (request.NewEngagement == null) throw new ArgumentNullException(nameof(request.NewEngagement));
var dataIncident = await _context.Incidents
.Include(cr => cr.Engagements)
.ThenInclude(en => en.Participant)
.FirstOrDefaultAsync(x => x.Id == request.IncidentId);
if (dataIncident == null) throw new KeyNotFoundException();
var dataEngagement = Mapper.Map<Data.Incidents.Models.Engagement>(request.NewEngagement);
dataEngagement.TimeEngaged = DateTime.UtcNow;
dataIncident.Engagements.Add(dataEngagement);
await _context.SaveChangesAsync();
return Mapper.Map<Engagement>(dataEngagement);
}
public async Task Handle(PutEngagementRequest request)
{
if (request.UpdateEngagement == null) throw new ArgumentNullException(nameof(UpdateEngagement));
var existingRecord = await _context.Engagements
.Include(en => en.Participant)
.FirstOrDefaultAsync(engagement => engagement.IncidentId == request.IncidentId && engagement.Id == request.EngagementId);
var updatedModel = Mapper.Map(request.UpdateEngagement, existingRecord);
await _context.SaveChangesAsync();
}
}
}

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

@ -1,66 +0,0 @@
using AutoMapper;
using AutoMapper.QueryableExtensions;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Sia.Data.Incidents;
using Sia.Domain;
using Sia.Gateway.Requests;
using Sia.Gateway.Requests.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Sia.Gateway.ServiceRepositories
{
public interface IEventRepository
: IAsyncRequestHandler<GetEventRequest, Event>,
IAsyncRequestHandler<PostEventRequest, Event>,
IAsyncRequestHandler<GetEventsRequest, IEnumerable<Event>>
{
}
public class EventRepository : IEventRepository
{
private readonly IncidentContext _context;
public EventRepository(IncidentContext context)
{
_context = context;
}
public async Task<Event> Handle(GetEventRequest request)
{
var eventRecord = await _context.Events.FirstOrDefaultAsync(ev => ev.IncidentId == request.IncidentId && ev.Id == request.Id);
if (eventRecord == null) throw new KeyNotFoundException();
return Mapper.Map<Event>(eventRecord);
}
public async Task<IEnumerable<Event>> Handle(GetEventsRequest request)
=> await _context.Events
.Where(ev => ev.IncidentId == request.IncidentId)
.WithPagination(request.Pagination)
.ProjectTo<Event>()
.ToListAsync();
public async Task<Event> Handle(PostEventRequest request)
{
if (request.NewEvent == null) throw new ArgumentNullException(nameof(request.NewEvent));
var dataCrisis = await _context.Incidents
.Include(cr => cr.Events)
.FirstOrDefaultAsync(x => x.Id == request.IncidentId);
if (dataCrisis == null) throw new KeyNotFoundException();
var dataEvent = Mapper.Map<Data.Incidents.Models.Event>(request.NewEvent);
dataCrisis.Events.Add(dataEvent);
await _context.SaveChangesAsync();
return Mapper.Map<Event>(dataEvent);
}
}
}