private async Task <RepositoryResponse <bool> > ImportAttributeSetsAsync(MixCmsContext context, IDbContextTransaction transaction)
        {
            var result = new RepositoryResponse <bool>()
            {
                IsSucceed = true
            };

            if (AttributeSets != null)
            {
                var startId         = MixAttributeSets.ImportViewModel.Repository.Max(m => m.Id, context, transaction).Data;
                var startFieldId    = MixAttributeFields.UpdateViewModel.Repository.Max(m => m.Id, context, transaction).Data;
                var attributeFields = new List <MixAttributeFields.UpdateViewModel>();
                foreach (var set in AttributeSets)
                {
                    set.CreatedBy = CreatedBy;
                    if (result.IsSucceed)
                    {
                        if (!context.MixAttributeSet.Any(m => m.Name == set.Name))
                        {
                            startId++;
                            set.Id = startId;
                            set.CreatedDateTime = DateTime.UtcNow;
                            attributeFields.AddRange(set.Fields
                                                     .Where(m => !attributeFields.Any(n => n.Id == m.Id))
                                                     .ToList());
                            var saveResult = await set.SaveModelAsync(false, context, transaction);

                            ViewModelHelper.HandleResult(saveResult, ref result);
                        }
                        if (!dicAttributeSetIds.Any(m => m.Key == set.Id))
                        {
                            dicAttributeSetIds.Add(set.Id, startId);
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                // save fields
                if (result.IsSucceed)
                {
                    foreach (var field in attributeFields)
                    {
                        if (result.IsSucceed)
                        {
                            field.CreatedBy = CreatedBy;
                            var setId = dicAttributeSetIds.FirstOrDefault(m => m.Key == field.AttributeSetId);
                            field.AttributeSetId = setId.Value;
                            if (field.ReferenceId != null)
                            {
                                var refId = dicAttributeSetIds.FirstOrDefault(m => m.Key == field.ReferenceId);
                                field.ReferenceId = refId.Value;
                            }
                            if (dicFieldIds.ContainsKey(field.Id))
                            {
                                field.Id = dicFieldIds[field.Id];
                                field.CreatedDateTime = DateTime.UtcNow;
                            }
                            else
                            {
                                startFieldId++;
                                dicFieldIds.Add(field.Id, startFieldId);
                                field.Id = startFieldId;
                                field.CreatedDateTime = DateTime.UtcNow;
                            }
                            var saveResult = await field.SaveModelAsync(false, context, transaction);

                            ViewModelHelper.HandleResult(saveResult, ref result);
                        }
                        else
                        {
                            result.Errors.Add($"Cannot Import {field.Name} - {field.Id}");
                            break;
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
        private async Task <RepositoryResponse <bool> > ImportRelatedAttributeSetDatas(string desCulture, MixCmsContext context, IDbContextTransaction transaction)
        {
            var result = new RepositoryResponse <bool>()
            {
                IsSucceed = true
            };

            foreach (var item in RelatedData)
            {
                item.Id             = Guid.NewGuid().ToString();
                item.Specificulture = desCulture;
                switch (item.ParentType)
                {
                case MixEnums.MixAttributeSetDataType.System:
                    break;

                case MixEnums.MixAttributeSetDataType.Set:
                    item.AttributeSetId = dicAttributeSetIds[item.AttributeSetId];
                    break;

                case MixEnums.MixAttributeSetDataType.Post:
                    break;

                case MixEnums.MixAttributeSetDataType.Page:
                    if (dicPageIds.TryGetValue(int.Parse(item.ParentId), out int pageId))
                    {
                        item.ParentId = pageId.ToString();
                    }
                    else
                    {
                        continue;
                    }
                    break;

                case MixEnums.MixAttributeSetDataType.Module:
                    if (dicModuleIds.TryGetValue(int.Parse(item.ParentId), out int moduleId))
                    {
                        item.ParentId = moduleId.ToString();
                    }
                    else
                    {
                        continue;
                    }
                    break;

                case MixEnums.MixAttributeSetDataType.Service:
                    break;

                default:
                    break;
                }
                if (result.IsSucceed)
                {
                    var saveResult = await item.SaveModelAsync(false, context, transaction);

                    ViewModelHelper.HandleResult(saveResult, ref result);
                }
                else
                {
                    break;
                }
            }
            return(result);
        }