Skip to content

Commit

Permalink
serverExecutionBase
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Mar 3, 2024
1 parent 0bbb374 commit 4e7664f
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Modules.Subscriptions.IntegrationEvents;
using Shared.Features.CQRS.Command;
using Shared.Features.CQRS.IntegrationEvent;
using Shared.Features.Server;
using Stripe;

namespace Modules.Subscriptions.Features.DomainFeatures.StripeSubscriptionAggregate.Application.Commands
Expand All @@ -17,7 +18,7 @@ public class CreateTrialingSubscriptionForTenant : ICommand
public Stripe.Subscription CreatedStripeSubscription { get; set; }
}

public class CreateTrialingSubscriptionCommandHandler : ICommandHandler<CreateTrialingSubscriptionForTenant>
public class CreateTrialingSubscriptionCommandHandler : ServerExecutionBase, ICommandHandler<CreateTrialingSubscriptionForTenant>
{
private readonly SubscriptionsDbContext subscriptionDbContext;
private readonly SubscriptionsConfiguration subscriptionConfiguration;
Expand All @@ -26,11 +27,10 @@ public class CreateTrialingSubscriptionCommandHandler : ICommandHandler<CreateTr
public CreateTrialingSubscriptionCommandHandler(
SubscriptionsDbContext subscriptionDbContext,
SubscriptionsConfiguration subscriptionConfiguration,
IIntegrationEventDispatcher integrationEventDispatcher)
IServiceProvider serviceProvider): base(serviceProvider)
{
this.subscriptionDbContext = subscriptionDbContext;
this.subscriptionConfiguration = subscriptionConfiguration;
this.integrationEventDispatcher = integrationEventDispatcher;
}

