Beispiel #1
0
        public async Task <bool> ReplaceTraceAsync(int id, ReplaceTraceDto replaceTraceDto)
        {
            Check.NotNull(replaceTraceDto, nameof(replaceTraceDto));

            if (string.IsNullOrWhiteSpace(replaceTraceDto.Title) ||
                replaceTraceDto.DueDate < LocalDate.FromDateTime(DateTime.Today))
            {
                throw new BusinessValidationException(
                          string.Format(TextConstants.UpdateTraceWithoutTitleOrFutureDateErrorMessageFormat, id));
            }

            if (!await _traceRepository.ExistsAsync(t => t.Id == id))
            {
                return(false);
            }

            var trace = await _traceRepository.GetAsync(id);

            trace.Description  = replaceTraceDto.Description.ValueOrDefault();
            trace.Title        = replaceTraceDto.Title;
            trace.DueDate      = replaceTraceDto.DueDate;
            trace.AssignedRole = replaceTraceDto.AssignedRole.ValueOrDefault();

            await _traceRepository.SaveAsync();

            return(true);
        }
        public async Task ShouldBeAbleToReplaceTraceAsync()
        {
            var replaceTraceDto = new ReplaceTraceDto
            {
                Description = TestOverdueTraceDescription.Some(),
                Title       = TestOverdueTraceTitle,
                DueDate     = LocalDate.FromDateTime(DateTime.Today)
            };

            _traceRepositoryMock.Setup(x => x.ExistsAsync(It.IsAny <Expression <Func <Trace, bool> > >()))
            .ReturnsAsync(true);

            _traceRepositoryMock.Setup(x => x.GetAsync(It.Is <int>(i => i == TestActiveTraceId)))
            .ReturnsAsync(new Trace
            {
                Title       = TestActiveTraceTitle,
                DueDate     = _testActiveTraceDueDate,
                Description = TestActiveTraceDescription
            });

            _traceRepositoryMock.Setup(x => x.SaveAsync())
            .Returns(Task.CompletedTask);

            var result = await _traceService.ReplaceTraceAsync(TestActiveTraceId, replaceTraceDto);

            result.Should().BeTrue();
        }
        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()
                });
            }
        }
        public async Task ShouldNotReplaceTraceWhenTitleIsEmptyAsync()
        {
            var replaceTraceDto = new ReplaceTraceDto
            {
                Description = TestOverdueTraceDescription.Some(),
                DueDate     = _testOverdueTraceDueDate
            };

            var result = await Assert.ThrowsAsync <BusinessValidationException>(() =>
                                                                                _traceService.ReplaceTraceAsync(TestCompletedTraceId, replaceTraceDto));

            result.Message.Should().Be($"Trace with id {TestCompletedTraceId} cannot be updated, the replacement must have a title and a due date in the future.");
        }
        public async Task ShouldNotReplaceWhenDueDateIsInThePastAsync()
        {
            var replaceTraceDto = new ReplaceTraceDto
            {
                Title       = TestActiveTraceTitle,
                Description = TestOverdueTraceDescription.Some(),
                DueDate     = LocalDate.FromDateTime(DateTime.Today.AddDays(-1))
            };

            var result = await Assert.ThrowsAsync <BusinessValidationException>(() =>
                                                                                _traceService.ReplaceTraceAsync(TestOverdueTraceId, replaceTraceDto));

            result.Message.Should()
            .Be(
                $"Trace with id {TestOverdueTraceId} cannot be updated, the replacement must have a title and a due date in the future.");
        }