diff --git a/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/OnboardedApps/AddContextTypeToOnboardedApp/AddContextTypeToOnboardedAppCommand.cs b/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/OnboardedApps/AddContextTypeToOnboardedApp/AddContextTypeToOnboardedAppCommand.cs index d38fb637b..51eae041b 100644 --- a/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/OnboardedApps/AddContextTypeToOnboardedApp/AddContextTypeToOnboardedAppCommand.cs +++ b/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/OnboardedApps/AddContextTypeToOnboardedApp/AddContextTypeToOnboardedAppCommand.cs @@ -4,61 +4,60 @@ using MediatR; using Microsoft.EntityFrameworkCore; -namespace Equinor.ProjectExecutionPortal.Application.Commands.OnboardedApps.AddContextTypeToOnboardedApp +namespace Equinor.ProjectExecutionPortal.Application.Commands.OnboardedApps.AddContextTypeToOnboardedApp; + +public class AddContextTypeToOnboardedAppCommand : IRequest { - public class AddContextTypeToOnboardedAppCommand : IRequest + public AddContextTypeToOnboardedAppCommand(string appKey, string type) { - public AddContextTypeToOnboardedAppCommand(string appKey, string type) - { - AppKey = appKey; - Type = type; - } + AppKey = appKey; + Type = type; + } - public string AppKey { get; } - public string Type { get; } + public string AppKey { get; } + public string Type { get; } +} + +public class Handler : IRequestHandler +{ + private readonly IReadWriteContext _readWriteContext; + + public Handler(IReadWriteContext readWriteContext) + { + _readWriteContext = readWriteContext; } - public class Handler : IRequestHandler + public async Task Handle(AddContextTypeToOnboardedAppCommand command, CancellationToken cancellationToken) { - private readonly IReadWriteContext _readWriteContext; + var onboardedAppWithAllContextTypes = await _readWriteContext.Set() + .Include(x => x.ContextTypes) + .FirstOrDefaultAsync(x => x.AppKey == command.AppKey, cancellationToken); - public Handler(IReadWriteContext readWriteContext) + if (onboardedAppWithAllContextTypes == null) { - _readWriteContext = readWriteContext; + throw new NotFoundException(nameof(Portal), command.AppKey); } - public async Task Handle(AddContextTypeToOnboardedAppCommand command, CancellationToken cancellationToken) - { - var onboardedAppWithAllContextTypes = await _readWriteContext.Set() - .Include(x => x.ContextTypes) - .FirstOrDefaultAsync(x => x.AppKey == command.AppKey, cancellationToken); - - if (onboardedAppWithAllContextTypes == null) - { - throw new NotFoundException(nameof(Portal), command.AppKey); - } + var contextTypeExistsOnOnboardedApp = onboardedAppWithAllContextTypes.ContextTypes.Where(x => x.ContextTypeKey == command.Type); - var contextTypeExistsOnOnboardedApp = onboardedAppWithAllContextTypes.ContextTypes.Where(x => x.ContextTypeKey == command.Type); - - if (contextTypeExistsOnOnboardedApp.Any()) - { - throw new InvalidOperationException($"context-type {command.Type} is already enabled on onboarded app"); - } + if (contextTypeExistsOnOnboardedApp.Any()) + { + throw new InvalidOperationException($"context-type {command.Type} is already enabled on onboarded app"); + } - var contextType = await _readWriteContext.Set() - .AsNoTracking() - .FirstOrDefaultAsync(x => x.ContextTypeKey == command.Type, cancellationToken); + var contextType = await _readWriteContext.Set() + .AsNoTracking() + .FirstOrDefaultAsync(x => x.ContextTypeKey == command.Type, cancellationToken); - if (contextType == null) - { - throw new InvalidActionException($"context-type: {command.Type} is not supported"); - } + if (contextType == null) + { + throw new InvalidActionException($"context-type: {command.Type} is not supported"); + } - onboardedAppWithAllContextTypes.AddContextType(contextType); + onboardedAppWithAllContextTypes.AddContextType(contextType); - await _readWriteContext.SaveChangesAsync(cancellationToken); + await _readWriteContext.SaveChangesAsync(cancellationToken); - return Unit.Value; - } + return Unit.Value; } } diff --git a/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/Portals/AddContextTypeToPortal/AddContextTypeToPortalCommand.cs b/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/Portals/AddContextTypeToPortal/AddContextTypeToPortalCommand.cs index 1fbf3b8ef..6e09a7d01 100644 --- a/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/Portals/AddContextTypeToPortal/AddContextTypeToPortalCommand.cs +++ b/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/Portals/AddContextTypeToPortal/AddContextTypeToPortalCommand.cs @@ -4,61 +4,60 @@ using MediatR; using Microsoft.EntityFrameworkCore; -namespace Equinor.ProjectExecutionPortal.Application.Commands.Portals.AddContextTypeToPortal +namespace Equinor.ProjectExecutionPortal.Application.Commands.Portals.AddContextTypeToPortal; + +public class AddContextTypeToPortalCommand : IRequest { - public class AddContextTypeToPortalCommand : IRequest + public AddContextTypeToPortalCommand(Guid portalId, string type) { - public AddContextTypeToPortalCommand(Guid portalId, string type) - { - PortalId = portalId; - Type = type; - } + PortalId = portalId; + Type = type; + } - public Guid PortalId { get; } - public string Type { get; } + public Guid PortalId { get; } + public string Type { get; } +} + +public class Handler : IRequestHandler +{ + private readonly IReadWriteContext _readWriteContext; + + public Handler(IReadWriteContext readWriteContext) + { + _readWriteContext = readWriteContext; } - public class Handler : IRequestHandler + public async Task Handle(AddContextTypeToPortalCommand command, CancellationToken cancellationToken) { - private readonly IReadWriteContext _readWriteContext; + var portalWithAllContextTypes = await _readWriteContext.Set() + .Include(x => x.ContextTypes) + .FirstOrDefaultAsync(x => x.Id == command.PortalId, cancellationToken); - public Handler(IReadWriteContext readWriteContext) + if (portalWithAllContextTypes == null) { - _readWriteContext = readWriteContext; + throw new NotFoundException(nameof(Portal), command.PortalId); } - public async Task Handle(AddContextTypeToPortalCommand command, CancellationToken cancellationToken) - { - var portalWithAllContextTypes = await _readWriteContext.Set() - .Include(x => x.ContextTypes) - .FirstOrDefaultAsync(x => x.Id == command.PortalId, cancellationToken); - - if (portalWithAllContextTypes == null) - { - throw new NotFoundException(nameof(Portal), command.PortalId); - } + var contextTypeExistsOnPortal = portalWithAllContextTypes.ContextTypes.Where(x => x.ContextTypeKey == command.Type); - var contextTypeExistsOnPortal = portalWithAllContextTypes.ContextTypes.Where(x => x.ContextTypeKey == command.Type); - - if (contextTypeExistsOnPortal.Any()) - { - throw new InvalidActionException($"context-type {command.Type}is already enabled on portal"); - } + if (contextTypeExistsOnPortal.Any()) + { + throw new InvalidActionException($"context-type {command.Type}is already enabled on portal"); + } - var contextType = await _readWriteContext.Set() - .AsNoTracking() - .FirstOrDefaultAsync(x => x.ContextTypeKey == command.Type, cancellationToken); + var contextType = await _readWriteContext.Set() + .AsNoTracking() + .FirstOrDefaultAsync(x => x.ContextTypeKey == command.Type, cancellationToken); - if (contextType == null) - { - throw new InvalidOperationException($"context-type: {command.Type} is not supported"); - } + if (contextType == null) + { + throw new InvalidOperationException($"context-type: {command.Type} is not supported"); + } - portalWithAllContextTypes.AddContextType(contextType); + portalWithAllContextTypes.AddContextType(contextType); - await _readWriteContext.SaveChangesAsync(cancellationToken); + await _readWriteContext.SaveChangesAsync(cancellationToken); - return Unit.Value; - } + return Unit.Value; } } diff --git a/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/Portals/RemovePortal/RemovePortalCommand.cs b/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/Portals/RemovePortal/RemovePortalCommand.cs index a0c495b8b..d7bab4ebd 100644 --- a/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/Portals/RemovePortal/RemovePortalCommand.cs +++ b/backend/src/Equinor.ProjectExecutionPortal.Application/Commands/Portals/RemovePortal/RemovePortalCommand.cs @@ -4,47 +4,46 @@ using MediatR; using Microsoft.EntityFrameworkCore; -namespace Equinor.ProjectExecutionPortal.Application.Commands.Portals.RemovePortal +namespace Equinor.ProjectExecutionPortal.Application.Commands.Portals.RemovePortal; + +public class RemovePortalCommand : IRequest { - public class RemovePortalCommand : IRequest + public RemovePortalCommand(Guid id) + { + Id = id; + } + + public Guid Id { get; } + + public class Handler : IRequestHandler { - public RemovePortalCommand(Guid id) + private readonly IReadWriteContext _context; + + public Handler(IReadWriteContext context) { - Id = id; + _context = context; } - public Guid Id { get; } - - public class Handler : IRequestHandler + public async Task Handle(RemovePortalCommand command, CancellationToken cancellationToken) { - private readonly IReadWriteContext _context; + var entity = await _context.Set() + .Include(x => x.Apps) + .FirstOrDefaultAsync(portal => portal.Id == command.Id, cancellationToken); - public Handler(IReadWriteContext context) + if (entity == null) { - _context = context; + throw new NotFoundException(nameof(Portal), command.Id); } - public async Task Handle(RemovePortalCommand command, CancellationToken cancellationToken) + if (entity.Apps.Any()) { - var entity = await _context.Set() - .Include(x => x.Apps) - .FirstOrDefaultAsync(portal => portal.Id == command.Id, cancellationToken); - - if (entity == null) - { - throw new NotFoundException(nameof(Portal), command.Id); - } - - if (entity.Apps.Any()) - { - throw new InvalidOperationException("Cannot remove Portal, portal has onboarded apps"); - } + throw new InvalidOperationException("Cannot remove Portal, portal has onboarded apps"); + } - _context.Set().Remove(entity); + _context.Set().Remove(entity); - await _context.SaveChangesAsync(cancellationToken); + await _context.SaveChangesAsync(cancellationToken); - } } } } diff --git a/backend/src/Equinor.ProjectExecutionPortal.Application/Services/AppService/IAppService.cs b/backend/src/Equinor.ProjectExecutionPortal.Application/Services/AppService/IAppService.cs index 4b817b83d..200f1f6ae 100644 --- a/backend/src/Equinor.ProjectExecutionPortal.Application/Services/AppService/IAppService.cs +++ b/backend/src/Equinor.ProjectExecutionPortal.Application/Services/AppService/IAppService.cs @@ -1,11 +1,10 @@ using Equinor.ProjectExecutionPortal.Application.Queries.OnboardedApps; -namespace Equinor.ProjectExecutionPortal.Application.Services.AppService +namespace Equinor.ProjectExecutionPortal.Application.Services.AppService; + +public interface IAppService { - public interface IAppService - { - Task EnrichWithFusionAppData(OnboardedAppDto onboardedApp, CancellationToken cancellationToken); + Task EnrichWithFusionAppData(OnboardedAppDto onboardedApp, CancellationToken cancellationToken); - Task> EnrichWithFusionAppData(List apps, CancellationToken cancellationToken); - } + Task> EnrichWithFusionAppData(List apps, CancellationToken cancellationToken); } diff --git a/backend/src/Equinor.ProjectExecutionPortal.Application/Services/ContextService/ContextService.cs b/backend/src/Equinor.ProjectExecutionPortal.Application/Services/ContextService/ContextService.cs index 4c87e97fd..360c80e70 100644 --- a/backend/src/Equinor.ProjectExecutionPortal.Application/Services/ContextService/ContextService.cs +++ b/backend/src/Equinor.ProjectExecutionPortal.Application/Services/ContextService/ContextService.cs @@ -2,69 +2,68 @@ using Equinor.ProjectExecutionPortal.Domain.Entities; using Fusion.Integration; -namespace Equinor.ProjectExecutionPortal.Application.Services.ContextService +namespace Equinor.ProjectExecutionPortal.Application.Services.ContextService; + +public class ContextService : IContextService { - public class ContextService : IContextService - { - private readonly IFusionContextResolver _fusionContextResolver; + private readonly IFusionContextResolver _fusionContextResolver; - public ContextService(IFusionContextResolver contextResolver) + public ContextService(IFusionContextResolver contextResolver) + { + _fusionContextResolver = contextResolver; + } + + public async Task EnrichContextWithFusionContextData(OnboardedContextDto context, CancellationToken cancellationToken) + { + var contextIdentifier = ContextIdentifier.FromExternalId(context.ExternalId); + var fusionContext = await _fusionContextResolver.ResolveContextAsync(contextIdentifier); + + if (fusionContext != null) { - _fusionContextResolver = contextResolver; + context.SupplyWithFusionData(fusionContext); } - public async Task EnrichContextWithFusionContextData(OnboardedContextDto context, CancellationToken cancellationToken) + return context; + } + + public async Task> EnrichContextsWithFusionContextData(List contexts, CancellationToken cancellationToken) + { + foreach (var onboardedContextDto in contexts) { - var contextIdentifier = ContextIdentifier.FromExternalId(context.ExternalId); + var contextIdentifier = ContextIdentifier.FromExternalId(onboardedContextDto.ExternalId); var fusionContext = await _fusionContextResolver.ResolveContextAsync(contextIdentifier); if (fusionContext != null) { - context.SupplyWithFusionData(fusionContext); + onboardedContextDto.SupplyWithFusionData(fusionContext); } - - return context; } - public async Task> EnrichContextsWithFusionContextData(List contexts, CancellationToken cancellationToken) - { - foreach (var onboardedContextDto in contexts) - { - var contextIdentifier = ContextIdentifier.FromExternalId(onboardedContextDto.ExternalId); - var fusionContext = await _fusionContextResolver.ResolveContextAsync(contextIdentifier); - - if (fusionContext != null) - { - onboardedContextDto.SupplyWithFusionData(fusionContext); - } - } - - return contexts; - } + return contexts; + } - public async Task GetFusionContext(Guid contextId, CancellationToken cancellation) - { - var context = await _fusionContextResolver.GetContextAsync(contextId); + public async Task GetFusionContext(Guid contextId, CancellationToken cancellation) + { + var context = await _fusionContextResolver.GetContextAsync(contextId); - return context; - } + return context; + } - public async Task> GetFusionContextIds(List contexts, CancellationToken cancellationToken) + public async Task> GetFusionContextIds(List contexts, CancellationToken cancellationToken) + { + var contextIds = new List(); + + foreach (var context in contexts) { - var contextIds = new List(); + var contextIdentifier = ContextIdentifier.FromExternalId(context.ExternalId); + var fusionContext = await _fusionContextResolver.ResolveContextAsync(contextIdentifier); - foreach (var context in contexts) + if (fusionContext != null) { - var contextIdentifier = ContextIdentifier.FromExternalId(context.ExternalId); - var fusionContext = await _fusionContextResolver.ResolveContextAsync(contextIdentifier); - - if (fusionContext != null) - { - contextIds.Add(fusionContext.Id); - } + contextIds.Add(fusionContext.Id); } - - return contextIds; } + + return contextIds; } } diff --git a/backend/src/Equinor.ProjectExecutionPortal.Application/Services/ContextService/IContextService.cs b/backend/src/Equinor.ProjectExecutionPortal.Application/Services/ContextService/IContextService.cs index 08d0d67da..7c661287e 100644 --- a/backend/src/Equinor.ProjectExecutionPortal.Application/Services/ContextService/IContextService.cs +++ b/backend/src/Equinor.ProjectExecutionPortal.Application/Services/ContextService/IContextService.cs @@ -2,13 +2,12 @@ using Equinor.ProjectExecutionPortal.Domain.Entities; using Fusion.Integration; -namespace Equinor.ProjectExecutionPortal.Application.Services.ContextService +namespace Equinor.ProjectExecutionPortal.Application.Services.ContextService; + +public interface IContextService { - public interface IContextService - { - Task EnrichContextWithFusionContextData(OnboardedContextDto context, CancellationToken cancellationToken); - Task> EnrichContextsWithFusionContextData(List contexts, CancellationToken cancellationToken); - Task GetFusionContext(Guid contextId, CancellationToken cancellationToken); - Task> GetFusionContextIds(List contexts, CancellationToken cancellationToken); - } + Task EnrichContextWithFusionContextData(OnboardedContextDto context, CancellationToken cancellationToken); + Task> EnrichContextsWithFusionContextData(List contexts, CancellationToken cancellationToken); + Task GetFusionContext(Guid contextId, CancellationToken cancellationToken); + Task> GetFusionContextIds(List contexts, CancellationToken cancellationToken); } diff --git a/backend/src/Equinor.ProjectExecutionPortal.Domain/Common/Exceptions/ApplicationException.cs b/backend/src/Equinor.ProjectExecutionPortal.Domain/Common/Exceptions/ApplicationException.cs index 6fd770fad..046697fca 100644 --- a/backend/src/Equinor.ProjectExecutionPortal.Domain/Common/Exceptions/ApplicationException.cs +++ b/backend/src/Equinor.ProjectExecutionPortal.Domain/Common/Exceptions/ApplicationException.cs @@ -1,10 +1,9 @@ -namespace Equinor.ProjectExecutionPortal.Domain.Common.Exceptions +namespace Equinor.ProjectExecutionPortal.Domain.Common.Exceptions; + +public abstract class ApplicationException : Exception { - public abstract class ApplicationException : Exception - { - protected ApplicationException(string title, string? message, Exception? exception) : base(message, exception) => - Title = title; + protected ApplicationException(string title, string? message, Exception? exception) : base(message, exception) => + Title = title; - public string Title { get; } - } + public string Title { get; } } diff --git a/backend/src/Equinor.ProjectExecutionPortal.Infrastructure/EntityConfigurations/ContextTypeConfiguration.cs b/backend/src/Equinor.ProjectExecutionPortal.Infrastructure/EntityConfigurations/ContextTypeConfiguration.cs index a6fccef48..9830ad99f 100644 --- a/backend/src/Equinor.ProjectExecutionPortal.Infrastructure/EntityConfigurations/ContextTypeConfiguration.cs +++ b/backend/src/Equinor.ProjectExecutionPortal.Infrastructure/EntityConfigurations/ContextTypeConfiguration.cs @@ -3,22 +3,21 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; -namespace Equinor.ProjectExecutionPortal.Infrastructure.EntityConfigurations +namespace Equinor.ProjectExecutionPortal.Infrastructure.EntityConfigurations; + +public class ContextTypeConfiguration : IEntityTypeConfiguration { - public class ContextTypeConfiguration : IEntityTypeConfiguration + public void Configure(EntityTypeBuilder builder) { - public void Configure(EntityTypeBuilder builder) - { - builder.ConfigureCreationAudit(); - builder.ConfigureModificationAudit(); + builder.ConfigureCreationAudit(); + builder.ConfigureModificationAudit(); - builder - .HasIndex(t => t.ContextTypeKey) - .IsUnique(); + builder + .HasIndex(t => t.ContextTypeKey) + .IsUnique(); - builder.Property(t => t.ContextTypeKey) - .HasMaxLength(ContextType.ContextTypeKeyLengthMax) - .IsRequired(); - } + builder.Property(t => t.ContextTypeKey) + .HasMaxLength(ContextType.ContextTypeKeyLengthMax) + .IsRequired(); } }