Ejemplo n.º 1
0
        public async Task <Result> Handle(GetSourceQuery request, CancellationToken cancellationToken)
        {
            Result result;

            try
            {
                var source = await _sourceReadRepository.GetAsync(request.Id);

                var sourceModel = _mapper.Map <SourceModel>(source);

                result = Result.Ok(sourceModel, source.Version);
            }
            catch (EntityNotFoundDbException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.NotFound.Name,
                        Message = string.Format(HandlerFailures.NotFound, "Source"),
                        Target  = "id"
                    }
                }
                                     );
            }
            catch
            {
                result = Result.Fail(CustomFailures.GetSourceFailure);
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task GetByIdShouldSucceed()
        {
            // Arrange
            var id                     = Guid.NewGuid();
            var name                   = "name";
            var description            = "description";
            var sourceBusinessUnitName = "BSG";
            var sourceBusinessUnit     = new SourceBusinessUnit(sourceBusinessUnitName);
            var sourceBusinessUnits    = new List <SourceBusinessUnit>()
            {
                sourceBusinessUnit
            };

            RepositoryHelper.ForSource.CreateSource(id, name, description, sourceBusinessUnits);

            // Act
            var result = await _repository.GetAsync(id);

            // Assert
            result.Should().NotBeNull();
        }
        public async Task <Result> Handle(UpdateSourceCommand request, CancellationToken cancellationToken)
        {
            Result result;

            try
            {
                var source = await _sourceReadRepository.GetAsync(request.Id);

                if (source.Version != request.Version)
                {
                    throw new CommandVersionException();
                }

                if (request.Name.HasValue)
                {
                    source.ChangeName(request.Name.Value);
                }
                if (request.Description.HasValue)
                {
                    source.ChangeDescription(request.Description.Value);
                }
                if (request.SourceBusinessUnits.HasValue)
                {
                    var sourceBusinessUnits = request.SourceBusinessUnits.Value ?? new List <string>();
                    source.ClearSourceBusinessUnits();
                    foreach (var sourceBusinessUnit in sourceBusinessUnits)
                    {
                        source.AddSourceBusinessUnit(new SourceBusinessUnit(sourceBusinessUnit));
                    }
                }

                source.Version = _versionProvider.Generate();
                await _sourceWriteRepository.UpdateAsync(source);

                result = Result.Ok(source.Version);
            }
            catch (EntityNotFoundDbException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.NotFound.Name,
                        Message = string.Format(HandlerFailures.NotFound, "Source"),
                        Target  = "id"
                    }
                }
                                     );
            }
            catch (CommandVersionException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.NotMet.Name,
                        Message = HandlerFailures.NotMet,
                        Target  = "version"
                    }
                }
                                     );
            }
            catch (UniqueKeyException)
            {
                result = Result.Fail(new System.Collections.Generic.List <Failure>()
                {
                    new HandlerFault()
                    {
                        Code    = HandlerFaultCode.Conflict.Name,
                        Message = HandlerFailures.Conflict,
                        Target  = "name"
                    }
                }
                                     );
            }
            catch
            {
                result = Result.Fail(CustomFailures.UpdateSourceFailure);
            }

            return(result);
        }