Esempio n. 1
0
        public async Task <DeleteProductOutput> DeleteProductAsync(DeleteProductInput input)
        {
            if (input.Id == 0)
            {
                throw new UserFriendlyException("خطا در دریافت اطلاعات");
            }

            var pc = _productRepo.GetAll()
                     .SingleOrDefault(p => p.Id == input.Id);

            if (pc == null)
            {
                throw new UserFriendlyException("آیتم مورد نظر وجود ندارد ویا حذف شده است !!!");
            }

            //if (_productRepo.GetAll().Any(p => p.ProductCategoryId == input.Id))
            //{
            //    throw new UserFriendlyException("برای این  محصول درج گردیده است !!!");
            //}


            _productRepo.Delete(pc);

            return(new DeleteProductOutput());
        }
        public async Task <ValidationResult> ValidateDeleteProduct(DeleteProductInput input)
        {
            ValidationResult validationResult = new();

            Product product = await _productRepository.GetAsync(input.Id);

            if (product is null)
            {
                validationResult.Messages.Add(new(nameof(DeleteProductInput.Id), "El producto no existe."));
            }

            return(validationResult);
        }
Esempio n. 3
0
        public async Task <OperationResult> DeleteAsync(DeleteProductInput input)
        {
            var validationResult = await _validator.ValidateDeleteProduct(input);

            if (validationResult.IsSuccess)
            {
                await _repository.RemoveAsync(input.Id);

                Task overviewTask = Task.Factory.StartNew(async() =>
                {
                    var overview = await _overviewRepository.GetByProduct(new() { Id = input.Id });
                    if (overview is not null)
                    {
                        await _overviewRepository.RemoveAsync(overview);
                    }
                });

                Task badgeTask = Task.Factory.StartNew(async() =>
                {
                    var badge = await _badgeRepository.GetByProduct(new() { Id = input.Id });
                    if (badge is not null)
                    {
                        await _badgeRepository.RemoveAsync(badge);
                    }
                });

                Task imageTask = Task.Factory.StartNew(async() =>
                {
                    var images = await _imageRepository.GetProductImages(new() { Id = input.Id });
                    if (images is not null)
                    {
                        await _imageRepository.RemoveAsync(new ProductImage {
                            ProductId = input.Id
                        });
                    }
                });

                Task.WaitAll(overviewTask, badgeTask, imageTask);

                return(OperationResult.Success());
            }
            else
            {
                return(OperationResult.Fail(validationResult));
            }
        }