private void UpdateExistingProductVersion(DAL.Product p, IEnumerable <int> regionIds, DateTime modification) { var pv = MapProductVersion(p, modification); pv.Deleted = false; _context.ProductVersions.Add(pv); }
private void UpdateDeletedProductVersion(DAL.Product p, string data) { var pv = MapProductVersion(p, DateTime.Now); pv.Deleted = true; pv.Data = data; _context.ProductVersions.Add(pv); }
private ProductVersion MapProductVersion(DAL.Product p, DateTime modification) { return(new ProductVersion { Modification = modification, DpcId = p.DpcId, Alias = p.Alias, Created = p.Created, Updated = p.Updated, Data = p.Data, Hash = p.Hash, Format = p.Format, IsLive = p.IsLive, Language = p.Language, MarketingProductId = p.MarketingProductId, ProductType = p.ProductType, Slug = p.Slug, Title = p.Title, UserUpdated = p.UserUpdated, UserUpdatedId = p.UserUpdatedId, Version = p.Version }); }
public ServiceResult UpdateProduct(ProductLocator locator, Product product, string data, string userName, int userId) { return(RunAction(new UserContext(), null, () => { var p = _context.GetProduct(locator, product.Id); var isNew = p == null; var now = DateTime.Now; if (isNew) { p = new DAL.Product { Created = now, DpcId = product.Id }; _context.FillProduct(locator, p); } if (product.MarketingProduct != null) { p.Alias = product.MarketingProduct.Alias; p.Title = product.MarketingProduct.Title; p.MarketingProductId = product.MarketingProduct.Id; } if (string.IsNullOrEmpty(p.Title)) { p.Title = product.Title; } if (string.IsNullOrEmpty(p.Alias)) { p.Alias = product.Alias; } p.Updated = now; p.Data = data; p.Hash = GetHash(data); p.ProductType = product.ProductType; p.UserUpdated = userName; p.UserUpdatedId = userId; var regionIds = new List <int>(); if (product.Regions != null) { if (p.Id != 0) { foreach (var pr in p.ProductRegions) { if (!product.Regions.Select(x => x.Id).Contains(pr.RegionId)) { regionIds.Add(pr.RegionId); _context.ProductRegions.Remove(pr); } } foreach (var r in product.Regions.Select(x => x.Id)) { if (!p.ProductRegions.Select(x => x.RegionId).Contains(r)) { var pr = new ProductRegion { RegionId = r, Product = p }; _context.ProductRegions.Add(pr); } } } else { foreach (var r in product.Regions.Select(x => x.Id)) { var pr = new ProductRegion { RegionId = r, Product = p }; _context.ProductRegions.Add(pr); } } } p.DpcId = product.Id; if (isNew) { _context.Products.Add(p); } if (locator.UseProductVersions) { UpdateExistingProductVersion(p, product.Regions?.Select(r => r.Id), now); } _context.SaveChanges(); regionIds.AddRange(p.ProductRegions.Select(x => x.RegionId)); foreach (var rid in regionIds) { var ru = _context.RegionUpdates.SingleOrDefault(n => n.RegionId == rid) ?? new RegionUpdate() { RegionId = rid }; ru.Updated = DateTime.Now; if (ru.Id <= 0) { _context.Add(ru); } } })); }