Пример #1
0
        public async Task <InvoiceDto> SaveAsync(Guid yearId, InvoiceDto dto)
        {
            if (yearId == null || yearId == Guid.Empty)
            {
                throw new ArgumentNullException("yearId", "Year id is missing");
            }

            Invoice invoice = _mapper.Map <Invoice>(dto);

            if (invoice.Id == Guid.Empty)
            {
                invoice.YearId       = yearId;
                invoice.DateCreated  = DateTime.UtcNow;
                invoice.DateModified = null;
                await _context.Invoices.AddAsync(invoice);
            }
            else
            {
                Invoice dbInvoice = await _context.Invoices
                                    .Where(i => i.Id == invoice.Id)
                                    .Include(i => i.InvoiceItems).ThenInclude(ii => ii.Stock)
                                    .SingleOrDefaultAsync();

                if (dbInvoice == null)
                {
                    invoice.DateCreated  = DateTime.UtcNow;
                    invoice.DateModified = null;
                    await _context.Invoices.AddAsync(invoice);
                }
                else
                {
                    invoice.DateModified = DateTime.UtcNow;
                    // delete all linet items that no longer exists
                    foreach (InvoiceItem dbLineItem in dbInvoice.InvoiceItems)
                    {
                        if (invoice.InvoiceItems.All(ii => ii.InvoiceItemId != dbLineItem.InvoiceItemId))
                        {
                            _context.Set <InvoiceItem>().Remove(dbLineItem);
                        }
                    }
                    // copy current (incoming) values to db
                    _context.Entry(dbInvoice).CurrentValues.SetValues(invoice);
                    var itemPairs = from curr in invoice.InvoiceItems //.Select(pi => pi.IngredientProduct)
                                    join db in dbInvoice.InvoiceItems //.Select(pi => pi.IngredientProduct)
                                    on curr.InvoiceItemId equals db.InvoiceItemId into grp
                                    from db in grp.DefaultIfEmpty()
                                    select new { curr, db };
                    foreach (var pair in itemPairs)
                    {
                        if (pair.db != null)
                        {
                            _context.Entry(pair.db).CurrentValues.SetValues(pair.curr);
                        }
                        else
                        {
                            await _context.Set <InvoiceItem>().AddAsync(pair.curr);
                        }
                    }
                }
            }
            await _context.SaveChangesAsync();

            dto = _mapper.Map <InvoiceDto>(invoice);
            return(dto);
        }
Пример #2
0
        public async Task <ProductDto> SaveAsync(Guid orgId, ProductDto dto)
        {
            if (orgId == null || orgId == Guid.Empty)
            {
                throw new ArgumentNullException("orgId", "Org id is missing");
            }

            Product product = _mapper.Map <Product>(dto);

            if (product.Id == Guid.Empty)
            {
                product.OrgId        = orgId;
                product.DateCreated  = DateTime.UtcNow;
                product.DateModified = null;
                await _context.Products.AddAsync(product);
            }
            else
            {
                Product dbProd = await _context.Products
                                 .Where(p => p.Id == product.Id)
                                 .Include(p => p.ParentIngredients)
                                 .Include(p => p.ProductPricings).ThenInclude(pr => pr.PricingRanges)
                                 .SingleOrDefaultAsync();

                if (dbProd == null)
                {
                    product.DateCreated  = DateTime.UtcNow;
                    product.DateModified = null;
                    await _context.Products.AddAsync(product);
                }
                else
                {
                    product.DateModified = DateTime.UtcNow;
                    // delete all ingredients that no longer exists
                    foreach (ProductIngredient dbIngredient in dbProd.ParentIngredients)
                    {
                        if (product.ParentIngredients.All(pi => pi.ParentProductId != dbIngredient.ParentProductId && pi.ChildProductId != dbIngredient.ChildProductId))
                        {
                            _context.Set <ProductIngredient>().Remove(dbIngredient);
                        }
                    }
                    foreach (ProductPricing dbPricing in dbProd.ProductPricings)
                    {
                        if (product.ProductPricings.All(pp => pp.Id != dbPricing.Id))
                        {
                            _context.Set <ProductPricing>().Remove(dbPricing);
                        }
                    }
                    // copy current (incoming) values to db
                    _context.Entry(dbProd).CurrentValues.SetValues(product);
                    var ingredientPairs = from curr in product.ParentIngredients //.Select(pi => pi.IngredientProduct)
                                          join db in dbProd.ParentIngredients    //.Select(pi => pi.IngredientProduct)
                                          on new { curr.ParentProductId, curr.ChildProductId } equals
                    new { db.ParentProductId, db.ChildProductId } into grp
                    from db in grp.DefaultIfEmpty()
                    select new { curr, db };
                    foreach (var pair in ingredientPairs)
                    {
                        if (pair.db != null)
                        {
                            _context.Entry(pair.db).CurrentValues.SetValues(pair.curr);
                            _context.Set <ProductIngredient>().Update(pair.db);
                        }
                        else
                        {
                            await _context.Set <ProductIngredient>().AddAsync(pair.curr);
                        }
                    }
                    var pricingPairs = from curr in product.ProductPricings //.Select(pi => pi.IngredientProduct)
                                       join db in dbProd.ProductPricings    //.Select(pi => pi.IngredientProduct)
                                       on curr.Id equals db.Id into grp
                                       from db in grp.DefaultIfEmpty()
                                       select new { curr, db };
                    foreach (var pair in pricingPairs)
                    {
                        if (pair.db != null)
                        {
                            _context.Entry(pair.db).CurrentValues.SetValues(pair.curr);
                            _context.Set <ProductPricing>().Update(pair.db);
                        }
                        else
                        {
                            await _context.Set <ProductPricing>().AddAsync(pair.curr);
                        }
                    }
                }
            }
            await _context.SaveChangesAsync();

            dto = _mapper.Map <ProductDto>(product);
            return(dto);
        }