Example #1
0
        public async Task <ProductViewModel> UpdateProductTransaction(UserProfile user, ProductDetailViewModel product)
        {
            var business = await _businessRepo.GetByIdAsync(user.business_id.Value);

            Product pr;
            int     prodId;

            try
            {
                pr = await _prodRepo.GetByIdAsync(product.Id.Value, user.business_id.Value);

                if (pr != null)
                {
                    prodId = pr.id;
                }
                else
                {
                    throw new Exception("Не указан идентификатор товара / неверный идентификатор.");
                }
            }
            catch (Exception)
            {
                throw new Exception("Не указан идентификатор товара / неверный идентификатор.");
            }


            var price = new Price
            {
                prod_id = prodId,
            };

            var prod = new Product
            {
                id          = prodId,
                name        = product.ProdName,
                business_id = user.business_id,
                unit_id     = product.UnitId,
                attr1       = product.VendorCode,
                attr9       = product.Size,
                attr10      = product.Color
            };

            if (!string.IsNullOrEmpty(product.Category))
            {
                prod.folder_id = await _foldersDataService.GetFolderIdByPath(product.Category, business.id);
            }
            else
            {
                prod.folder_id = pr.folder_id;
            }

            var candidatePrice = _priceRepo.GetPriceByProdId(prodId);

            candidatePrice.price = product.Price;
            prod.Price           = candidatePrice;

            if (product.img != null)
            {
                try
                {
                    var imgDal = await _imgRepo.GetByIdAsync(prodId);

                    if (imgDal != null && !string.IsNullOrEmpty(imgDal.img_url))
                    {
                        var isDeleted = await _dbBase.Delete("/products/" + business.id + ". " + business.name + "/" +
                                                             imgDal.prod_id + "." + imgDal.img_name + "." + imgDal.img_type);

                        if (isDeleted)
                        {
                            imgDal.img_url      = null;
                            imgDal.img_url_temp = null;
                            await _imgRepo.UpdateImage(imgDal);
                        }
                    }

                    using (var stream = product.img.OpenReadStream())
                    {
                        var bytes = ReadToEnd(stream);
                        prod.ImgMemoryStream = new MemoryStream(bytes);
                        prod.ImgBase64       = product.ImgBase64;
                        prod.Category        = product.Category;
                        prod.Image           = imgDal;
                    }
                }
                catch (Exception)
                {
                    throw new Exception("Невозможно удалить предыдущую картинку товара.");
                }
            }

            await _productDataService.Update(prod);

            return(await GetProduct(user, prodId));
        }
Example #2
0
        public async Task <int> Insert(Product product)
        {
            using (var session = new Session())
            {
                var uow = session.UnitOfWork;
                _productRepository     = new ProductRepository(uow);
                _priceRepository       = new PriceRepository(uow);
                _businessRepository    = new BusinessRepository(uow);
                _imageRepository       = new ImagesRepository(uow);
                _strategy              = new FifoStrategy();
                _ordersRepository      = new OrdersRepository(uow);
                _orderDetailRepository = new OrderDetailRepository(uow);
                _costRepository        = new CostRepository(uow);
                var imgPath = string.Empty;

                uow.Begin();
                try
                {
                    var productId = await _productRepository.InsertUow(product);

                    if (product.Price != null && product.Price.price.HasValue)
                    {
                        await _priceRepository.InsertUow(new Price { price = product.Price.price, prod_id = productId });
                    }
                    else
                    {
                        await _priceRepository.InsertUow(new Price { prod_id = productId });
                    }

                    if (!string.IsNullOrEmpty(product.ImgBase64) && product.ImgMemoryStream != null)
                    {
                        var business = await _businessRepository.GetByIdUow(product.business_id.Value);

                        var imgParts = product.ImgBase64.Split('.');
                        imgPath = "/products/" + business.id + ". " + business.name + "/" +
                                  productId + "." + product.name + "." + imgParts.Last();
                        var imgUrl = await _dbBase.Upload(product.ImgMemoryStream, imgPath);

                        var img = new Image
                        {
                            img_url      = imgUrl,
                            prod_id      = productId,
                            img_name     = product.name,
                            img_type     = imgParts.LastOrDefault(),
                            img_url_temp = MakeTemporary(imgUrl),
                            img_path     = product.Category
                        };
                        await _imageRepository.InsertUow(img);
                    }

                    if (product.Orders.Any())
                    {
                        foreach (var order in product.Orders)
                        {
                            var orderId = await _ordersRepository.InsertUow(order);

                            foreach (var orderDetail in order.OrderDetails)
                            {
                                orderDetail.prod_id  = productId;
                                orderDetail.order_id = orderId;
                                await _orderDetailRepository.InsertUow(orderDetail);
                            }

                            var orderDal = await _ordersRepository.GetByIdWithMultiUow(orderId);

                            await _strategy.UpdateAverageCostUow(Direction.Order, orderDal, uow);
                        }
                    }
                    else if (product.Cost.Any())
                    {
                        var cost = product.Cost.FirstOrDefault();
                        if (cost != null && cost.value.HasValue)
                        {
                            cost.prod_id = productId;
                            await _costRepository.InsertUow(cost);
                        }
                        else
                        {
                            await _costRepository.InsertUow(new Cost { prod_id = productId });
                        }
                    }

                    uow.Commit();
                    return(productId);
                }
                catch (Exception e)
                {
                    uow.RollBack();
                    if (!string.IsNullOrEmpty(imgPath))
                    {
                        await _dbBase.Delete(imgPath);
                    }

                    throw e;
                }
            }
        }