From 7088c766badc81d8cce334b5da8faee6aa500200 Mon Sep 17 00:00:00 2001 From: Maciej Jedrzejewski Date: Tue, 2 Apr 2024 09:11:09 +0200 Subject: [PATCH] refactor: use time provider in contracts --- .../Sign/SignContractCommandHandler.cs | 8 ++++---- .../ContractSignedEvent.cs | 17 ++++++++++++++--- .../Src/Fitnet.Contracts/Program.cs | 2 -- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.Application/Sign/SignContractCommandHandler.cs b/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.Application/Sign/SignContractCommandHandler.cs index 853b4ff6..bb682028 100644 --- a/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.Application/Sign/SignContractCommandHandler.cs +++ b/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.Application/Sign/SignContractCommandHandler.cs @@ -1,7 +1,6 @@ namespace EvolutionaryArchitecture.Fitnet.Contracts.Application.Sign; using Common.Api.ErrorHandling; -using Common.Core.SystemClock; using Core; using IntegrationEvents; using MassTransit; @@ -9,19 +8,20 @@ namespace EvolutionaryArchitecture.Fitnet.Contracts.Application.Sign; [UsedImplicitly] internal sealed class SignContractCommandHandler( IContractsRepository contractsRepository, - ISystemClock systemClock, + TimeProvider timeProvider, IPublishEndpoint publishEndpoint) : IRequestHandler { public async Task Handle(SignContractCommand command, CancellationToken cancellationToken) { var contract = await contractsRepository.GetByIdAsync(command.Id, cancellationToken) ?? throw new ResourceNotFoundException(command.Id); - contract.Sign(command.SignedAt, systemClock.Now); + contract.Sign(command.SignedAt, timeProvider.GetUtcNow()); await contractsRepository.CommitAsync(cancellationToken); var @event = ContractSignedEvent.Create(contract.Id, contract.CustomerId, contract.SignedAt!.Value, - contract.ExpiringAt!.Value); + contract.ExpiringAt!.Value, + timeProvider.GetUtcNow()); await publishEndpoint.Publish(@event, cancellationToken); } } diff --git a/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.IntegrationEvents/ContractSignedEvent.cs b/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.IntegrationEvents/ContractSignedEvent.cs index 77c9cb7e..2158eb39 100644 --- a/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.IntegrationEvents/ContractSignedEvent.cs +++ b/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts.IntegrationEvents/ContractSignedEvent.cs @@ -1,7 +1,18 @@ namespace EvolutionaryArchitecture.Fitnet.Contracts.IntegrationEvents; -public sealed record ContractSignedEvent(Guid Id, Guid ContractId, Guid ContractCustomerId, DateTimeOffset SignedAt, DateTimeOffset ExpireAt, DateTimeOffset OccurredDateTime) +public sealed record ContractSignedEvent( + Guid Id, + Guid ContractId, + Guid ContractCustomerId, + DateTimeOffset SignedAt, + DateTimeOffset ExpireAt, + DateTimeOffset OccurredDateTime) { - public static ContractSignedEvent Create(Guid contractId, Guid contractCustomerId, DateTimeOffset signedAt, DateTimeOffset expireAt) => - new(Guid.NewGuid(), contractId, contractCustomerId, signedAt, expireAt, DateTimeOffset.UtcNow); + public static ContractSignedEvent Create( + Guid contractId, + Guid contractCustomerId, + DateTimeOffset signedAt, + DateTimeOffset expireAt, + DateTimeOffset occurredAt) => + new(Guid.NewGuid(), contractId, contractCustomerId, signedAt, expireAt, occurredAt); } diff --git a/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts/Program.cs b/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts/Program.cs index 793f572f..0f8ca9a5 100644 --- a/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts/Program.cs +++ b/Chapter-3-microservice-extraction/Fitnet.Contracts/Src/Fitnet.Contracts/Program.cs @@ -1,5 +1,4 @@ using EvolutionaryArchitecture.Fitnet.Common.Api.ErrorHandling; -using EvolutionaryArchitecture.Fitnet.Common.Core.SystemClock; using EvolutionaryArchitecture.Fitnet.Contracts.Api; var builder = WebApplication.CreateBuilder(args); @@ -8,7 +7,6 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); -builder.Services.AddSystemClock(); builder.Services.AddFeatureManagement(); builder.Services.AddContractsApi(builder.Configuration);