Skip to content

Commit

Permalink
download and upload of catalogs
Browse files Browse the repository at this point in the history
  • Loading branch information
sei-tspencer committed Jul 24, 2024
1 parent e4792e7 commit 82fb361
Show file tree
Hide file tree
Showing 5 changed files with 425 additions and 10 deletions.
36 changes: 28 additions & 8 deletions Blueprint.Api/Hubs/MainHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Blueprint.Api.Services;
using Blueprint.Api.Infrastructure.Authorization;
using Blueprint.Api.Infrastructure.Options;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;

namespace Blueprint.Api.Hubs
{
Expand Down Expand Up @@ -49,6 +48,7 @@ public async Task Join()
{
var userId = Context.User.Identities.First().Claims.First(c => c.Type == "sub")?.Value;
var idList = await GetMselIdList(userId);
idList.AddRange(await GetUnitIdList(userId));
idList.Add(userId);
foreach (var id in idList)
{
Expand Down Expand Up @@ -118,15 +118,15 @@ private async Task<List<string>> GetMselIdList(string userId)
}
else
{
var teamIdList = await _context.TeamUsers
var unitIdList = await _context.UnitUsers
.Where(tu => tu.UserId == userGuid)
.Select(tu => tu.TeamId)
.Select(tu => tu.UnitId)
.ToListAsync();
// get my teams' msels
var teamMselIds = _context.Teams
.Where(t => teamIdList.Contains(t.Id) && t.Msel.Status != Data.Enumerations.MselItemStatus.Archived)
// get my unit's msels
var unitMselIds = _context.MselUnits
.Where(t => unitIdList.Contains(t.Id) && t.Msel.Status != Data.Enumerations.MselItemStatus.Archived)
.Select(t => t.Msel.Id.ToString());
var teamMselIdList = await teamMselIds
var unitMselIdList = await unitMselIds
.Where(id => id != null)
.ToListAsync();
// get msels I created and all templates
Expand All @@ -135,13 +135,24 @@ private async Task<List<string>> GetMselIdList(string userId)
.Select(m => m.Id.ToString())
.ToListAsync();
// combine lists
var mselIdList = teamMselIdList.Union(myMselIdList);
var mselIdList = unitMselIdList.Union(myMselIdList);
idList.AddRange(mselIdList);
}

return idList;
}

private async Task<List<string>> GetUnitIdList(string userId)
{
var userGuid = Guid.Parse(userId);
var unitIdList = await _context.UnitUsers
.Where(tu => tu.UserId == userGuid)
.Select(tu => tu.UnitId.ToString())
.ToListAsync();

return unitIdList;
}

private async Task<List<string>> GetAdminIdList()
{
var idList = new List<string>();
Expand All @@ -166,6 +177,9 @@ public static class MainHubMethods
public const string CardTeamCreated = "CardTeamCreated";
public const string CardTeamUpdated = "CardTeamUpdated";
public const string CardTeamDeleted = "CardTeamDeleted";
public const string CatalogCreated = "CatalogCreated";
public const string CatalogUpdated = "CatalogUpdated";
public const string CatalogDeleted = "CatalogDeleted";
public const string CiteActionCreated = "CiteActionCreated";
public const string CiteActionUpdated = "CiteActionUpdated";
public const string CiteActionDeleted = "CiteActionDeleted";
Expand All @@ -178,6 +192,12 @@ public static class MainHubMethods
public const string DataValueCreated = "DataValueCreated";
public const string DataValueUpdated = "DataValueUpdated";
public const string DataValueDeleted = "DataValueDeleted";
public const string InjectCreated = "InjectCreated";
public const string InjectUpdated = "InjectUpdated";
public const string InjectDeleted = "InjectDeleted";
public const string InjectTypeCreated = "InjectTypeCreated";
public const string InjectTypeUpdated = "InjectTypeUpdated";
public const string InjectTypeDeleted = "InjectTypeDeleted";
public const string MselCreated = "MselCreated";
public const string MselUpdated = "MselUpdated";
public const string MselDeleted = "MselDeleted";
Expand Down
126 changes: 126 additions & 0 deletions Blueprint.Api/Infrastructure/EventHandlers/CatalogHandler.cs
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 Blueprint.Api/Infrastructure/EventHandlers/InjectHandler.cs
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);
}
}
}
Loading

0 comments on commit 82fb361

Please sign in to comment.