예제 #1
0
        public IActionResult Adicionar([FromBody] LocacaoViewModel pLocacao)
        {
            pLocacao.DataLocacao = DateTime.Now;
            pLocacao.Locacoes    = null;
            var locacao = _mapper.Map <LocacaoViewModel, Locacao>(pLocacao);

            try
            {
                _locacaoRepository.Adicionar(locacao);
                _locacaoRepository.SaveChanges();

                foreach (var fId in pLocacao.FilmesId)
                {
                    var locFilme = new LocacaoFilme
                    {
                        FilmeId   = fId,
                        LocacaoId = locacao.Id
                    };
                    _locacaoFilmeRepository.Adicionar(locFilme);
                    _locacaoFilmeRepository.SaveChanges();
                }

                var locacoesFilme = _locacaoFilmeRepository.PegarLocacoesFilmePorLocacaoId(locacao.Id, UsuarioCPF);
                MontarFilmesDaLocacao(locacao, locacoesFilme);

                var retorno = _mapper.Map <Locacao, LocacaoViewModel>(locacao);
                return(Response(true, retorno));
            }
            catch (Exception)
            {
                return(Response(false, erros: locacao.ValidationErrors));
            }
        }
예제 #2
0
        public async Task Alugar(Locacao locacao)
        {
            if (locacao.ClienteId == Guid.Empty)
            {
                throw new Exception("Cliente não pode ser nulo");
            }
            if (locacao.FilmeId == Guid.Empty)
            {
                throw new Exception("Filme não pode ser nulo");
            }

            var filme = await _filmeRepository.ObterPorId(locacao.FilmeId);

            if (filme == null)
            {
                throw new Exception("Filme não encontrado");
            }

            var cliente = await _clienteRepository.ObterPorId(locacao.ClienteId);

            if (cliente == null)
            {
                throw new Exception("Cliente não encontrado");
            }

            if (_locacaoRepository.VerificarSeClienteJaLocouFilme(locacao.ClienteId, locacao.FilmeId).Result.Any())
            {
                throw new Exception("O Filme já foi locado anteriormente");
            }

            if (filme.QuantidadeDisponivel == 0)
            {
                throw new Exception("O Filme esta indisponivel");
            }



            locacao.Inicio = DateTime.Now;

            await _locacaoRepository.Adicionar(locacao);

            filme.QuantidadeDisponivel--;
            await _filmeRepository.Atualizar(filme);
        }
예제 #3
0
        public override Locacao Adicionar(Locacao locacao)
        {
            int dispnivel = _repository.FilmeDisponivel(locacao.FilmeId);

            if (dispnivel == 0)
            {
                throw new Exception("filme indisponivel.");
            }
            try
            {
                _repository.BeginTransaction();
                _repository.UpdateLocacao(locacao.FilmeId, (dispnivel - 1));
                var a = _repository.Adicionar(locacao);
                _repository.SaveChanges();
                _repository.Commit();

                return(a);
            }
            catch (Exception)
            {
                _repository.Rollback();
                throw;
            }
        }