protected Entities.Tenant.Products.Product CreateProductEntity(Product productModel) { var product = new Entities.Tenant.Products.Product(); UpdateProductEntity(product, productModel); return(product); }
protected void UpdateProductEntity(Entities.Tenant.Products.Product productEntity, Product productModel) { PrepareDictionaryCache(); _mapper.Map(productModel, productEntity); var productAdvertiser = productEntity.ProductAdvertisers.FirstOrDefault(); if (productEntity.ProductAdvertisers.Count == 1 && productAdvertiser.Advertiser.ExternalIdentifier == productModel.AdvertiserIdentifier && productAdvertiser.Advertiser.Name == productModel.AdvertiserName) { productAdvertiser.StartDate = productModel.AdvertiserLinkStartDate; productAdvertiser.EndDate = productModel.AdvertiserLinkEndDate; } else { _dbContext.RemoveRange(productEntity.ProductAdvertisers.ToArray()); productEntity.ProductAdvertisers.Clear(); if (!string.IsNullOrWhiteSpace(productModel.AdvertiserIdentifier)) { if (_dbAdvertisers.TryGetValue(productModel.AdvertiserIdentifier, out var dbAdv)) { if (!string.Equals(dbAdv.Name, productModel.AdvertiserName)) { dbAdv.Name = productModel.AdvertiserName; } } else { dbAdv = new Advertiser { ExternalIdentifier = productModel.AdvertiserIdentifier, Name = productModel.AdvertiserName, ShortName = productModel.AdvertiserName }; _dbContext.Add(dbAdv); _dbAdvertisers.Add(dbAdv.ExternalIdentifier, dbAdv); } productAdvertiser = new ProductAdvertiser { Product = productEntity, Advertiser = dbAdv, StartDate = productModel.AdvertiserLinkStartDate, EndDate = productModel.AdvertiserLinkEndDate }; _dbContext.Add(productAdvertiser); productEntity.ProductAdvertisers.Add(productAdvertiser); } } var productAgency = productEntity.ProductAgencies.FirstOrDefault(); if (productEntity.ProductAgencies.Count == 1 && productAgency.Agency.ExternalIdentifier == productModel.AgencyIdentifier && productAgency.Agency.Name == productModel.AgencyName) { productAgency.StartDate = productModel.AgencyStartDate; productAgency.EndDate = productModel.AgencyLinkEndDate; } else { _dbContext.RemoveRange(productEntity.ProductAgencies.ToArray()); productEntity.ProductAgencies.Clear(); if (!string.IsNullOrWhiteSpace(productModel.AgencyIdentifier)) { Entities.Tenant.AgencyGroup dbAgencyGroup = null; if (productModel.AgencyGroup != null) { var key = HashCode.Combine(productModel.AgencyGroup.ShortName, productModel.AgencyGroup.Code); if (!_dbAgencyGroups.TryGetValue(key, out dbAgencyGroup)) { dbAgencyGroup = _mapper.Map <Entities.Tenant.AgencyGroup>(productModel.AgencyGroup); _dbContext.Add(dbAgencyGroup); _dbAgencyGroups.Add(key, dbAgencyGroup); } } if (_dbAgencies.TryGetValue(productModel.AgencyIdentifier, out var dbAgency)) { if (!string.Equals(dbAgency.Name, productModel.AgencyName)) { dbAgency.Name = productModel.AgencyName; } } else { dbAgency = new Agency { ExternalIdentifier = productModel.AgencyIdentifier, Name = productModel.AgencyName, ShortName = productModel.AgencyName }; _dbContext.Add(dbAgency); _dbAgencies.Add(dbAgency.ExternalIdentifier, dbAgency); } productAgency = new ProductAgency { Product = productEntity, Agency = dbAgency, AgencyGroup = dbAgencyGroup, StartDate = productModel.AgencyStartDate, EndDate = productModel.AgencyLinkEndDate }; _dbContext.Add(productAgency); productEntity.ProductAgencies.Add(productAgency); } } var productPerson = productEntity.ProductPersons.FirstOrDefault(); if (productEntity.ProductPersons.Count == 1 && productPerson.Person.ExternalIdentifier == productModel.SalesExecutive?.Identifier && productPerson.Person.Name == productModel.SalesExecutive?.Name) { productPerson.StartDate = productModel.SalesExecutive.StartDate; productPerson.EndDate = productModel.SalesExecutive.EndDate; } else { _dbContext.RemoveRange(productEntity.ProductPersons.ToArray()); productEntity.ProductPersons.Clear(); if (!(productModel.SalesExecutive is null)) { if (_dbPersons.TryGetValue(productModel.SalesExecutive.Identifier, out var dbPerson)) { if (!string.Equals(dbPerson.Name, productModel.SalesExecutive.Name)) { dbPerson.Name = productModel.SalesExecutive.Name; } } else { dbPerson = _mapper.Map <Person>(productModel.SalesExecutive); _dbContext.Add(dbPerson); _dbPersons.Add(dbPerson.ExternalIdentifier, dbPerson); } productPerson = new ProductPerson { Product = productEntity, Person = dbPerson, StartDate = productModel.SalesExecutive.StartDate, EndDate = productModel.SalesExecutive.EndDate }; _dbContext.Add(productPerson); productEntity.ProductPersons.Add(productPerson); } } }