Skip to content

Commit

Permalink
add context-types to put-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jossilainen committed Aug 14, 2024
1 parent b0280d3 commit 4b1b606
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Equinor.ProjectExecutionPortal.Application.Helpers;
using Equinor.ProjectExecutionPortal.Application.Services.ContextTypeService;
using Equinor.ProjectExecutionPortal.Domain.Common.Exceptions;
using Equinor.ProjectExecutionPortal.Domain.Entities;
using Equinor.ProjectExecutionPortal.Infrastructure;
Expand All @@ -9,7 +10,7 @@ namespace Equinor.ProjectExecutionPortal.Application.Commands.Portals.UpdatePort

public class UpdatePortalCommand : IRequest<Guid>
{
public UpdatePortalCommand(Guid id, string name, string shortName, string subText, string? description, int order, string icon)
public UpdatePortalCommand(Guid id, string name, string shortName, string subText, string? description, int order, string icon, IList<string>? contextTypes)
{
Id = id;
Name = name;
Expand All @@ -18,6 +19,7 @@ public UpdatePortalCommand(Guid id, string name, string shortName, string subTex
Description = description;
Order = order;
Icon = icon;
ContextTypes = contextTypes;
}

public Guid Id { get; }
Expand All @@ -27,19 +29,23 @@ public UpdatePortalCommand(Guid id, string name, string shortName, string subTex
public string? Description { get; set; }
public int Order { get; }
public string Icon { get; }
public IList<string>? ContextTypes { get; set; }

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

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

public async Task<Guid> Handle(UpdatePortalCommand command, CancellationToken cancellationToken)
{
var entity = await _readWriteContext.Set<Portal>()
.Include(x => x.ContextTypes)
.FirstOrDefaultAsync(x => x.Id == command.Id, cancellationToken);

if (entity == null)
Expand All @@ -51,6 +57,8 @@ public async Task<Guid> Handle(UpdatePortalCommand command, CancellationToken ca

entity.Update(slug, command.Name, command.ShortName, command.SubText, command.Description, command.Order, command.Icon);

entity.AddContextTypes(await _contextTypeService.GetContextTypesByContextTypeKey(command.ContextTypes, cancellationToken));

await _readWriteContext.SaveChangesAsync(cancellationToken);

return entity.Id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public class ApiUpdatePortalRequest
public string? Description { get; set; }
public int Order { get; set; }
public string Icon { get; set; } = null!;
public IList<string>? ContextTypes { get; set; }

public UpdatePortalCommand ToCommand(Guid id)
{
return new UpdatePortalCommand(id, Name, ShortName, Subtext, Description, Order, Icon);
return new UpdatePortalCommand(id, Name, ShortName, Subtext, Description, Order, Icon, ContextTypes);
}

public class UpdatePortalRequestValidator : AbstractValidator<ApiUpdatePortalRequest>
Expand Down

0 comments on commit 4b1b606

Please sign in to comment.