public async Task HandleAsync(CreateTrialingSubscriptionForTenant command, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public async Task<ActionResult> RedirectToStripePremiumSubscription([FromRoute]
var createStripeCheckoutSession = new CreateStripeCheckoutSession
{
SubscriptionPlanType = subscriptionPlanType,
UserId = ExecutionContext.UserId,
TenantId = ExecutionContext.TenantId,
RedirectBaseUrl = ExecutionContext.BaseURI.AbsoluteUri
UserId = executionContext.UserId,
TenantId = executionContext.TenantId,
RedirectBaseUrl = executionContext.BaseURI.AbsoluteUri
};
var checkoutSession = await CommandDispatcher.DispatchAsync<CreateStripeCheckoutSession, Stripe.Checkout.Session>(createStripeCheckoutSession);
var checkoutSession = await commandDispatcher.DispatchAsync<CreateStripeCheckoutSession, Stripe.Checkout.Session>(createStripeCheckoutSession);

Response.Headers.Add("Location", checkoutSession.Url);
return new StatusCodeResult(303);
Expand All @@ -38,10 +38,10 @@ public async Task<ActionResult> Create()
{
var createBillingPortalSession = new CreateStripeBillingPortalSession
{
UserId = ExecutionContext.UserId,
RedirectBaseUrl = ExecutionContext.BaseURI.AbsoluteUri,
UserId = executionContext.UserId,
RedirectBaseUrl = executionContext.BaseURI.AbsoluteUri,
};
var billingPortalSession = await CommandDispatcher.DispatchAsync<CreateStripeBillingPortalSession, Stripe.BillingPortal.Session>(createBillingPortalSession);
var billingPortalSession = await commandDispatcher.DispatchAsync<CreateStripeBillingPortalSession, Stripe.BillingPortal.Session>(createBillingPortalSession);

Response.Headers.Add("Location", billingPortalSession.Url);
return new StatusCodeResult(303);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class RemoveUserFromTenant : ICommand
public class RemoveUserFromTenantommandHandler : ICommandHandler<RemoveUserFromTenant>
{
private readonly TenantIdentityDbContext tenantIdentityDbContext;
private readonly IExecutionContext executionContext;

public RemoveUserFromTenantommandHandler(TenantIdentityDbContext tenantIdentityDbContext)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Modules.TenantIdentity.Features.Infrastructure.EFCore;
using Shared.Features.CQRS.Query;
using Shared.Features.Server;
using Shared.Kernel.BuildingBlocks.Auth.Constants;
using System.Security.Claims;
using System.Threading;
Expand All @@ -11,14 +12,12 @@ public class GetClaimsForUser : IQuery<IEnumerable<Claim>>
public Guid UserId { get; set; }
}

public class ClaimsForUserQueryHandler : IQueryHandler<GetClaimsForUser, IEnumerable<Claim>>
public class ClaimsForUserQueryHandler : ServerExecutionBase, IQueryHandler<GetClaimsForUser, IEnumerable<Claim>>
{
private readonly IQueryDispatcher queryDispatcher;
private readonly TenantIdentityDbContext tenantIdentityDbContext;

public ClaimsForUserQueryHandler(IQueryDispatcher queryDispatcher, TenantIdentityDbContext tenantIdentityDbContext)
public ClaimsForUserQueryHandler(TenantIdentityDbContext tenantIdentityDbContext, IServiceProvider serviceProvider) : base(serviceProvider)
{
this.queryDispatcher = queryDispatcher;
this.tenantIdentityDbContext = tenantIdentityDbContext;
}
public async Task<IEnumerable<Claim>> HandleAsync(GetClaimsForUser query, CancellationToken cancellation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public TenantsController(SignInManager<ApplicationUser> signInManager, IServiceP
[AuthorizeTenantAdmin]
public async Task<ActionResult<TenantDTO>> GetTenant()
{
var tenantId = ExecutionContext.TenantId;
TenantDTO tenant = await QueryDispatcher.DispatchAsync<GetTenantByID, TenantDTO>(new GetTenantByID { TenantId = tenantId });
var tenantId = executionContext.TenantId;
TenantDTO tenant = await queryDispatcher.DispatchAsync<GetTenantByID, TenantDTO>(new GetTenantByID { TenantId = tenantId });

return Ok(tenant);
}

[HttpGet("{tenantId}/details")]
public async Task<ActionResult<TenantDetailDTO>> GetTenantDetail()
{
var tenantId = ExecutionContext.TenantId;
TenantDetailDTO tenantDetail = await QueryDispatcher.DispatchAsync<GetTenantDetailsByID, TenantDetailDTO>(new GetTenantDetailsByID { TenantId = tenantId });
var tenantId = executionContext.TenantId;
TenantDetailDTO tenantDetail = await queryDispatcher.DispatchAsync<GetTenantDetailsByID, TenantDetailDTO>(new GetTenantDetailsByID { TenantId = tenantId });

return Ok(tenantDetail);
}
Expand All @@ -50,25 +50,25 @@ public async Task<ActionResult<TenantDetailDTO>> GetTenantDetail()
[Authorize(AuthenticationSchemes = AuthConstant.ApplicationAuthenticationScheme)]
public async Task<ActionResult<IEnumerable<TenantDTO>>> GetAllTenantsWhereUserIsMember()
{
var userId = ExecutionContext.UserId;
List<TenantMembershipDTO> teamMemberships = await QueryDispatcher.DispatchAsync<GetAllTenantMembershipsOfUser, List<TenantMembershipDTO>>(null);
var userId = executionContext.UserId;
List<TenantMembershipDTO> teamMemberships = await queryDispatcher.DispatchAsync<GetAllTenantMembershipsOfUser, List<TenantMembershipDTO>>(null);

return Ok(teamMemberships);
}

[HttpPost]
public async Task<ActionResult<TenantDTO>> CreateTenant(CreateTenantDTO createTenantDTO)
{
ValidationService.ThrowIfInvalidModel(createTenantDTO);
validationService.ThrowIfInvalidModel(createTenantDTO);

var createTenant = new CreateTenantWithAdmin
{
AdminId = ExecutionContext.UserId,
AdminId = executionContext.UserId,
Name = createTenantDTO.Name
};
var createdTenant = await CommandDispatcher.DispatchAsync<CreateTenantWithAdmin, TenantDTO>(null);
var createdTenant = await commandDispatcher.DispatchAsync<CreateTenantWithAdmin, TenantDTO>(null);

var user = await QueryDispatcher.DispatchAsync<GetUserById, ApplicationUser>(new GetUserById { });
var user = await queryDispatcher.DispatchAsync<GetUserById, ApplicationUser>(new GetUserById { });
await signInManager.RefreshSignInAsync(user);

return CreatedAtAction(nameof(CreateTenant), createdTenant);
Expand All @@ -77,36 +77,36 @@ public async Task<ActionResult<TenantDTO>> CreateTenant(CreateTenantDTO createTe
[HttpDelete("{id}")]
public async Task DeleteTenant(Guid id)
{
await CommandDispatcher.DispatchAsync<DeleteTenant>(new DeleteTenant { });
await commandDispatcher.DispatchAsync<DeleteTenant>(new DeleteTenant { });

var userId = ExecutionContext.UserId;
var user = await QueryDispatcher.DispatchAsync<GetUserById, ApplicationUser>(new GetUserById { });
var userId = executionContext.UserId;
var user = await queryDispatcher.DispatchAsync<GetUserById, ApplicationUser>(new GetUserById { });

await signInManager.RefreshSignInAsync(user);
}

[HttpPost("memberships")]
public async Task<ActionResult> CreateTenantMembership(InviteUserToTenantDTO inviteUserToGroupDTO)
{
var userId = ExecutionContext.UserId;
var userId = executionContext.UserId;

await CommandDispatcher.DispatchAsync<AddUserToTenant>(null);
await commandDispatcher.DispatchAsync<AddUserToTenant>(null);

return Ok();
}

[HttpPut("memberships")]
public async Task<ActionResult> UpdateTenantMembership(ChangeRoleOfTenantMemberDTO changeRoleOfTeamMemberDTO)
{
await CommandDispatcher.DispatchAsync<UpdateTenantMembership>(new UpdateTenantMembership { });
await commandDispatcher.DispatchAsync<UpdateTenantMembership>(new UpdateTenantMembership { });

return Ok();
}

[HttpDelete("memberships/{userId}")]
public async Task<ActionResult> DeleteTenantMembership(Guid id)
{
await CommandDispatcher.DispatchAsync<RemoveUserFromTenant>(new RemoveUserFromTenant { });
await commandDispatcher.DispatchAsync<RemoveUserFromTenant>(new RemoveUserFromTenant { });

return Ok();
}
Expand Down
30 changes: 0 additions & 30 deletions Source/Shared/Features/CQRS/Command/BaseCommandHandler.cs

This file was deleted.

8 changes: 3 additions & 5 deletions Source/Shared/Features/CQRS/Command/ICommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Shared.Features.Server;

namespace Shared.Features.CQRS.Command
namespace Shared.Features.CQRS.Command
{
public interface ICommandHandler<in TCommand> : ServerExecutionBase where TCommand : ICommand
public interface ICommandHandler<in TCommand> where TCommand : ICommand
{
Task HandleAsync(TCommand command, CancellationToken cancellationToken);
}
public interface ICommandHandler<in TCommand, TResult> : ServerExecutionBase where TCommand : ICommand<TResult>
public interface ICommandHandler<in TCommand, TResult> where TCommand : ICommand<TResult>
{
Task<TResult> HandleAsync(TCommand command, CancellationToken cancellationToken);
}
Expand Down
30 changes: 0 additions & 30 deletions Source/Shared/Features/CQRS/DomainEvent/BaseDomainEventHandler.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Shared.Features.Domain;
using Shared.Features.Server;

namespace Shared.Features.CQRS.DomainEvent
{
public interface IDomainEventHandler<in TDomainEvent> : ServerExecutionBase where TDomainEvent : IDomainEvent
public interface IDomainEventHandler<in TDomainEvent> where TDomainEvent : IDomainEvent
{
Task HandleAsync(TDomainEvent query, CancellationToken cancellation);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Shared.Features.Server;
using Shared.Kernel.BuildingBlocks;
using Shared.Kernel.BuildingBlocks;

namespace Shared.Features.CQRS.IntegrationEvent
{
public interface IIntegrationEventHandler<in TIntegrationEvent> : ServerExecutionBase where TIntegrationEvent : IIntegrationEvent
public interface IIntegrationEventHandler<in TIntegrationEvent> where TIntegrationEvent : IIntegrationEvent
{
Task HandleAsync(TIntegrationEvent integrationEvent, CancellationToken cancellation);
}
Expand Down
17 changes: 0 additions & 17 deletions Source/Shared/Features/CQRS/Query/BaseQueryHandler.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Source/Shared/Features/CQRS/Query/IQueryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Shared.Features.CQRS.Query
{
public interface IQueryHandler<in TQuery, TQueryResult> : ServerExecutionBase where TQuery : IQuery<TQueryResult>
public interface IQueryHandler<in TQuery, TQueryResult> where TQuery : IQuery<TQueryResult>
{
Task<TQueryResult> HandleAsync(TQuery query, CancellationToken cancellation);
}
Expand Down
26 changes: 13 additions & 13 deletions Source/Shared/Features/Server/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@

namespace Shared.Features.Server
{
public class BaseController : ControllerBase, ServerExecutionBase
public class BaseController : ControllerBase
{
public IExecutionContext ExecutionContext { get; }
public ICommandDispatcher CommandDispatcher { get; }
public IQueryDispatcher QueryDispatcher { get; }
public IIntegrationEventDispatcher IntegrationEventDispatcher { get; }
public IDomainEventDispatcher DomainEventDispatcher { get; }
public IValidationService ValidationService { get; }
protected readonly IExecutionContext executionContext;
protected readonly ICommandDispatcher commandDispatcher;
protected readonly IQueryDispatcher queryDispatcher;
protected readonly IIntegrationEventDispatcher integrationEventDispatcher;
protected readonly IDomainEventDispatcher domainEventDispatcher;
protected readonly IValidationService validationService;

public BaseController(IServiceProvider serviceProvider)
{
ExecutionContext = serviceProvider.GetRequiredService<IExecutionContext>();
CommandDispatcher = serviceProvider.GetRequiredService<ICommandDispatcher>();
QueryDispatcher = serviceProvider.GetRequiredService<IQueryDispatcher>();
IntegrationEventDispatcher = serviceProvider.GetRequiredService<IIntegrationEventDispatcher>();
DomainEventDispatcher = serviceProvider.GetRequiredService<IDomainEventDispatcher>();
ValidationService = serviceProvider.GetRequiredService<IValidationService>();
executionContext = serviceProvider.GetRequiredService<IExecutionContext>();
commandDispatcher = serviceProvider.GetRequiredService<ICommandDispatcher>();
queryDispatcher = serviceProvider.GetRequiredService<IQueryDispatcher>();
integrationEventDispatcher = serviceProvider.GetRequiredService<IIntegrationEventDispatcher>();
domainEventDispatcher = serviceProvider.GetRequiredService<IDomainEventDispatcher>();
validationService = serviceProvider.GetRequiredService<IValidationService>();
}
}
}
Loading

0 comments on commit 4e7664f

Please sign in to comment.