-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4792e7
commit 82fb361
Showing
5 changed files
with
425 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
Blueprint.Api/Infrastructure/EventHandlers/CatalogHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2024 Carnegie Mellon University. All Rights Reserved. | ||
// Released under a MIT (SEI)-style license, please see LICENSE.md in the project root for license information or contact [email protected] for full terms. | ||
|
||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using MediatR; | ||
using Microsoft.AspNetCore.SignalR; | ||
using Microsoft.EntityFrameworkCore; | ||
using Blueprint.Api.Data; | ||
using Blueprint.Api.Data.Models; | ||
using Blueprint.Api.Services; | ||
using Blueprint.Api.Hubs; | ||
using Blueprint.Api.Infrastructure.Extensions; | ||
|
||
namespace Blueprint.Api.Infrastructure.EventHandlers | ||
{ | ||
public class CatalogHandler | ||
{ | ||
protected readonly BlueprintContext _db; | ||
protected readonly IMapper _mapper; | ||
protected readonly ICatalogService _CatalogService; | ||
protected readonly IHubContext<MainHub> _mainHub; | ||
|
||
public CatalogHandler( | ||
BlueprintContext db, | ||
IMapper mapper, | ||
ICatalogService CatalogService, | ||
IHubContext<MainHub> mainHub) | ||
{ | ||
_db = db; | ||
_mapper = mapper; | ||
_CatalogService = CatalogService; | ||
_mainHub = mainHub; | ||
} | ||
|
||
protected async Task<string[]> GetGroups(CatalogEntity CatalogEntity) | ||
{ | ||
var groupIds = await _db.CatalogUnits | ||
.Where(m => m.CatalogId == CatalogEntity.Id) | ||
.Select(m => m.UnitId.ToString()) | ||
.ToListAsync(); | ||
// the admin data group gets everything | ||
groupIds.Add(MainHub.ADMIN_DATA_GROUP); | ||
|
||
return groupIds.ToArray(); | ||
} | ||
|
||
protected async Task HandleCreateOrUpdate( | ||
CatalogEntity CatalogEntity, | ||
string method, | ||
string[] modifiedProperties, | ||
CancellationToken cancellationToken) | ||
{ | ||
var groupIds = await GetGroups(CatalogEntity); | ||
var Catalog = _mapper.Map<ViewModels.Catalog>(CatalogEntity); | ||
var tasks = new List<Task>(); | ||
|
||
foreach (var groupId in groupIds) | ||
{ | ||
tasks.Add(_mainHub.Clients.Group(groupId).SendAsync(method, Catalog, modifiedProperties, cancellationToken)); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
} | ||
} | ||
|
||
public class CatalogCreatedSignalRHandler : CatalogHandler, INotificationHandler<EntityCreated<CatalogEntity>> | ||
{ | ||
public CatalogCreatedSignalRHandler( | ||
BlueprintContext db, | ||
IMapper mapper, | ||
ICatalogService CatalogService, | ||
IHubContext<MainHub> mainHub) : base(db, mapper, CatalogService, mainHub) { } | ||
|
||
public async Task Handle(EntityCreated<CatalogEntity> notification, CancellationToken cancellationToken) | ||
{ | ||
await base.HandleCreateOrUpdate(notification.Entity, MainHubMethods.CatalogCreated, null, cancellationToken); | ||
} | ||
} | ||
|
||
public class CatalogUpdatedSignalRHandler : CatalogHandler, INotificationHandler<EntityUpdated<CatalogEntity>> | ||
{ | ||
public CatalogUpdatedSignalRHandler( | ||
BlueprintContext db, | ||
IMapper mapper, | ||
ICatalogService CatalogService, | ||
IHubContext<MainHub> mainHub) : base(db, mapper, CatalogService, mainHub) { } | ||
|
||
public async Task Handle(EntityUpdated<CatalogEntity> notification, CancellationToken cancellationToken) | ||
{ | ||
await base.HandleCreateOrUpdate( | ||
notification.Entity, | ||
MainHubMethods.CatalogUpdated, | ||
notification.ModifiedProperties.Select(x => x.TitleCaseToCamelCase()).ToArray(), | ||
cancellationToken); | ||
} | ||
} | ||
|
||
public class CatalogDeletedSignalRHandler : CatalogHandler, INotificationHandler<EntityDeleted<CatalogEntity>> | ||
{ | ||
public CatalogDeletedSignalRHandler( | ||
BlueprintContext db, | ||
IMapper mapper, | ||
ICatalogService CatalogService, | ||
IHubContext<MainHub> mainHub) : base(db, mapper, CatalogService, mainHub) | ||
{ | ||
} | ||
|
||
public async Task Handle(EntityDeleted<CatalogEntity> notification, CancellationToken cancellationToken) | ||
{ | ||
var groupIds = await base.GetGroups(notification.Entity); | ||
var tasks = new List<Task>(); | ||
|
||
foreach (var groupId in groupIds) | ||
{ | ||
tasks.Add(_mainHub.Clients.Group(groupId).SendAsync(MainHubMethods.CatalogDeleted, notification.Entity.Id, cancellationToken)); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
} | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
Blueprint.Api/Infrastructure/EventHandlers/InjectHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2024 Carnegie Mellon University. All Rights Reserved. | ||
// Released under a MIT (SEI)-style license, please see LICENSE.md in the project root for license information or contact [email protected] for full terms. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using MediatR; | ||
using Microsoft.AspNetCore.SignalR; | ||
using Blueprint.Api.Data; | ||
using Blueprint.Api.Data.Models; | ||
using Blueprint.Api.Services; | ||
using Blueprint.Api.Hubs; | ||
using Blueprint.Api.Infrastructure.Extensions; | ||
|
||
namespace Blueprint.Api.Infrastructure.EventHandlers | ||
{ | ||
public class InjectHandler | ||
{ | ||
protected readonly BlueprintContext _db; | ||
protected readonly IMapper _mapper; | ||
protected readonly IInjectService _InjectService; | ||
protected readonly IHubContext<MainHub> _mainHub; | ||
|
||
public InjectHandler( | ||
BlueprintContext db, | ||
IMapper mapper, | ||
IInjectService InjectService, | ||
IHubContext<MainHub> mainHub) | ||
{ | ||
_db = db; | ||
_mapper = mapper; | ||
_InjectService = InjectService; | ||
_mainHub = mainHub; | ||
} | ||
|
||
protected string[] GetGroups(InjectEntity InjectEntity) | ||
{ | ||
var groupIds = new List<string>(); | ||
// the admin data group gets everything | ||
groupIds.Add(MainHub.ADMIN_DATA_GROUP); | ||
|
||
return groupIds.ToArray(); | ||
} | ||
|
||
protected async Task HandleCreateOrUpdate( | ||
InjectEntity InjectEntity, | ||
string method, | ||
string[] modifiedProperties, | ||
CancellationToken cancellationToken) | ||
{ | ||
var groupIds = GetGroups(InjectEntity); | ||
var Inject = _mapper.Map<ViewModels.Injectm>(InjectEntity); | ||
var tasks = new List<Task>(); | ||
|
||
foreach (var groupId in groupIds) | ||
{ | ||
tasks.Add(_mainHub.Clients.Group(groupId).SendAsync(method, Inject, modifiedProperties, cancellationToken)); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
} | ||
} | ||
|
||
public class InjectCreatedSignalRHandler : InjectHandler, INotificationHandler<EntityCreated<InjectEntity>> | ||
{ | ||
public InjectCreatedSignalRHandler( | ||
BlueprintContext db, | ||
IMapper mapper, | ||
IInjectService InjectService, | ||
IHubContext<MainHub> mainHub) : base(db, mapper, InjectService, mainHub) { } | ||
|
||
public async Task Handle(EntityCreated<InjectEntity> notification, CancellationToken cancellationToken) | ||
{ | ||
await base.HandleCreateOrUpdate(notification.Entity, MainHubMethods.InjectCreated, null, cancellationToken); | ||
} | ||
} | ||
|
||
public class InjectUpdatedSignalRHandler : InjectHandler, INotificationHandler<EntityUpdated<InjectEntity>> | ||
{ | ||
public InjectUpdatedSignalRHandler( | ||
BlueprintContext db, | ||
IMapper mapper, | ||
IInjectService InjectService, | ||
IHubContext<MainHub> mainHub) : base(db, mapper, InjectService, mainHub) { } | ||
|
||
public async Task Handle(EntityUpdated<InjectEntity> notification, CancellationToken cancellationToken) | ||
{ | ||
await base.HandleCreateOrUpdate( | ||
notification.Entity, | ||
MainHubMethods.InjectUpdated, | ||
notification.ModifiedProperties.Select(x => x.TitleCaseToCamelCase()).ToArray(), | ||
cancellationToken); | ||
} | ||
} | ||
|
||
public class InjectDeletedSignalRHandler : InjectHandler, INotificationHandler<EntityDeleted<InjectEntity>> | ||
{ | ||
public InjectDeletedSignalRHandler( | ||
BlueprintContext db, | ||
IMapper mapper, | ||
IInjectService InjectService, | ||
IHubContext<MainHub> mainHub) : base(db, mapper, InjectService, mainHub) | ||
{ | ||
} | ||
|
||
public async Task Handle(EntityDeleted<InjectEntity> notification, CancellationToken cancellationToken) | ||
{ | ||
var groupIds = base.GetGroups(notification.Entity); | ||
var tasks = new List<Task>(); | ||
|
||
foreach (var groupId in groupIds) | ||
{ | ||
tasks.Add(_mainHub.Clients.Group(groupId).SendAsync(MainHubMethods.InjectDeleted, notification.Entity.Id, cancellationToken)); | ||
} | ||
|
||
await Task.WhenAll(tasks); | ||
} | ||
} | ||
} |
Oops, something went wrong.