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 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);
        }