コード例 #1
0
        public async Task Update(Guid id, ProductDto productDto)
        {
            var vendor = await _vendorRepository.GetById(productDto.VendorId);

            if (vendor == null)
            {
                throw new KeyNotFoundException($"Vendor with id: {productDto.VendorId} not found.");
            }

            var category = await _categoryRepository.GetById(productDto.CategoryId);

            if (category == null)
            {
                throw new KeyNotFoundException($"Category with id: {productDto.CategoryId} not found.");
            }

            var product = await _productRepository.GetById(id);

            product.Name               = productDto.Name;
            product.Description        = productDto.Description;
            product.Code               = productDto.Code;
            product.CategoryId         = productDto.CategoryId;
            product.VendorId           = productDto.VendorId;
            product.Brand              = productDto.Brand;
            product.MinimumStock       = productDto.MinimumStock;
            product.UnitOfMeasurement  = productDto.UnitOfMeasurement;
            product.LastModificationBy = productDto.LastModificationBy;

            await _productRepository.Update(product);

            //PurchasePrice
            var lastPurchasePrice = (await _priceRepository.Find(x => x.ProductId == productDto.Id && x.PriceType == ePriceTypes.PurchasePrice))
                                    .OrderByDescending(x => x.DateTime).FirstOrDefault();

            if (lastPurchasePrice == null || lastPurchasePrice.Value != productDto.PurchasePrice.Value)
            {
                var price = new Price()
                {
                    Value     = productDto.SalePrice.Value,
                    DateTime  = DateTime.Now,
                    ProductId = productDto.Id,
                    CreatedBy = productDto.LastModificationBy.Value,
                    PriceType = ePriceTypes.PurchasePrice,
                };

                await _priceRepository.Add(price);
            }

            //SalePrice
            var latSalePrice = (await _priceRepository.Find(x => x.ProductId == productDto.Id && x.PriceType == ePriceTypes.PurchasePrice))
                               .OrderByDescending(x => x.DateTime).FirstOrDefault();

            if (latSalePrice == null || latSalePrice.Value != productDto.SalePrice.Value)
            {
                var price = new Price()
                {
                    Value     = productDto.SalePrice.Value,
                    DateTime  = DateTime.Now,
                    ProductId = productDto.Id,
                    CreatedBy = productDto.LastModificationBy.Value,
                    PriceType = ePriceTypes.SalePrice,
                };

                await _priceRepository.Add(price);
            }

            await _productRepository.CommitAsync();

            await _priceRepository.CommitAsync();
        }