Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Shared.EventStore.Aggregate;
using Shouldly;
using SimpleResults;
using TransactionProcessor.BusinessLogic.Common;
using TransactionProcessor.BusinessLogic.Services;
using Xunit;

Expand Down
46 changes: 46 additions & 0 deletions TransactionProcessor.BusinessLogic/Common/DomainServiceHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Shared.EventStore.Aggregate;
using Shared.Results;
using SimpleResults;

namespace TransactionProcessor.BusinessLogic.Common;

public static class DomainServiceHelper
{
public static async Task<Result<TAggregate>> GetAggregateOrFailure<TAggregate>(Func<CancellationToken, Task<Result<TAggregate>>> fetchFunc,
Guid aggregateId,
CancellationToken cancellationToken,
Boolean isNotFoundError = true) where TAggregate : Aggregate, new()
{
Result<TAggregate> result = await fetchFunc(cancellationToken);
return result.IsFailed switch

Check notice on line 18 in TransactionProcessor.BusinessLogic/Common/DomainServiceHelper.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TransactionProcessor.BusinessLogic/Common/DomainServiceHelper.cs#L18

Replace this 'switch' expression with a ternary conditional operator to increase readability.
{
true => DomainServiceHelper.HandleGetAggregateResult(result, aggregateId, isNotFoundError),
_ => Result.Success(result.Data)
};
}

public static Result<T> HandleGetAggregateResult<T>(Result<T> result, Guid aggregateId, bool isNotFoundError = true)
where T : Aggregate, new() // Constraint: T is a subclass of Aggregate and has a parameterless constructor
{
if (result.IsFailed && result.Status != ResultStatus.NotFound)
{
return ResultHelpers.CreateFailure(result);
}

if (result.Status == ResultStatus.NotFound && isNotFoundError)
{
return ResultHelpers.CreateFailure(result);
}

T aggregate = result.Status switch

Check notice on line 38 in TransactionProcessor.BusinessLogic/Common/DomainServiceHelper.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

TransactionProcessor.BusinessLogic/Common/DomainServiceHelper.cs#L38

Replace this 'switch' expression with a ternary conditional operator to increase readability.
{
ResultStatus.NotFound => new T { AggregateId = aggregateId }, // Set AggregateId when creating a new instance
_ => result.Data
};

return Result.Success(aggregate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Shared.Results;
using SimpleResults;
using TransactionProcessor.Aggregates;
using TransactionProcessor.BusinessLogic.Common;
using TransactionProcessor.BusinessLogic.Requests;
using TransactionProcessor.Models.Contract;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Shared.Results;
using SimpleResults;
using TransactionProcessor.Aggregates;
using TransactionProcessor.BusinessLogic.Common;
using TransactionProcessor.BusinessLogic.Requests;

namespace TransactionProcessor.BusinessLogic.Services
Expand Down
232 changes: 97 additions & 135 deletions TransactionProcessor.BusinessLogic/Services/FloatDomainService.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Threading.Tasks;
using SecurityService.DataTransferObjects.Responses;
using TransactionProcessor.Aggregates;
using TransactionProcessor.BusinessLogic.Common;
using TransactionProcessor.BusinessLogic.Requests;
using TransactionProcessor.Database.Entities;
using TransactionProcessor.Models.Estate;
Expand Down Expand Up @@ -71,21 +72,9 @@ public MerchantDomainService(Func<IAggregateService> aggregateService,

#region Methods

//private async Task<Result<TAggregate>> GetAggregateOrFailure<TAggregate>(Func<CancellationToken, Task<Result<TAggregate>>> fetchFunc,
// Guid aggregateId,
// CancellationToken cancellationToken,
// Boolean isNotFoundError = true) where TAggregate : Aggregate, new()
//{
// Result<TAggregate> result = await fetchFunc(cancellationToken);
// return result.IsFailed switch {
// true => DomainServiceHelper.HandleGetAggregateResult(result, aggregateId, isNotFoundError),
// _ => Result.Success(result.Data)
// };
//}

public async Task<Result> AddDeviceToMerchant(MerchantCommands.AddMerchantDeviceCommand command, CancellationToken cancellationToken) {
try {
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down Expand Up @@ -294,7 +283,7 @@ public async Task<Result> MakeMerchantDeposit(MerchantCommands.MakeMerchantDepos
{
try
{
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand All @@ -310,7 +299,7 @@ public async Task<Result> MakeMerchantDeposit(MerchantCommands.MakeMerchantDepos
if (validateResult.IsFailed)
return ResultHelpers.CreateFailure(validateResult);

Result<MerchantDepositListAggregate> getDepositListResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest<MerchantDepositListAggregate>(command.MerchantId, ct), command.MerchantId, cancellationToken, false).ConfigureAwait(false);
Result<MerchantDepositListAggregate> getDepositListResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest<MerchantDepositListAggregate>(command.MerchantId, ct), command.MerchantId, cancellationToken, false);
if (getDepositListResult.IsFailed)
return ResultHelpers.CreateFailure(getDepositListResult);

Expand Down Expand Up @@ -346,7 +335,7 @@ public async Task<Result> MakeMerchantWithdrawal(MerchantCommands.MakeMerchantWi

try
{
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand All @@ -362,7 +351,7 @@ public async Task<Result> MakeMerchantWithdrawal(MerchantCommands.MakeMerchantWi
if (validateResult.IsFailed)
return ResultHelpers.CreateFailure(validateResult);

Result<MerchantDepositListAggregate> getDepositListResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest<MerchantDepositListAggregate>(command.MerchantId, ct), command.MerchantId, cancellationToken).ConfigureAwait(false);
Result<MerchantDepositListAggregate> getDepositListResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest<MerchantDepositListAggregate>(command.MerchantId, ct), command.MerchantId, cancellationToken);
if (getDepositListResult.IsFailed)
return ResultHelpers.CreateFailure(getDepositListResult);

Expand Down Expand Up @@ -407,7 +396,7 @@ public async Task<Result> MakeMerchantWithdrawal(MerchantCommands.MakeMerchantWi
public async Task<Result> AddContractToMerchant(MerchantCommands.AddMerchantContractCommand command, CancellationToken cancellationToken)
{
try {
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down Expand Up @@ -449,7 +438,7 @@ public async Task<Result> UpdateMerchant(MerchantCommands.UpdateMerchantCommand
{
try
{
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down Expand Up @@ -486,7 +475,7 @@ public async Task<Result> AddMerchantAddress(MerchantCommands.AddMerchantAddress
{
try
{
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down Expand Up @@ -527,7 +516,7 @@ public async Task<Result> UpdateMerchantAddress(MerchantCommands.UpdateMerchantA
{
try
{
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down Expand Up @@ -569,7 +558,7 @@ public async Task<Result> AddMerchantContact(MerchantCommands.AddMerchantContact
{
try
{
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down Expand Up @@ -604,7 +593,7 @@ public async Task<Result> AddMerchantContact(MerchantCommands.AddMerchantContact
public async Task<Result> UpdateMerchantContact(MerchantCommands.UpdateMerchantContactCommand command,
CancellationToken cancellationToken) {
try {
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down Expand Up @@ -636,7 +625,7 @@ public async Task<Result> UpdateMerchantContact(MerchantCommands.UpdateMerchantC
public async Task<Result> RemoveOperatorFromMerchant(MerchantCommands.RemoveOperatorFromMerchantCommand command, CancellationToken cancellationToken)
{
try {
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down Expand Up @@ -668,7 +657,7 @@ public async Task<Result> RemoveContractFromMerchant(MerchantCommands.RemoveMerc
{
try
{
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down Expand Up @@ -720,7 +709,7 @@ public async Task<Result> SwapMerchantDevice(MerchantCommands.SwapMerchantDevice
{
try
{
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Shared.Results;
using SimpleResults;
using TransactionProcessor.Aggregates;
using TransactionProcessor.BusinessLogic.Common;
using TransactionProcessor.BusinessLogic.Requests;

namespace TransactionProcessor.BusinessLogic.Services
Expand All @@ -30,7 +31,7 @@ public OperatorDomainService(Func<IAggregateService> aggregateService) {
public async Task<Result> CreateOperator(OperatorCommands.CreateOperatorCommand command, CancellationToken cancellationToken)
{
try {
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down Expand Up @@ -66,7 +67,7 @@ public async Task<Result> UpdateOperator(OperatorCommands.UpdateOperatorCommand
{
try
{
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken).ConfigureAwait(false);
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
if (estateResult.IsFailed)
return ResultHelpers.CreateFailure(estateResult);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Shared.Results;
using TransactionProcessor.Aggregates;
using TransactionProcessor.BusinessLogic.Common;
using TransactionProcessor.Database.Contexts;
using TransactionProcessor.DataTransferObjects.Responses.Estate;
using TransactionProcessor.Models.Estate;
Expand Down
Loading