Skip to content

Commit

Permalink
restore: unessery changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilbaczek committed May 24, 2024
1 parent 51f082b commit afe14bb
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ namespace EvolutionaryArchitecture.Fitnet.Contracts.Api.PrepareContract;

using EvolutionaryArchitecture.Fitnet.Common.Api.Validations;
using Application;
using Common.Errors;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

internal static class PrepareContractEndpoint
{
internal static void MapPrepareContract(this IEndpointRouteBuilder app) => app.MapPost(ContractsApiPaths.Prepare,
async (PrepareContractRequest request, IContractsModule contractsModule,
CancellationToken cancellationToken) =>
{
var command = request.ToCommand();
var contractId = await contractsModule.ExecuteCommandAsync(command, cancellationToken);

return Results.Created(ContractsApiPaths.GetPreparedContractPath(contractId), contractId);
})
internal static void MapPrepareContract(this IEndpointRouteBuilder app) =>
app.MapPost(ContractsApiPaths.Prepare,
async Task (PrepareContractRequest request, IContractsModule contractsModule, CancellationToken cancellationToken) =>
await contractsModule.ExecuteCommandAsync(request.ToCommand(), cancellationToken).Match(
contractId => Results.Created(ContractsApiPaths.GetPreparedContractPath(contractId), (object?)contractId),
errors => errors.ToProblem()))
.ValidateRequest<PrepareContractRequest>()
.WithOpenApi(operation => new(operation)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Application.AttachAnnexToBindingContract;

using Common.Api.ErrorHandling;

[UsedImplicitly]
internal sealed class AttachAnnexToBindingContractCommandHandler(
IBindingContractsRepository bindingContractsRepository,
TimeProvider timeProvider) : IRequestHandler<AttachAnnexToBindingContractCommand, Guid>
TimeProvider timeProvider) : IRequestHandler<AttachAnnexToBindingContractCommand, ErrorOr<Guid>>
{
public async Task<Guid> Handle(AttachAnnexToBindingContractCommand command, CancellationToken cancellationToken)
{
var bindingContract =
await bindingContractsRepository.GetByIdAsync(command.BindingContractId, cancellationToken) ??
throw new ResourceNotFoundException(command.BindingContractId);
var annexId = bindingContract.AttachAnnex(command.ValidFrom, timeProvider.GetUtcNow());
await bindingContractsRepository.CommitAsync(cancellationToken);
public async Task<ErrorOr<Guid>> Handle(AttachAnnexToBindingContractCommand command,
CancellationToken cancellationToken) =>
await bindingContractsRepository.GetByIdAsync(command.BindingContractId, cancellationToken)
.ThenAsync(bindingContract => bindingContract.AttachAnnex(command.ValidFrom, timeProvider.GetUtcNow())
.ThenAsync(async annexId =>
{
await bindingContractsRepository.CommitAsync(cancellationToken);

return annexId.Value;
}
return annexId.Value;
}));
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
global using System.Threading.Tasks;
global using ErrorOr;
global using ErrorOr;
global using JetBrains.Annotations;
global using MediatR;
global using MediatR;
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public interface IBindingContractsRepository
{
Task<BindingContract?> GetByIdAsync(Guid bindingContract, CancellationToken cancellationToken = default);
Task<ErrorOr<BindingContract>> GetByIdAsync(Guid bindingContract, CancellationToken cancellationToken = default);
Task AddAsync(BindingContract bindingContract, CancellationToken cancellationToken = default);
Task CommitAsync(CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Application.TerminateBindingContract;

using Common.Api.ErrorHandling;
using ErrorOr;

[UsedImplicitly]
internal sealed class TerminateBindingContractCommandHandler(
IBindingContractsRepository bindingContractsRepository,
TimeProvider timeProvider) : IRequestHandler<TerminateBindingContractCommand, ErrorOr<Unit>>
{
public async Task<ErrorOr<Unit>> Handle(TerminateBindingContractCommand command, CancellationToken cancellationToken)
{
var contract = await bindingContractsRepository.GetByIdAsync(command.BindingContractId, cancellationToken) ??
throw new ResourceNotFoundException(command.BindingContractId);
var terminatedAt = timeProvider.GetUtcNow();
contract.Terminate(terminatedAt);
await bindingContractsRepository.CommitAsync(cancellationToken);

return Unit.Value;
}
public async Task<ErrorOr<Unit>> Handle(TerminateBindingContractCommand command,
CancellationToken cancellationToken) =>
await bindingContractsRepository.GetByIdAsync(command.BindingContractId, cancellationToken)
.ThenAsync(bindingContract => bindingContract.Terminate(timeProvider.GetUtcNow())
.ThenAsync(async _ =>
{
await bindingContractsRepository.CommitAsync(cancellationToken);
return Unit.Value;
}));
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace EvolutionaryArchitecture.Fitnet.Contracts.Core;

using AttachAnnexToBindingContract.BusinessRules;
using Common.Core.BusinessRules;
using Common.BussinessRules;
using DomainDrivenDesign.BuildingBlocks;
using SignContract;
using TerminateBindingContract;
Expand Down Expand Up @@ -44,31 +44,30 @@ internal static BindingContract Start(
DateTimeOffset bindingFrom,
DateTimeOffset expiringAt) => new(id, customerId, duration, bindingFrom, expiringAt);

public AnnexId AttachAnnex(DateTimeOffset validFrom, DateTimeOffset now)
{
BusinessRuleValidator.Validate(
new AnnexCanOnlyBeAttachedToActiveBindingContractRule(TerminatedAt, ExpiringAt, now));

BusinessRuleValidator.Validate(
new AnnexCanOnlyStartDuringBindingContractPeriodRule(ExpiringAt, validFrom));
public ErrorOr<AnnexId> AttachAnnex(DateTimeOffset validFrom, DateTimeOffset now) => BusinessRuleValidator.Validate(
new AnnexCanOnlyBeAttachedToActiveBindingContractRule(TerminatedAt, ExpiringAt, now),
new AnnexCanOnlyStartDuringBindingContractPeriodRule(ExpiringAt, validFrom))
.Then(_ =>
{
var annex = Annex.Attach(Id, validFrom);

var annex = Annex.Attach(Id, validFrom);
AttachedAnnexes.Add(annex);

AttachedAnnexes.Add(annex);
return annex.Id;
});

return annex.Id;
}

public void Terminate(DateTimeOffset terminatedAt)
{
BusinessRuleValidator.Validate(
new TerminationIsPossibleOnlyAfterThreeMonthsHavePassedRule(BindingFrom, terminatedAt));
public ErrorOr<Success> Terminate(DateTimeOffset terminatedAt) => BusinessRuleValidator.Validate(
new TerminationIsPossibleOnlyAfterThreeMonthsHavePassedRule(BindingFrom, terminatedAt))
.Then(_ =>
{
TerminatedAt = terminatedAt;

TerminatedAt = terminatedAt;
var @event = BindingContractTerminatedEvent.Raise(terminatedAt);
RecordEvent(@event);

var @event = BindingContractTerminatedEvent.Raise(terminatedAt);
RecordEvent(@event);
}
return new Success();
});
}

public record struct ContractId(Guid Value)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
global using System;
global using System.Diagnostics.CodeAnalysis;
global using System.Diagnostics.CodeAnalysis;
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
global using System.Net;
global using System.Net.Http.Json;
global using Xunit;
global using Bogus;
global using FluentAssertions;
global using FluentAssertions;

0 comments on commit afe14bb

Please sign in to comment.