Example #1
0
        public async Task <Result> AddAsync(Dtos.ProductAddDto productDto)
        {
            var result = await Task.Run(() =>
            {
                var result = new Result();

                // add product
                var new_product        = productDto.CreateProduct();
                var new_stock          = productDto.CreateStock(new_product.Id);
                var exist_Product_Spec = new ProductSameExistSpecification(new_product);

                // is specification satisfied?
                var isStaisfy = exist_Product_Spec.IsSatisfiedBy(new_product);
                if (isStaisfy)
                {
                    var exist_product = _productRepository.Find(exist_Product_Spec).FirstOrDefault();
                    if (exist_product == null)
                    {
                        _productRepository.Add(new_product);
                        _productRepository.Complete();

                        var add_product_event = AbstractDomainEvent <ProductAddDto> .Create(_serviceProvider);
                        add_product_event.Raise(new_product);
                    }
                    else
                    {
                        result.IsError   = true;
                        result.Message   = "Product exist already";
                        result.StatuCode = 400;
                    }
                }

                if (!result.IsError)
                {
                    // add stock
                    var stock_Spec  = new StockExistSpecification(new_stock);
                    var exist_Stock = _stockRepository.Find(stock_Spec).FirstOrDefault();

                    // always statify ATM
                    var isSatisfy = stock_Spec.IsSatisfiedBy(new_stock);
                    if (isSatisfy)
                    {
                        if (exist_Stock == null)
                        {
                            _stockRepository.Add(new_stock);
                            _stockRepository.Complete();

                            var add_stock_event = AbstractDomainEvent <StockAddDto> .Create(_serviceProvider);
                            add_stock_event.Raise(new_stock);
                        }
                        else
                        {
                            result.IsError   = true;
                            result.Message   = "Stock exist already";
                            result.StatuCode = 400;
                            return(result);
                        }
                    }
                }

                return(result);
            });

            return(result);
        }