Esempio n. 1
0
        public async Task <Response <Sandwich> > Handle(UpdateSandwichCommand request, CancellationToken cancellationToken)
        {
            var sandwichEntity = await _repository.FindByIdAsync(request.Id);

            var response = await IsValid(request, sandwichEntity);

            if (response == null)
            {
                sandwichEntity.Name           = request.Name;
                sandwichEntity.Ingredients    = request.Ingredients;
                sandwichEntity.LastModified   = DateTime.Now;
                sandwichEntity.LastModifiedBy = _appContext.UserName;
                sandwichEntity.Deleted        = request.Deleted;

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

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

                response = new Response <Sandwich>(sandwichEntity);
            }
            return(response);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
 public async Task <Sandwich> Handle(GetSandwichCommand request, CancellationToken cancellationToken)
 {
     return(await _repository.FindByIdAsync(request.Id));
 }