public async Task <ConvertCoinBySegmentResultDto> ExecuteAsync(ConvertCoinBySegmentDto dto, CancellationToken cancellationToken)
        {
            try
            {
                var convertionResult = await _mediator.Send(new ConvertCoinQuery(dto.CoinFrom, dto.Amount, DateTime.Today), cancellationToken);

                if (convertionResult is null)
                {
                    _notificationContext.AddNotification(ApplicationMessages.ConvertCoinBySegmentUseCase_Coin_Convertion_Is_Not_Possible);
                    return(null);
                }

                var segment = await _mediator.Send(new GetSegmentByIdQuery(dto.SegmentId), cancellationToken);

                if (segment is null)
                {
                    _notificationContext.AddNotification(ApplicationMessages.ConvertCoinBySegmentUseCase_Segment_Not_Found);
                    return(null);
                }

                var convertedAmount = segment.ApplyExchangeRate(convertionResult.ResultAmount);
                return(new ConvertCoinBySegmentResultDto
                {
                    Amount = convertionResult.Amount,
                    CoinFrom = convertionResult.CoinFrom,
                    CoinTo = convertionResult.CoinTo,
                    ConvertedAmount = convertedAmount,
                    SegmentId = segment.Id,
                    SegmentName = segment.Name
                });
            }
            catch (Exception ex)
            {
                _notificationContext.AddNotification(ex);
            }

            return(null);
        }
Example #2
0
 public async Task <IActionResult> ConvertCoinBySegment([FromQuery] ConvertCoinBySegmentDto dto, CancellationToken cancellationToken, [FromServices] IConvertCoinBySegmentUseCase convertCoinBySegmentUseCase)
 {
     return(ApiSuccessResult(await convertCoinBySegmentUseCase.ExecuteAsync(dto, cancellationToken)));
 }