diff --git a/src/Sia.Gateway/Initialization/ServicesStartup.cs b/src/Sia.Gateway/Initialization/ServicesStartup.cs index 2368ce6..0742de1 100644 --- a/src/Sia.Gateway/Initialization/ServicesStartup.cs +++ b/src/Sia.Gateway/Initialization/ServicesStartup.cs @@ -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(); - services.AddScoped(); - services.AddSingleton(i => config); services.AddSingleton(i => incidentAuthConfig); } diff --git a/src/Sia.Gateway/Requests/Engagements/GetEngagement.cs b/src/Sia.Gateway/Requests/Engagements/GetEngagement.cs index 4314011e..44838ef 100644 --- a/src/Sia.Gateway/Requests/Engagements/GetEngagement.cs +++ b/src/Sia.Gateway/Requests/Engagements/GetEngagement.cs @@ -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 @@ -17,4 +21,24 @@ namespace Sia.Gateway.Requests public long Id { get; } public long IncidentId { get; } } + + public class GetEngagementHandler + : IAsyncRequestHandler + { + private readonly IncidentContext _context; + + public GetEngagementHandler(IncidentContext context) + { + _context = context; + } + public async Task 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(EngagementRecord); + } + } } diff --git a/src/Sia.Gateway/Requests/Engagements/PostEngagement.cs b/src/Sia.Gateway/Requests/Engagements/PostEngagement.cs index a276acb..0736f6b 100644 --- a/src/Sia.Gateway/Requests/Engagements/PostEngagement.cs +++ b/src/Sia.Gateway/Requests/Engagements/PostEngagement.cs @@ -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 + { + private readonly IncidentContext _context; + + public PostEngagementHandler(IncidentContext context) + { + _context = context; + } + public async Task 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(request.NewEngagement); + dataEngagement.TimeEngaged = DateTime.UtcNow; + + dataIncident.Engagements.Add(dataEngagement); + await _context.SaveChangesAsync(); + + return Mapper.Map(dataEngagement); + } + } } diff --git a/src/Sia.Gateway/Requests/Engagements/PutEngagement.cs b/src/Sia.Gateway/Requests/Engagements/PutEngagement.cs index b47b2d4..a8caff4 100644 --- a/src/Sia.Gateway/Requests/Engagements/PutEngagement.cs +++ b/src/Sia.Gateway/Requests/Engagements/PutEngagement.cs @@ -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 + { + 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(); + } + } } diff --git a/src/Sia.Gateway/Requests/Events/GetEvent.cs b/src/Sia.Gateway/Requests/Events/GetEvent.cs index c186ef1..4a4c274 100644 --- a/src/Sia.Gateway/Requests/Events/GetEvent.cs +++ b/src/Sia.Gateway/Requests/Events/GetEvent.cs @@ -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 + { + private readonly IncidentContext _context; + + public GetEventHandler(IncidentContext context) + { + _context = context; + } + public async Task 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(eventRecord); + } + } } diff --git a/src/Sia.Gateway/Requests/Events/GetEvents.cs b/src/Sia.Gateway/Requests/Events/GetEvents.cs index 4df4bad..1d699b2 100644 --- a/src/Sia.Gateway/Requests/Events/GetEvents.cs +++ b/src/Sia.Gateway/Requests/Events/GetEvents.cs @@ -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> + { + private readonly IncidentContext _context; + + public GetEventsHandler(IncidentContext context) + { + _context = context; + } + public async Task> Handle(GetEventsRequest request) + => await _context.Events + .Where(ev => ev.IncidentId == request.IncidentId) + .WithPagination(request.Pagination) + .ProjectTo() + .ToListAsync(); + } } diff --git a/src/Sia.Gateway/Requests/Events/PostEvent.cs b/src/Sia.Gateway/Requests/Events/PostEvent.cs index 97a28ba..2705364 100644 --- a/src/Sia.Gateway/Requests/Events/PostEvent.cs +++ b/src/Sia.Gateway/Requests/Events/PostEvent.cs @@ -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 + { + private readonly IncidentContext _context; + + public PostEventHandler(IncidentContext context) + { + _context = context; + } + public async Task 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(request.NewEvent); + + dataCrisis.Events.Add(dataEvent); + await _context.SaveChangesAsync(); + + return Mapper.Map(dataEvent); + } + } } diff --git a/src/Sia.Gateway/Requests/Incidents/GetIncident.cs b/src/Sia.Gateway/Requests/Incidents/GetIncident.cs index 973a30f..6bfb5b7 100644 --- a/src/Sia.Gateway/Requests/Incidents/GetIncident.cs +++ b/src/Sia.Gateway/Requests/Incidents/GetIncident.cs @@ -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; diff --git a/src/Sia.Gateway/Requests/Incidents/GetIncidents.cs b/src/Sia.Gateway/Requests/Incidents/GetIncidents.cs index 5032c98..e2fea41 100644 --- a/src/Sia.Gateway/Requests/Incidents/GetIncidents.cs +++ b/src/Sia.Gateway/Requests/Incidents/GetIncidents.cs @@ -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; diff --git a/src/Sia.Gateway/Requests/Incidents/PostIncident.cs b/src/Sia.Gateway/Requests/Incidents/PostIncident.cs index ef67ed4..a7c2a2a 100644 --- a/src/Sia.Gateway/Requests/Incidents/PostIncident.cs +++ b/src/Sia.Gateway/Requests/Incidents/PostIncident.cs @@ -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 diff --git a/src/Sia.Gateway/ServiceRepositories/IEngagementRepository.cs b/src/Sia.Gateway/ServiceRepositories/IEngagementRepository.cs deleted file mode 100644 index 9a78567..0000000 --- a/src/Sia.Gateway/ServiceRepositories/IEngagementRepository.cs +++ /dev/null @@ -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, - IAsyncRequestHandler, - IAsyncRequestHandler - { - } - - public class EngagementRepository : IEngagementRepository - { - private readonly IncidentContext _context; - - public EngagementRepository(IncidentContext context) - { - _context = context; - } - - public async Task 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(EngagementRecord); - } - - public async Task 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(request.NewEngagement); - dataEngagement.TimeEngaged = DateTime.UtcNow; - - dataIncident.Engagements.Add(dataEngagement); - await _context.SaveChangesAsync(); - - return Mapper.Map(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(); - } - } -} diff --git a/src/Sia.Gateway/ServiceRepositories/IEventRepository.cs b/src/Sia.Gateway/ServiceRepositories/IEventRepository.cs deleted file mode 100644 index e9af5d8..0000000 --- a/src/Sia.Gateway/ServiceRepositories/IEventRepository.cs +++ /dev/null @@ -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, - IAsyncRequestHandler, - IAsyncRequestHandler> - { - } - - public class EventRepository : IEventRepository - { - - private readonly IncidentContext _context; - - public EventRepository(IncidentContext context) - { - _context = context; - } - - public async Task 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(eventRecord); - } - - public async Task> Handle(GetEventsRequest request) - => await _context.Events - .Where(ev => ev.IncidentId == request.IncidentId) - .WithPagination(request.Pagination) - .ProjectTo() - .ToListAsync(); - - - public async Task 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(request.NewEvent); - - dataCrisis.Events.Add(dataEvent); - await _context.SaveChangesAsync(); - - return Mapper.Map(dataEvent); - } - } -}