Exemplo n.º 1
0
        public async Task <CaixaResponse> SacarAsync(SaqueInput saqueInput)
        {
            try
            {
                var estoqueCaixas = await _estoqueCaixaRepository.ListAsync(saqueInput.CaixaId);

                var total = estoqueCaixas.Sum(ec => ec.Cedula * ec.Qtd);

                if (saqueInput.Valor > total)
                {
                    return(new CaixaResponse("Quantidade em dinheiro no caixa inferior ao valor do saque!"));
                }

                if (saqueInput.Valor > SaqueMax)
                {
                    return(new CaixaResponse($"Quantidade máxima permitida p/ saque: { SaqueMax }"));
                }

                Dictionary <int, int> cedQtd = new Dictionary <int, int>();

                foreach (var ec in estoqueCaixas)
                {
                    cedQtd.Add(ec.Cedula, ec.Qtd);
                }

                var dictSaque = ComputeSaque(cedQtd, saqueInput.Valor);

                foreach (var ec in estoqueCaixas)
                {
                    ec.Qtd -= dictSaque[ec.Cedula];
                }

                await _unitOfWork.CompleteAsync();

                var caixa = await _caixaRepository.FindByIdAsync(saqueInput.CaixaId);

                return(new CaixaResponse(caixa));
            }
            catch (Exception ex) {
                return(new CaixaResponse($"An error occurred on saque: {ex.Message}"));
            }
        }
        public async Task <ActionResult <CaixaResponse> > SacarCaixaAsync([FromBody] SaqueInput saqueInput)
        {
            var caixas = await _caixaService.SacarAsync(saqueInput);

            return(Ok(caixas));
        }