Exemplo n.º 1
0
        public async Task <Unit> Handle(AtualizarEstabelecimentoCommand request, CancellationToken cancellationToken)
        {
            if (request.Estabelecimento.Codigo.ToLower() != request.Codigo.Trim().ToLower())
            {
                if (await estabelecimentoReadOnlyRepository.GetByCodigoAsync(request.Codigo.Trim()) != null)
                {
                    throw new CodigoDeEstabelecimentoEmUsoException();
                }

                request.Estabelecimento.Codigo = request.Codigo.Trim();
            }

            request.Estabelecimento.Nome = request.Nome.Trim();

            if (request.CipaId.HasValue)
            {
                request.Estabelecimento.DefinirCipa(request.CipaId.Value);
            }
            else
            {
                request.Estabelecimento.RemoverCipa();
            }

            return(await ProcederComAAtualizacaoDoEstabelecimentoAsync(request.Estabelecimento));
        }
        public async Task <CriarCommandResult> Handle(CriarEstabelecimentoCommand request, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (await estabelecimentoReadOnlyRepository.GetByCodigoAsync(request.Codigo) != null)
            {
                throw new CodigoDeEstabelecimentoEmUsoException();
            }

            var estabelecimento = new Estabelecimento(request.Codigo, request.Nome)
            {
                CreationUser = userPrincipalBuilder.UserPrincipal.UserName
            };

            if (request.CipaId.HasValue)
            {
                estabelecimento.DefinirCipa(request.CipaId.Value);
            }

            estabelecimentoWriteOnlyRepository.Insert(estabelecimento);

            await estabelecimentoWriteOnlyRepository.UnitOfWork.SaveEntitiesAsync();

            await auditTrailProvider.AddTrailsAsync(AuditOperation.Create, userPrincipalBuilder.UserPrincipal.UserName, new AuditableObjects <Estabelecimento>(estabelecimento.Id.ToString(), estabelecimento));

            return(new CriarCommandResult(estabelecimento.Id, estabelecimento.Codigo));
        }