Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
kjetilhau committed Oct 30, 2024
1 parent 9370be3 commit f05001c
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@

namespace Equinor.ProjectExecutionPortal.Application.Cache;

public class FusionAppsCache(ICacheManager cacheManager, IAppsClient fusionAppsClient) : IFusionAppsCache
public class FusionAppsCache : IFusionAppsCache
{
private readonly ICacheManager _cacheManager;
private readonly IAppsClient _fusionAppsClient;

public FusionAppsCache(ICacheManager cacheManager, IAppsClient fusionAppsClient)
{
_cacheManager = cacheManager;
_fusionAppsClient = fusionAppsClient;
}

// TODO: Move cache duration to app settings

public async Task<List<App>> GetFusionApps()
{
return await cacheManager.GetOrCreateAsync("FUSION_APP",
return await _cacheManager.GetOrCreateAsync("FUSION_APP",
async () =>
{
var fusionApps = await fusionAppsClient.GetAppsAsync();
var fusionApps = await _fusionAppsClient.GetAppsAsync();

return fusionApps.ToList();
},
Expand All @@ -22,9 +31,9 @@ public async Task<List<App>> GetFusionApps()

public async Task<App?> GetFusionApp(string appKey)
{
return await cacheManager.GetOrCreateAsync("FUSION_APP",
async () => await fusionAppsClient.GetAppAsync(appKey),
return await _cacheManager.GetOrCreateAsync("FUSION_APP",
async () => await _fusionAppsClient.GetAppAsync(appKey),
CacheDuration.Minutes,
60);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@

namespace Equinor.ProjectExecutionPortal.Application.Commands.ContextTypes.AddContextType;

public class AddContextTypeCommand(string contextTypeKey) : IRequest<Guid>
public class AddContextTypeCommand : IRequest<Guid>
{
public string ContextTypeKey { get; } = contextTypeKey;
public AddContextTypeCommand(string contextTypeKey)
{
ContextTypeKey = contextTypeKey;
}

public class Handler(IReadWriteContext readWriteContext) : IRequestHandler<AddContextTypeCommand, Guid>
public string ContextTypeKey { get; }

public class Handler : IRequestHandler<AddContextTypeCommand, Guid>
{
private readonly IReadWriteContext _readWriteContext;

public Handler(IReadWriteContext readWriteContext)
{
_readWriteContext = readWriteContext;
}

public async Task<Guid> Handle(AddContextTypeCommand command, CancellationToken cancellationToken)
{
var contextTypeExists = await readWriteContext.Set<ContextType>()
var contextTypeExists = await _readWriteContext.Set<ContextType>()
.AsNoTracking()
.AnyAsync(x => x.ContextTypeKey == command.ContextTypeKey, cancellationToken);

Expand All @@ -27,15 +39,15 @@ public async Task<Guid> Handle(AddContextTypeCommand command, CancellationToken
if (!FusionContextType.IsValid(command.ContextTypeKey))
{
throw new NotFoundException($"ContextType: {command.ContextTypeKey} is not a valid Context type");
}
}

var contextType = new ContextType(command.ContextTypeKey);

readWriteContext.Set<ContextType>().Add(contextType);
_readWriteContext.Set<ContextType>().Add(contextType);

await readWriteContext.SaveChangesAsync(cancellationToken);
await _readWriteContext.SaveChangesAsync(cancellationToken);

return contextType.Id;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,38 @@

namespace Equinor.ProjectExecutionPortal.Application.Commands.OnboardedApps.OnboardApp;

public class OnboardAppCommand(string appKey, IList<string>? contextTypes) : IRequest<Guid>
public class OnboardAppCommand : IRequest<Guid>
{
public string AppKey { get; } = appKey;
public IList<string>? ContextTypes { get; set; } = contextTypes;

public class Handler(
IReadWriteContext readWriteContext,
IFusionAppsService fusionAppsService,
IContextTypeService contextTypeService)
: IRequestHandler<OnboardAppCommand, Guid>
public OnboardAppCommand(string appKey, IList<string>? contextTypes)
{
AppKey = appKey;
ContextTypes = contextTypes;
}

public string AppKey { get; }
public IList<string>? ContextTypes { get; set; }

public class Handler : IRequestHandler<OnboardAppCommand, Guid>
{
private readonly IReadWriteContext _readWriteContext;
private readonly IFusionAppsService _fusionAppsService;
private readonly IContextTypeService _contextTypeService;

public Handler(IReadWriteContext readWriteContext, IFusionAppsService fusionAppsService, IContextTypeService contextTypeService)
{
_readWriteContext = readWriteContext;
_fusionAppsService = fusionAppsService;
_contextTypeService = contextTypeService;
}

public async Task<Guid> Handle(OnboardAppCommand command, CancellationToken cancellationToken)
{
if (!await fusionAppsService.FusionAppExist(command.AppKey, cancellationToken))
if (!await _fusionAppsService.FusionAppExist(command.AppKey, cancellationToken))
{
throw new NotFoundException($"Could not locate app '{command.AppKey}' in Fusion.");
}

var onboardedAppExists = await readWriteContext.Set<OnboardedApp>()
var onboardedAppExists = await _readWriteContext.Set<OnboardedApp>()
.AsNoTracking()
.AnyAsync(x => x.AppKey == command.AppKey, cancellationToken);

Expand All @@ -39,18 +52,18 @@ public async Task<Guid> Handle(OnboardAppCommand command, CancellationToken canc

try
{
onboardedApp.AddContextTypes(await contextTypeService.GetAllowedContextTypesByKeys(command.ContextTypes, cancellationToken));
onboardedApp.AddContextTypes(await _contextTypeService.GetAllowedContextTypesByKeys(command.ContextTypes, cancellationToken));
}
catch (InvalidActionException ex)
{
throw new InvalidOperationException(ex.Message);
}

readWriteContext.Set<OnboardedApp>().Add(onboardedApp);
_readWriteContext.Set<OnboardedApp>().Add(onboardedApp);

await readWriteContext.SaveChangesAsync(cancellationToken);
await _readWriteContext.SaveChangesAsync(cancellationToken);

return onboardedApp.Id;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,31 @@

namespace Equinor.ProjectExecutionPortal.Application.Commands.OnboardedApps.UpdateOnboardedApp;

public class UpdateOnboardedAppCommand(string appKey, IList<string>? contextTypes) : IRequest<Guid>
public class UpdateOnboardedAppCommand : IRequest<Guid>
{
public string AppKey { get; } = appKey;
public IList<string>? ContextTypes { get; set; } = contextTypes;
public UpdateOnboardedAppCommand(string appKey, IList<string>? contextTypes)
{
AppKey = appKey;
ContextTypes = contextTypes;
}

public class Handler(IReadWriteContext readWriteContext, IContextTypeService contextTypeService) : IRequestHandler<UpdateOnboardedAppCommand, Guid>
public string AppKey { get; }
public IList<string>? ContextTypes { get; set; }

public class Handler : IRequestHandler<UpdateOnboardedAppCommand, Guid>
{
private readonly IReadWriteContext _readWriteContext;
private readonly IContextTypeService _contextTypeService;

public Handler(IReadWriteContext readWriteContext, IContextTypeService contextTypeService)
{
_readWriteContext = readWriteContext;
_contextTypeService = contextTypeService;
}

public async Task<Guid> Handle(UpdateOnboardedAppCommand command, CancellationToken cancellationToken)
{
var entity = await readWriteContext.Set<OnboardedApp>()
var entity = await _readWriteContext.Set<OnboardedApp>()
.Include(x => x.ContextTypes)
.FirstOrDefaultAsync(x => x.AppKey == command.AppKey, cancellationToken);

Expand All @@ -27,16 +42,16 @@ public async Task<Guid> Handle(UpdateOnboardedAppCommand command, CancellationTo

try
{
entity.AddContextTypes(await contextTypeService.GetAllowedContextTypesByKeys(command.ContextTypes, cancellationToken));
entity.AddContextTypes(await _contextTypeService.GetAllowedContextTypesByKeys(command.ContextTypes, cancellationToken));
}
catch (InvalidActionException ex)
{
throw new InvalidOperationException(ex.Message);
}

await readWriteContext.SaveChangesAsync(cancellationToken);
await _readWriteContext.SaveChangesAsync(cancellationToken);

return entity.Id;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,49 @@

namespace Equinor.ProjectExecutionPortal.Application.Commands.Portals.AddContextAppToPortal;

public class AddContextAppToPortalCommand(Guid portalId, Guid contextId, string appKey) : IRequest<Unit>
public class AddContextAppToPortalCommand : IRequest<Unit>
{
public Guid PortalId { get; } = portalId;
public Guid ContextId { get; } = contextId;
public string AppKey { get; } = appKey;
public AddContextAppToPortalCommand(Guid portalId, Guid contextId, string appKey)
{
PortalId = portalId;
ContextId = contextId;
AppKey = appKey;
}

public class Handler(IReadWriteContext readWriteContext, IContextService contextService) : IRequestHandler<AddContextAppToPortalCommand, Unit>
public Guid PortalId { get; }
public Guid ContextId { get; }
public string AppKey { get; }

public class Handler : IRequestHandler<AddContextAppToPortalCommand, Unit>
{
private readonly IReadWriteContext _readWriteContext;
private readonly IContextService _contextService;

public Handler(IReadWriteContext readWriteContext, IContextService contextService)
{
_readWriteContext = readWriteContext;
_contextService = contextService;
}

public async Task<Unit> Handle(AddContextAppToPortalCommand command, CancellationToken cancellationToken)
{
var fusionContext = await contextService.GetFusionContext(command.ContextId, cancellationToken)
var fusionContext = await _contextService.GetFusionContext(command.ContextId, cancellationToken)

?? throw new InvalidOperationException($"Cannot add app '{command.AppKey} to '{command.PortalId}'. Missing context parameter.");

var onboardedContext = await readWriteContext.Set<OnboardedContext>()
var onboardedContext = await _readWriteContext.Set<OnboardedContext>()
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ExternalId == fusionContext.ExternalId && x.Type == fusionContext.Type.Name, cancellationToken)

?? throw new NotFoundException("Could not find any onboarded context with id", command.ContextId);

var onboardedApp = await readWriteContext.Set<OnboardedApp>()
var onboardedApp = await _readWriteContext.Set<OnboardedApp>()
.AsNoTracking()
.FirstOrDefaultAsync(x => x.AppKey == command.AppKey, cancellationToken)

?? throw new NotFoundException("Could not find any onboarded app with id", command.AppKey);

var portalWithAllApps = await readWriteContext.Set<Portal>()
var portalWithAllApps = await _readWriteContext.Set<Portal>()
.Include(x => x.Apps
.Where(wsApp => wsApp.OnboardedContext == null || wsApp.OnboardedContext.Id == onboardedContext.Id))
.ThenInclude(x => x.OnboardedContext)
Expand All @@ -55,9 +71,9 @@ public async Task<Unit> Handle(AddContextAppToPortalCommand command, Cancellatio

portalWithAllApps.AddApp(portalApp);

await readWriteContext.SaveChangesAsync(cancellationToken);
await _readWriteContext.SaveChangesAsync(cancellationToken);

return Unit.Value;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,54 @@

namespace Equinor.ProjectExecutionPortal.Application.Commands.Portals.CreatePortal;

public class CreatePortalCommand(
string name,
string shortName,
string subText,
string? description,
string icon,
IList<string> contextTypes)
: IRequest<Guid>
public class CreatePortalCommand : IRequest<Guid>
{
public string Name { get; set; } = name;
public string ShortName { get; set; } = shortName;
public string SubText { get; set; } = subText;
public string? Description { get; set; } = description;
public string Icon { get; set; } = icon;
public IList<string> ContextTypes { get; set; } = contextTypes;

public class Handler(IReadWriteContext readWriteContext, IContextTypeService contextTypeService) : IRequestHandler<CreatePortalCommand, Guid>
public CreatePortalCommand(string name,
string shortName,
string subText,
string? description,
string icon,
IList<string> contextTypes)
{
Name = name;
ShortName = shortName;
SubText = subText;
Description = description;
Icon = icon;
ContextTypes = contextTypes;
}

public string Name { get; set; }
public string ShortName { get; set; }
public string SubText { get; set; }
public string? Description { get; set; }
public string Icon { get; set; }
public IList<string> ContextTypes { get; set; }

public class Handler : IRequestHandler<CreatePortalCommand, Guid>
{
private readonly IReadWriteContext _readWriteContext;
private readonly IContextTypeService _contextTypeService;

public Handler(IReadWriteContext readWriteContext, IContextTypeService contextTypeService)
{
_readWriteContext = readWriteContext;
_contextTypeService = contextTypeService;
}

public async Task<Guid> Handle(CreatePortalCommand command, CancellationToken cancellationToken)
{
var slug = SlugHelper.Sluggify(command.Name);

var portal = new Portal(slug, command.Name, command.ShortName, command.SubText, command.Description, command.Icon);

portal.AddContextTypes(await contextTypeService.GetAllowedContextTypesByKeys(command.ContextTypes, cancellationToken));
portal.AddContextTypes(await _contextTypeService.GetAllowedContextTypesByKeys(command.ContextTypes, cancellationToken));

await readWriteContext.Set<Portal>().AddAsync(portal, cancellationToken);
await _readWriteContext.Set<Portal>().AddAsync(portal, cancellationToken);

await readWriteContext.SaveChangesAsync(cancellationToken);
await _readWriteContext.SaveChangesAsync(cancellationToken);

return portal.Id;
}
}
}
}
Loading

0 comments on commit f05001c

Please sign in to comment.