Skip to content

Commit

Permalink
title added to context
Browse files Browse the repository at this point in the history
  • Loading branch information
Jossilainen committed Oct 3, 2023
1 parent 568ca5d commit 23667f7
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<string> Handle(OnboardContextCommand command, CancellationToke
throw new InvalidActionException($"Context: '{command.ExternalId}' is already onboarded");
}

var onboardedContext = new OnboardedContext(command.ExternalId, command.Type, null, command.Description);
var onboardedContext = new OnboardedContext(command.ExternalId, command.Type, command.Description);

await _readWriteContext.Set<OnboardedContext>().AddAsync(onboardedContext, cancellationToken);
await _readWriteContext.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoMapper;
using Equinor.ProjectExecutionPortal.Application.Services.ContextService;
using Equinor.ProjectExecutionPortal.Domain.Infrastructure;
using Equinor.ProjectExecutionPortal.Infrastructure;
using MediatR;
Expand All @@ -19,20 +20,25 @@ public class Handler : IRequestHandler<GetOnboardedContextQuery, OnboardedContex
{
private readonly IReadWriteContext _readWriteContext;
private readonly IMapper _mapper;
private readonly IContextService _contextService;

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

public async Task<OnboardedContextDto?> Handle(GetOnboardedContextQuery request, CancellationToken cancellationToken)
{
var entity = await _readWriteContext.Set<Domain.Entities.OnboardedContext>()
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ExternalId == request.ExternalId, cancellationToken);
var onboardedContext = _mapper.Map<Domain.Entities.OnboardedContext?, OnboardedContextDto?>(entity);

await _contextService.EnrichContextWithFusionContextData(onboardedContext, cancellationToken);

return _mapper.Map<Domain.Entities.OnboardedContext?, OnboardedContextDto?>(entity);
return onboardedContext;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AutoMapper;
using Equinor.ProjectExecutionPortal.Application.Services.ContextService;
using Equinor.ProjectExecutionPortal.Domain.Infrastructure;
using Equinor.ProjectExecutionPortal.Infrastructure;
using MediatR;
Expand All @@ -12,20 +13,27 @@ public class Handler : IRequestHandler<GetOnboardedContextsQuery, IList<Onboarde
{
private readonly IReadWriteContext _context;
private readonly IMapper _mapper;
private readonly IContextService _contextService;

public Handler(IReadWriteContext context, IMapper mapper)
public Handler(IReadWriteContext context, IMapper mapper, IContextService contextService)
{
_context = context;
_mapper = mapper;
_contextService = contextService;
}

public async Task<IList<OnboardedContextDto>> Handle(GetOnboardedContextsQuery request, CancellationToken cancellationToken)
{
var enitity = await _context.Set<Domain.Entities.OnboardedContext>()
var entity = await _context.Set<Domain.Entities.OnboardedContext>()
.AsNoTracking()
.ToListAsync(cancellationToken);
.ToListAsync(cancellationToken);

var onboardedContext = _mapper.Map<List<Domain.Entities.OnboardedContext>, List<OnboardedContextDto>>(entity);

await _contextService.EnrichContextsWithFusionContextData(onboardedContext, cancellationToken);

return onboardedContext;

return _mapper.Map<List<Domain.Entities.OnboardedContext>, List<OnboardedContextDto>>(enitity);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Equinor.ProjectExecutionPortal.Application.Infrastructure.Mappings;
using Equinor.ProjectExecutionPortal.Domain.Entities;
using Fusion.Integration;

namespace Equinor.ProjectExecutionPortal.Application.Queries.OnboardedContexts;

Expand All @@ -8,5 +9,11 @@ public class OnboardedContextDto : IMapFrom<OnboardedContext>
public Guid Id { get; set; }
public string ExternalId { get; set; }
public string Type { get; set; }
public string Title { get; set; }
public string? Description { get; set; }

public void SupplyWithFusionData(FusionContext context)
{
Title = context.Title;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Equinor.ProjectExecutionPortal.Application.Queries.OnboardedContexts;
using Fusion.Integration;

namespace Equinor.ProjectExecutionPortal.Application.Services.ContextService
{
public class ContextService : IContextService
{
private readonly IFusionContextResolver _fusionContextResolver;


public ContextService(IFusionContextResolver contextResolver)
{
_fusionContextResolver = contextResolver;
}

public async Task<OnboardedContextDto> EnrichContextWithFusionContextData(OnboardedContextDto context, CancellationToken cancellationToken)
{
var contextIdentifier = ContextIdentifier.FromExternalId(context.ExternalId);
var fusionContext = await _fusionContextResolver.ResolveContextAsync(contextIdentifier);

if (fusionContext != null)
{
context.SupplyWithFusionData(fusionContext);
}

return context;
}

public async Task<IList<OnboardedContextDto>> EnrichContextsWithFusionContextData(IList<OnboardedContextDto> 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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Equinor.ProjectExecutionPortal.Application.Queries.OnboardedContexts;

namespace Equinor.ProjectExecutionPortal.Application.Services.ContextService
{
public interface IContextService
{
Task<OnboardedContextDto> EnrichContextWithFusionContextData(OnboardedContextDto context, CancellationToken cancellationToken);
Task<IList<OnboardedContextDto>> EnrichContextsWithFusionContextData(IList <OnboardedContextDto> contexts, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ public class OnboardedContext : AuditableEntityBase, ICreationAuditable, IModifi
{
public const int ExternalIdLengthMax = 200;
public const int TypeLengthMax = 200;
public const int TitleLengthMax = 400;
public const int DescriptionLengthMax = 4000;

private readonly List<WorkSurfaceApp> _apps = new();

public OnboardedContext(string externalId, string type, string? title, string? description)
public OnboardedContext(string externalId, string type, string? description)
{
ExternalId = externalId;
Type = type;
Title = title;
// Title = title;
Description = description;
}

public string ExternalId { get; }
public string Type { get; } // TODO: Necessary? Type can alternatively be resolved by Fusion Context
public string? Title { get; set; } // TODO: Could this change? Should perhaps be fetched from Fusion Context resolver.
public string? Description { get; set; }

public IReadOnlyCollection<WorkSurfaceApp> Apps => _apps.AsReadOnly();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public void Configure(EntityTypeBuilder<OnboardedContext> builder)
builder.Property(x => x.Type)
.HasMaxLength(OnboardedContext.TypeLengthMax);

builder.Property(t => t.Title)
.HasMaxLength(OnboardedContext.TitleLengthMax);

builder.Property(t => t.Description)
.HasMaxLength(OnboardedContext.DescriptionLengthMax);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Equinor.ProjectExecutionPortal.WebApi.Controllers
public class OnboardedContextController : ApiControllerBase
{
[HttpGet("")]
public async Task<ActionResult<IList<ApiOnboardedContext>>> OboardedContexts()
public async Task<ActionResult<IList<ApiOnboardedContext>>> OnboardedContexts()
{
var onboardedContextsDto = await Mediator.Send(new GetOnboardedContextsQuery());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Equinor.ProjectExecutionPortal.Application.Services.AppService;
using Equinor.ProjectExecutionPortal.Application.Services.ContextService;
using Equinor.ProjectExecutionPortal.Application.Services.WorkSurfaceService;

namespace Equinor.ProjectExecutionPortal.WebApi.DiModules;
Expand All @@ -9,5 +10,6 @@ public static void AddApplicationServicesModules(this IServiceCollection service
{
services.AddScoped<IAppService, AppService>();
services.AddScoped<IWorkSurfaceService, WorkSurfaceService>();
services.AddScoped<IContextService, ContextService>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public ApiOnboardedContext(OnboardedContextDto onboardedAppDto)
ExternalId = onboardedAppDto.ExternalId;
Type = onboardedAppDto.Type;
Description = onboardedAppDto.Description;
Title = onboardedAppDto.Title;
}

public Guid Id { get; set; }
public string ExternalId { get; set; } = null!;
public string Type { get; set; } = null!;
public string Title { get; set; }
public string? Description { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public OnboardContextRequestValidator()
.NotEmpty()
.NotContainScriptTag()
.WithMessage("External Id is required");

RuleFor(x => x.Description)
.MaximumLength(Domain.Entities.OnboardedContext.TitleLengthMax)
.NotContainScriptTag();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ internal class OnboardedContextsData
{
public class InitialSeedData
{
public static OnboardedContext JcaContext = new(FusionContextData.InitialSeedData.JcaContextExternalId, FusionContextData.InitialSeedData.ContextType, "title", "desc");
public static OnboardedContext OgpContext = new(FusionContextData.InitialSeedData.OgpContextExternalId, FusionContextData.InitialSeedData.ContextType, "title", "desc");
public static OnboardedContext JcaContext = new(FusionContextData.InitialSeedData.JcaContextExternalId, FusionContextData.InitialSeedData.ContextType, "desc");
public static OnboardedContext OgpContext = new(FusionContextData.InitialSeedData.OgpContextExternalId, FusionContextData.InitialSeedData.ContextType, "desc");
}
}
}

0 comments on commit 23667f7

Please sign in to comment.