コード例 #1
0
        public async Task <ResultModel <bool> > ReplaceTraceAsync(ReplaceTraceItemModel replaceTraceItemModel)
        {
            try
            {
                var replaceTraceDto = new ReplaceTraceDto
                {
                    Title        = replaceTraceItemModel.Title,
                    Description  = replaceTraceItemModel.Description.SomeWhen(t => !string.IsNullOrWhiteSpace(t)),
                    DueDate      = replaceTraceItemModel.DueDate.ToLocalDateTime().Date,
                    AssignedRole = replaceTraceItemModel.AssignedRole.SomeNotNull()
                };

                var replaceResult = await _traceService.ReplaceTraceAsync(replaceTraceItemModel.Id, replaceTraceDto);

                return(new ResultModel <bool>
                {
                    Success = true,
                    Result = replaceResult.Some()
                });
            }
            catch (BusinessValidationException ex)
            {
                _logger.LogWarning(ex, $"{nameof(TraceModifierService)}.{nameof(ReplaceTraceAsync)} - Exception while trying to replace trace with Id {replaceTraceItemModel.Id}");

                return(new ResultModel <bool>
                {
                    Success = false,
                    ErrorMessage = ex.Message.Some()
                });
            }
        }
コード例 #2
0
        public async Task ShouldNotReplaceWhenValidationExceptionIsThrownAsync()
        {
            const string exceptionMessage       = "Think again";
            const int    testReplaceTraceId     = 101;
            const string testReplaceTitle       = "ReplaceMe";
            const string testReplaceDescription = "#DescribeIt";
            var          testReplaceDate        = DateTime.Now.AddDays(3);

            var testReplaceTrace = new ReplaceTraceItemModel
            {
                Title       = testReplaceTitle,
                Description = testReplaceDescription,
                DueDate     = testReplaceDate,
                Id          = testReplaceTraceId
            };

            _traceServiceMock.Setup(x => x.ReplaceTraceAsync(
                                        It.Is <int>(v => v == testReplaceTraceId),
                                        It.Is <ReplaceTraceDto>(v => v.Title == testReplaceTitle &&
                                                                v.Description.ValueOrFailure() == testReplaceDescription &&
                                                                v.DueDate.ToDateTimeUnspecified().Date == testReplaceDate.Date)))
            .ThrowsAsync(new BusinessValidationException(exceptionMessage));

            var replaceResult = await _traceModifierService.ReplaceTraceAsync(testReplaceTrace);

            replaceResult.Should().NotBeNull();
            replaceResult.Success.Should().BeFalse();
            replaceResult.Result.HasValue.Should().BeFalse();
            replaceResult.ErrorMessage.HasValue.Should().BeTrue();

            var errorMessage = replaceResult.ErrorMessage.ValueOrFailure();

            errorMessage.Should().Be(exceptionMessage);
        }