Skip to content

Commit

Permalink
Remove more scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
kjetilhau committed Dec 11, 2024
1 parent e181316 commit 7288edd
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Unit>
{
public class AddContextTypeToOnboardedAppCommand : IRequest<Unit>
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<AddContextTypeToOnboardedAppCommand, Unit>
{
private readonly IReadWriteContext _readWriteContext;

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

public class Handler : IRequestHandler<AddContextTypeToOnboardedAppCommand, Unit>
public async Task<Unit> Handle(AddContextTypeToOnboardedAppCommand command, CancellationToken cancellationToken)
{
private readonly IReadWriteContext _readWriteContext;
var onboardedAppWithAllContextTypes = await _readWriteContext.Set<OnboardedApp>()
.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<Unit> Handle(AddContextTypeToOnboardedAppCommand command, CancellationToken cancellationToken)
{
var onboardedAppWithAllContextTypes = await _readWriteContext.Set<OnboardedApp>()
.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<ContextType>()
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ContextTypeKey == command.Type, cancellationToken);
var contextType = await _readWriteContext.Set<ContextType>()
.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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Unit>
{
public class AddContextTypeToPortalCommand : IRequest<Unit>
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<AddContextTypeToPortalCommand, Unit>
{
private readonly IReadWriteContext _readWriteContext;

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

public class Handler : IRequestHandler<AddContextTypeToPortalCommand, Unit>
public async Task<Unit> Handle(AddContextTypeToPortalCommand command, CancellationToken cancellationToken)
{
private readonly IReadWriteContext _readWriteContext;
var portalWithAllContextTypes = await _readWriteContext.Set<Portal>()
.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<Unit> Handle(AddContextTypeToPortalCommand command, CancellationToken cancellationToken)
{
var portalWithAllContextTypes = await _readWriteContext.Set<Portal>()
.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<ContextType>()
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ContextTypeKey == command.Type, cancellationToken);
var contextType = await _readWriteContext.Set<ContextType>()
.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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<RemovePortalCommand>
{
public RemovePortalCommand(Guid id)
private readonly IReadWriteContext _context;

public Handler(IReadWriteContext context)
{
Id = id;
_context = context;
}

public Guid Id { get; }

public class Handler : IRequestHandler<RemovePortalCommand>
public async Task Handle(RemovePortalCommand command, CancellationToken cancellationToken)
{
private readonly IReadWriteContext _context;
var entity = await _context.Set<Portal>()
.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<Portal>()
.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<Portal>().Remove(entity);
_context.Set<Portal>().Remove(entity);

await _context.SaveChangesAsync(cancellationToken);
await _context.SaveChangesAsync(cancellationToken);

}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<OnboardedAppDto> EnrichWithFusionAppData(OnboardedAppDto onboardedApp, CancellationToken cancellationToken);
Task<OnboardedAppDto> EnrichWithFusionAppData(OnboardedAppDto onboardedApp, CancellationToken cancellationToken);

Task<List<OnboardedAppDto>> EnrichWithFusionAppData(List<OnboardedAppDto> apps, CancellationToken cancellationToken);
}
Task<List<OnboardedAppDto>> EnrichWithFusionAppData(List<OnboardedAppDto> apps, CancellationToken cancellationToken);
}
Loading

0 comments on commit 7288edd

Please sign in to comment.