Ejemplo n.º 1
0
        private async Task <Response <Sandwich> > IsValid(UpdateSandwichCommand request, Sandwich sandwichEntity)
        {
            if (sandwichEntity == null)
            {
                return(new Response <Sandwich>("Lanche não encontrado!"));
            }

            if (string.IsNullOrEmpty(request.Name))
            {
                return(new Response <Sandwich>("Informe o nome do lanche!"));
            }

            if (request.Ingredients == null || request.Ingredients.Count == 0)
            {
                return(new Response <Sandwich>("Nenhum ingrediente informado!"));
            }

            //Atualiza os ingredientes com a posição atual do db.
            for (var i = 0; i < request.Ingredients.Count; i++)
            {
                request.Ingredients[i].Ingredient = await _ingredientRepository.FindByIdAsync(request.Ingredients[i].IngredientId);
            }

            if (request.Ingredients.Any(i => (i.Ingredient.Deleted.HasValue && i.Ingredient.Deleted.Value) && !i.Deleted.HasValue || !i.Deleted.Value))
            {
                return(new Response <Sandwich>($"Ingrediente {request.Ingredients.First(i => (i.Ingredient.Deleted.HasValue && i.Ingredient.Deleted.Value) && !i.Deleted.HasValue || !i.Deleted.Value).Ingredient.Name} indisponível!"));
            }

            if (request.Ingredients.Any(i => (!i.Deleted.HasValue || !i.Deleted.Value) && i.Quantity <= 0))
            {
                return(new Response <Sandwich>($"Ingrediente {request.Ingredients.First(i => (!i.Deleted.HasValue || !i.Deleted.Value) && i.Quantity <= 0).Ingredient.Name} com quantidade inválida!"));
            }

            return(null);
        }
Ejemplo n.º 2
0
        //public Task<SaveCategoryResponse> UpdateAsync(int id, Category category)
        //{
        //    throw new NotImplementedException();
        //}
        public async Task <IngredientResponse> UpdateAsync(int id, Ingredients ingredient)
        {
            var existingIngredient = await _ingredientRepository.FindByIdAsync(id);

            if (existingIngredient == null)
            {
                return(new IngredientResponse("Category not found."));
            }

            existingIngredient.Name = ingredient.Name;

            try
            {
                _ingredientRepository.Update(existingIngredient);
                await _unitOfWork.CompleteAsync();

                return(new IngredientResponse(existingIngredient));
            }
            catch (Exception ex)
            {
                // Do some logging stuff
                return(new IngredientResponse($"An error occurred when updating the category: {ex.Message}"));
            }
        }
Ejemplo n.º 3
0
        private async Task <Response <Order> > OrderIsValid(CreateOrderCommand request)
        {
            if (string.IsNullOrEmpty(_appContext.UserName))
            {
                return(new Response <Order>("Responsável pela operação não informado!"));
            }

            if (request.Sandwiches == null || request.Sandwiches.Count == 0)
            {
                return(new Response <Order>("Informe ao menos um lanche para abrir o pedido!"));
            }

            //Atualiza as informações do pedido com a última posição do db
            var sandwiches = OrderSandwichs(request);

            for (var i = 0; i < sandwiches.Count; i++)
            {
                sandwiches[i].Sandwich = await _sandwichRepository.FindByIdAsync(sandwiches[i].SandwichId);

                if (sandwiches[i].AdditionalIngredients == null)
                {
                    sandwiches[i].AdditionalIngredients = new List <OrderSandwichIngredient>();
                }

                for (var x = 0; x < sandwiches[i].AdditionalIngredients.Count; x++)
                {
                    sandwiches[i].AdditionalIngredients[x].Ingredient = await _ingredientRepository.FindByIdAsync(sandwiches[i].AdditionalIngredients[x].IngredientId);
                }
            }

            if (sandwiches.Any(x => (x.Sandwich.Deleted.HasValue && x.Sandwich.Deleted.Value) || x.Sandwich.Ingredients.Any(i => i.Deleted.HasValue && i.Deleted.Value)))
            {
                return(new Response <Order>($"Lanche {sandwiches.First(x => (x.Sandwich.Deleted.HasValue && x.Sandwich.Deleted.Value) || x.Sandwich.Ingredients.Any(i => i.Deleted.HasValue && i.Deleted.Value)).Sandwich.Name} indisponível!"));
            }

            if (sandwiches.Any(x => x.AdditionalIngredients.Any(ai => (!ai.Deleted.HasValue || !ai.Deleted.Value) && (ai.Ingredient == null || (ai.Ingredient.Deleted.HasValue && ai.Ingredient.Deleted.Value)))))
            {
                var sandwich   = sandwiches.First(x => x.AdditionalIngredients.Any(ai => (!ai.Deleted.HasValue || !ai.Deleted.Value) && (ai.Ingredient == null || (ai.Ingredient.Deleted.HasValue && ai.Ingredient.Deleted.Value))));
                var ingredient = sandwich.AdditionalIngredients.First(ai => (!ai.Deleted.HasValue || !ai.Deleted.Value) && (ai.Ingredient == null || (ai.Ingredient.Deleted.HasValue && ai.Ingredient.Deleted.Value)));

                return(new Response <Order>($"O ingrediente adicional {ingredient.Ingredient.Name} do Lanche {request.Sandwiches.First(x => x.Sandwich.Deleted.HasValue && x.Sandwich.Deleted.Value).Sandwich.Name}, está indisponível!"));
            }

            return(null);
        }
Ejemplo n.º 4
0
        public async Task <Response <Ingredient> > Handle(UpdateIngredientCommand request, CancellationToken cancellationToken)
        {
            var ingredientEntity = await _repository.FindByIdAsync(request.Id);

            if (ingredientEntity == null)
            {
                return(new Response <Ingredient>("Ingrediente não encontrado!"));
            }

            if (string.IsNullOrEmpty(_appContext.UserName))
            {
                return(new Response <Ingredient>("Responsável pela operação não informado!"));
            }

            if (ingredientEntity.Cost > ingredientEntity.Price)
            {
                return(new Response <Ingredient>("O custo do ingrediente não pode ser maior que o preço de venda!"));
            }

            ingredientEntity.Name           = request.Name;
            ingredientEntity.Price          = request.Price;
            ingredientEntity.Cost           = request.Cost;
            ingredientEntity.Deleted        = request.Deleted;
            ingredientEntity.LastModifiedBy = _appContext.UserName;
            ingredientEntity.LastModified   = DateTime.Now;

            if (request.Deleted.HasValue && request.Deleted.Value)
            {
                ingredientEntity.DeletedBy = _appContext.UserName;
                ingredientEntity.DeletedAt = DateTime.Now;
            }

            _repository.Update(ingredientEntity);
            await _unitOfWork.CompleteAsync();

            return(new Response <Ingredient>(ingredientEntity));
        }
Ejemplo n.º 5
0
 public async Task <Ingredient> Handle(GetIngredientCommand request, CancellationToken cancellationToken)
 {
     return(await _repository.FindByIdAsync(request.Id));
 }
Ejemplo n.º 6
0
 public async Task <Ingredient> FindByIdAsync(int id)
 {
     return(await _ingredientRepository.FindByIdAsync(id));
 }