public coreModel.Category Create(coreModel.Category category) { if (category == null) { throw new ArgumentNullException("category"); } var dbCategory = category.ToDataModel(); using (var repository = _catalogRepositoryFactory()) { repository.Add(dbCategory); CommitChanges(repository); } //Need add seo separately if (category.SeoInfos != null) { foreach (var seoInfo in category.SeoInfos) { seoInfo.ObjectId = dbCategory.Id; seoInfo.ObjectType = typeof(coreModel.Category).Name; _commerceService.UpsertSeo(seoInfo); } } category.Id = dbCategory.Id; return(GetById(dbCategory.Id)); }
/// <summary> /// Patch changes /// </summary> /// <param name="source"></param> /// <param name="target"></param> public static void Patch(this coreModel.Category source, dataModel.Category target, PrimaryKeyResolvingMap pkMap) { if (target == null) { throw new ArgumentNullException("target"); } //TODO: temporary solution because partial update replaced not nullable properties in db entity if (source.IsActive != null) { target.IsActive = source.IsActive.Value; } //Handle three valuable states (null, empty and have value states) for case when need reset catalog or category if (source.CatalogId == String.Empty) { target.CatalogId = null; } if (source.ParentId == String.Empty) { target.ParentCategoryId = null; } var dbSource = source.ToDataModel(pkMap) as dataModel.Category; var dbTarget = target as dataModel.Category; if (dbSource != null && dbTarget != null) { var patchInjectionPolicy = new PatchInjection <dataModel.Category>(x => x.Code, x => x.Name, x => x.TaxType, x => x.CatalogId, x => x.ParentCategoryId); dbTarget.InjectFrom(patchInjectionPolicy, dbSource); if (!dbSource.CategoryPropertyValues.IsNullCollection()) { dbSource.CategoryPropertyValues.Patch(dbTarget.CategoryPropertyValues, (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } if (!dbSource.OutgoingLinks.IsNullCollection()) { dbSource.OutgoingLinks.Patch(dbTarget.OutgoingLinks, new LinkedCategoryComparer(), (sourceLink, targetLink) => sourceLink.Patch(targetLink)); } if (!dbSource.Images.IsNullCollection()) { dbSource.Images.Patch(dbTarget.Images, (sourceImage, targetImage) => sourceImage.Patch(targetImage)); } } }
public coreModel.Category Create(coreModel.Category category) { if (category == null) { throw new ArgumentNullException("category"); } var pkMap = new PrimaryKeyResolvingMap(); var dbCategory = category.ToDataModel(pkMap); using (var repository = _catalogRepositoryFactory()) { repository.Add(dbCategory); CommitChanges(repository); pkMap.ResolvePrimaryKeys(); } //Need add seo separately _commerceService.UpsertSeoForObjects(new[] { category }); return(GetById(dbCategory.Id, Domain.Catalog.Model.CategoryResponseGroup.Info)); }
/// <summary> /// Patch changes /// </summary> /// <param name="source"></param> /// <param name="target"></param> public static void Patch(this coreModel.Category source, dataModel.Category target, PrimaryKeyResolvingMap pkMap) { if (target == null) { throw new ArgumentNullException("target"); } //TODO: temporary solution because partial update replaced not nullable properties in db entity if (source.IsActive != null) { target.IsActive = source.IsActive.Value; } var dbSource = source.ToDataModel(pkMap) as dataModel.Category; var dbTarget = target as dataModel.Category; if (dbSource != null && dbTarget != null) { dbTarget.CatalogId = string.IsNullOrEmpty(dbSource.CatalogId) ? null : dbSource.CatalogId; dbTarget.ParentCategoryId = string.IsNullOrEmpty(dbSource.ParentCategoryId) ? null : dbSource.ParentCategoryId; dbTarget.Code = dbSource.Code; dbTarget.Name = dbSource.Name; dbTarget.TaxType = dbSource.TaxType; if (!dbSource.CategoryPropertyValues.IsNullCollection()) { dbSource.CategoryPropertyValues.Patch(dbTarget.CategoryPropertyValues, (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } if (!dbSource.OutgoingLinks.IsNullCollection()) { dbSource.OutgoingLinks.Patch(dbTarget.OutgoingLinks, new LinkedCategoryComparer(), (sourceLink, targetLink) => sourceLink.Patch(targetLink)); } if (!dbSource.Images.IsNullCollection()) { dbSource.Images.Patch(dbTarget.Images, (sourceImage, targetImage) => sourceImage.Patch(targetImage)); } } }
/// <summary> /// Patch changes /// </summary> /// <param name="source"></param> /// <param name="target"></param> public static void Patch(this coreModel.Category source, dataModel.Category target) { if (target == null) { throw new ArgumentNullException("target"); } //TODO: temporary solution because partial update replaced not nullable properties in db entity if (source.IsActive != null) { target.IsActive = source.IsActive.Value; } var dbSource = source.ToDataModel() as dataModel.Category; var dbTarget = target as dataModel.Category; if (dbSource != null && dbTarget != null) { var patchInjectionPolicy = new PatchInjection <dataModel.Category>(x => x.Code, x => x.Name, x => x.TaxType); target.InjectFrom(patchInjectionPolicy, source); if (!dbSource.CategoryPropertyValues.IsNullCollection()) { dbSource.CategoryPropertyValues.Patch(dbTarget.CategoryPropertyValues, (sourcePropValue, targetPropValue) => sourcePropValue.Patch(targetPropValue)); } if (!dbSource.OutgoingLinks.IsNullCollection()) { dbSource.OutgoingLinks.Patch(dbTarget.OutgoingLinks, new LinkedCategoryComparer(), (sourceLink, targetLink) => sourceLink.Patch(targetLink)); } if (!dbSource.Images.IsNullCollection()) { dbSource.Images.Patch(dbTarget.Images, (sourceImage, targetImage) => sourceImage.Patch(targetImage)); } } }