Beispiel #1
0
        public static async Task <RepositoryResponse <bool> > ImportData(
            string culture, Lib.ViewModels.MixAttributeSets.ReadViewModel attributeSet, IFormFile file)
        {
            using (var context = new MixCmsContext())
            {
                var result = new RepositoryResponse <bool>()
                {
                    IsSucceed = true
                };
                try
                {
                    List <ImportViewModel> data = LoadFileData(culture, attributeSet, file);

                    var fields   = MixAttributeFields.UpdateViewModel.Repository.GetModelListBy(f => f.AttributeSetId == attributeSet.Id).Data;
                    var priority = ImportViewModel.Repository.Count(m => m.AttributeSetName == attributeSet.Name && m.Specificulture == culture).Data;
                    foreach (var item in data)
                    {
                        priority += 1;

                        //item.Model.Id = Guid.NewGuid().ToString();
                        //item.Model.Specificulture = culture;
                        //item.Model.CreatedDateTime = DateTime.UtcNow;
                        //item.Model.Priority = context.MixAttributeSetData.Count() + 1;
                        item.Priority         = priority;
                        item.Fields           = fields;
                        item.AttributeSetName = attributeSet.Name;
                        item.ParseModel();
                        context.Entry(item.Model).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                        foreach (var val in item.Values)
                        {
                            val.DataId         = item.Id;
                            val.Specificulture = culture;
                            val.ParseModel();
                            //val.Model.DataId = item.Id;
                            //val.Model.CreatedDateTime = DateTime.UtcNow;
                            //val.Model.Specificulture = culture;
                            //val.Model.Id = Guid.NewGuid().ToString();
                            context.Entry(val.Model).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                        }
                    }
                    int tmp = await context.SaveChangesAsync();

                    //if (result.IsSucceed)
                    //{
                    //    foreach (var item in data)
                    //    {
                    //       item.GenerateCache(item.Model, item);
                    //    }
                    //}
                    return(result);
                }
                catch (Exception ex)
                {
                    result.IsSucceed = false;
                    result.Exception = ex;
                    result.Errors.Add(ex.Message);
                    return(result);
                }
            }
        }
Beispiel #2
0
        public static async Task <RepositoryResponse <bool> > ImportData(
            string culture, Lib.ViewModels.MixAttributeSets.ReadViewModel attributeSet, IFormFile file)
        {
            var result = new RepositoryResponse <bool>()
            {
                IsSucceed = true
            };

            UnitOfWorkHelper <MixCmsContext> .InitTransaction(null, null, out MixCmsContext context, out IDbContextTransaction transaction, out bool isRoot);

            try
            {
                List <ImportViewModel> data = LoadFileData(culture, attributeSet, file);

                var fields = MixAttributeFields.UpdateViewModel.Repository.GetModelListBy(f => f.AttributeSetId == attributeSet.Id, context, transaction).Data;
                foreach (var item in data)
                {
                    if (result.IsSucceed)
                    {
                        var isCreateNew = string.IsNullOrEmpty(item.Id);
                        item.Fields           = fields;
                        item.AttributeSetName = attributeSet.Name;
                        item.Status           = MixService.GetEnumConfig <MixContentStatus>(MixAppSettingKeywords.DefaultContentStatus);
                        var saveResult = await item.SaveModelAsync(true, context, transaction);

                        ViewModelHelper.HandleResult(saveResult, ref result);
                    }
                }
                UnitOfWorkHelper <MixCmsContext> .HandleTransaction(result.IsSucceed, isRoot, transaction);

                return(result);
            }
            catch (Exception ex)
            {
                return(UnitOfWorkHelper <MixCmsContext> .HandleException <bool>(ex, isRoot, transaction));
            }
            finally
            {
                if (isRoot)
                {
                    //if current Context is Root
                    UnitOfWorkHelper <MixCmsContext> .CloseDbContext(ref context, ref transaction);
                }
            }
        }
Beispiel #3
0
        private static List <ImportViewModel> LoadFileData(
            string culture, Lib.ViewModels.MixAttributeSets.ReadViewModel attributeSet, IFormFile file)
        {
            //create a list to hold all the values
            List <ImportViewModel> excelData = new List <ImportViewModel>();

            //create a new Excel package in a memorystream
            using (var stream = file.OpenReadStream())
                using (ExcelPackage excelPackage = new ExcelPackage(stream))
                {
                    //loop all worksheets
                    foreach (ExcelWorksheet worksheet in excelPackage.Workbook.Worksheets)
                    {
                        // First row is supose to be headers (list field name) => start from row 2
                        int startRow = 2;//worksheet.Dimension.Start.Row
                        //loop all rows
                        for (int i = startRow; i <= worksheet.Dimension.End.Row; i++)
                        {
                            JObject obj = new JObject();
                            //loop all columns in a row
                            for (int j = worksheet.Dimension.Start.Column; j <= worksheet.Dimension.End.Column; j++)
                            {
                                obj.Add(new JProperty(worksheet.Cells[1, j].Value.ToString(), worksheet.Cells[i, j].Value));
                            }
                            ImportViewModel data = new ImportViewModel()
                            {
                                Id               = obj["id"]?.ToString(),
                                AttributeSetId   = attributeSet.Id,
                                AttributeSetName = attributeSet.Name,
                                Specificulture   = culture,
                                Obj              = obj
                            };
                            excelData.Add(data);
                        }
                    }
                    return(excelData);
                }
        }