public async Task UpdateAsync(Guid id, OrganizationDto model) { if (id != model.Id) { throw new BadRequestException("Org id mismatch"); } Organization org = _mapper.Map <Organization>(model); Organization dbOrg = await _context.Organizations .Where(o => o.Id == org.Id) .Include(o => o.OrgContacts).ThenInclude(oc => oc.Contact) .Include(o => o.OrgAddresses).ThenInclude(oa => oa.Address) .FirstOrDefaultAsync(); if (dbOrg == null) { //org.DateCreated = DateTime.UtcNow; //org.DateModified = null; //await _context.Organizations.AddAsync(org); throw new NotFoundException("Organization not found"); } // delete all contacts that are no longer exists foreach (OrgContact dbOrgContact in dbOrg.OrgContacts) { Contact dbContact = dbOrgContact.Contact; if (org.OrgContacts.All(oc => oc.Contact.Id != dbContact.Id)) { _context.Contacts.Remove(dbContact); } } // delete all addresses that are no longer exists foreach (OrgAddress dbOrgAddress in dbOrg.OrgAddresses) { Address dbAddress = dbOrgAddress.Address; if (org.OrgAddresses.All(oa => oa.Address.Id != dbAddress.Id)) { _context.Addresses.Remove(dbAddress); } } // copy current (incoming) values to db org.DateModified = DateTime.UtcNow; _context.Entry(dbOrg).CurrentValues.SetValues(org); #region Contacts var contactPairs = from curr in org.OrgContacts.Select(oc => oc.Contact) join db in dbOrg.OrgContacts.Select(oc => oc.Contact) on curr.Id equals db.Id into grp from db in grp.DefaultIfEmpty() select new { curr, db }; foreach (var pair in contactPairs) { if (pair.db != null) { _context.Entry(pair.db).CurrentValues.SetValues(pair.curr); _context.Contacts.Update(pair.db); } else { var orgContact = new OrgContact { OrgId = org.Id, Organization = org, ContactId = pair.curr.Id, Contact = pair.curr }; dbOrg.OrgContacts.Add(orgContact); } } #endregion #region Addresses var addressPairs = from curr in org.OrgAddresses.Select(oa => oa.Address) join db in dbOrg.OrgAddresses.Select(oa => oa.Address) on curr.Id equals db.Id into grp from db in grp.DefaultIfEmpty() select new { curr, db }; foreach (var pair in addressPairs) { if (pair.db != null) { _context.Entry(pair.db).CurrentValues.SetValues(pair.curr); _context.Addresses.Update(pair.db); } else { var orgAddress = new OrgAddress { OrgId = org.Id, Organization = org, AddressId = pair.curr.Id, Address = pair.curr }; dbOrg.OrgAddresses.Add(orgAddress); } } _context.Organizations.Update(dbOrg); #endregion await _context.SaveChangesAsync(); //model = _mapper.Map<OrganizationDto>(org); //return model; }
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); }
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); }