Exemplo n.º 1
0
        public async Task <RepositoryResponse <bool> > InitCms(string siteName, InitCulture culture)
        {
            RepositoryResponse <bool> result         = new RepositoryResponse <bool>();
            MixCmsContext             context        = null;
            MixCmsAccountContext      accountContext = null;
            MixChatServiceContext     messengerContext;
            IDbContextTransaction     transaction    = null;
            IDbContextTransaction     accTransaction = null;
            bool isSucceed = true;

            try
            {
                if (!string.IsNullOrEmpty(MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION)))
                {
                    context          = new MixCmsContext();
                    accountContext   = new MixCmsAccountContext();
                    messengerContext = new MixChatServiceContext();
                    //MixChatServiceContext._cnn = MixService.GetConnectionString(MixConstants.CONST_CMS_CONNECTION);
                    await context.Database.MigrateAsync();

                    await accountContext.Database.MigrateAsync();

                    await messengerContext.Database.MigrateAsync();

                    transaction = context.Database.BeginTransaction();

                    var countCulture = context.MixCulture.Count();

                    var isInit = countCulture > 0;

                    if (!isInit)
                    {
                        MixService.SetConfig <string>("SiteName", siteName);
                        isSucceed = InitCultures(culture, context, transaction);
                        isSucceed = isSucceed && InitPositions(context, transaction);
                        isSucceed = isSucceed && await InitConfigurationsAsync(siteName, culture, context, transaction);

                        isSucceed = isSucceed && await InitLanguagesAsync(culture, context, transaction);

                        isSucceed = isSucceed && InitThemes(siteName, context, transaction);
                    }
                    else
                    {
                        isSucceed = true;
                    }

                    if (isSucceed && context.MixPage.Count() == 0)
                    {
                        var cate = new MixPage()
                        {
                            Id              = 1,
                            Level           = 0,
                            Title           = "Home",
                            Specificulture  = culture.Specificulture,
                            Template        = "Pages/_Home.cshtml",
                            Type            = (int)MixPageType.Home,
                            CreatedBy       = "Admin",
                            CreatedDateTime = DateTime.UtcNow,
                            Status          = (int)PageStatus.Published
                        };


                        context.Entry(cate).State = EntityState.Added;
                        var alias = new MixUrlAlias()
                        {
                            Id              = 1,
                            SourceId        = "1",
                            Type            = (int)UrlAliasType.Page,
                            Specificulture  = culture.Specificulture,
                            CreatedDateTime = DateTime.UtcNow,
                            Alias           = cate.Title.ToLower()
                        };
                        context.Entry(alias).State = EntityState.Added;

                        var createVNHome = await context.SaveChangesAsync().ConfigureAwait(false);

                        isSucceed = createVNHome > 0;

                        var cate404 = new MixPage()
                        {
                            Id              = 2,
                            Title           = "404",
                            Level           = 0,
                            Specificulture  = culture.Specificulture,
                            Template        = "Pages/_404.cshtml",
                            Type            = (int)MixPageType.Article,
                            CreatedBy       = "Admin",
                            CreatedDateTime = DateTime.UtcNow,
                            Status          = (int)PageStatus.Published
                        };

                        var alias404 = new MixUrlAlias()
                        {
                            Id              = 2,
                            SourceId        = "2",
                            Type            = (int)UrlAliasType.Page,
                            Specificulture  = culture.Specificulture,
                            CreatedDateTime = DateTime.UtcNow,
                            Alias           = cate404.Title.ToLower()
                        };
                        context.Entry(cate404).State  = EntityState.Added;
                        context.Entry(alias404).State = EntityState.Added;

                        var create404 = await context.SaveChangesAsync().ConfigureAwait(false);

                        isSucceed = create404 > 0;
                    }

                    if (isSucceed)
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
                result.IsSucceed = isSucceed;
                return(result);
            }
            catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only
            {
                transaction?.Rollback();
                accTransaction?.Rollback();
                result.IsSucceed = false;
                result.Exception = ex;
                return(result);
            }
            finally
            {
                context?.Dispose();
                accountContext?.Dispose();
            }
        }
Exemplo n.º 2
0
        public override async Task <RepositoryResponse <bool> > SaveSubModelsAsync(MixCulture parent, MixCmsContext _context = null, IDbContextTransaction _transaction = null)
        {
            var result = new RepositoryResponse <bool>()
            {
                IsSucceed = true
            };

            if (Id == 0)
            {
                var getPages = await MixPages.ReadViewModel.Repository.GetModelListByAsync(c => c.Specificulture == MixService.GetConfig <string>(MixConstants.ConfigurationKeyword.DefaultCulture), _context, _transaction);

                if (getPages.IsSucceed)
                {
                    foreach (var p in getPages.Data)
                    {
                        var page = new MixPage()
                        {
                            Specificulture  = Specificulture,
                            Id              = p.Id,
                            Content         = p.Content,
                            CreatedBy       = p.CreatedBy,
                            CreatedDateTime = DateTime.UtcNow,
                            Layout          = p.Layout,
                            CssClass        = p.CssClass,
                            Excerpt         = p.Excerpt,
                            Icon            = p.Icon,
                            Image           = p.Image,
                            Level           = p.Level,
                            ModifiedBy      = p.ModifiedBy,
                            PageSize        = p.PageSize,
                            Priority        = p.Priority,
                            SeoDescription  = p.SeoDescription,
                            SeoKeywords     = p.SeoKeywords,
                            SeoName         = p.SeoName,
                            SeoTitle        = p.SeoTitle,
                            StaticUrl       = p.StaticUrl,
                            Status          = (int)p.Status,
                            Tags            = p.Tags,
                            Template        = p.Template,
                            Title           = p.Title,
                            Type            = (int)p.Type,
                        };
                        _context.Entry(page).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                    }
                }
                var getConfigurations = await MixConfigurations.ReadMvcViewModel.Repository.GetModelListByAsync(c => c.Specificulture == MixService.GetConfig <string>(MixConstants.ConfigurationKeyword.DefaultCulture), _context, _transaction);

                if (getConfigurations.IsSucceed)
                {
                    foreach (var c in getConfigurations.Data)
                    {
                        var cnf = new MixConfiguration()
                        {
                            Keyword        = c.Keyword,
                            Specificulture = Specificulture,
                            Category       = c.Category,
                            DataType       = (int)c.DataType,
                            Description    = c.Description,
                            Priority       = c.Priority,
                            Status         = (int)c.Status,
                            Value          = c.Value
                        };
                        _context.Entry(cnf).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                    }
                }

                var getLanguages = await MixLanguages.ReadMvcViewModel.Repository.GetModelListByAsync(c => c.Specificulture == MixService.GetConfig <string>(MixConstants.ConfigurationKeyword.DefaultCulture), _context, _transaction);

                if (getLanguages.IsSucceed)
                {
                    foreach (var c in getLanguages.Data)
                    {
                        var cnf = new MixLanguage()
                        {
                            Keyword        = c.Keyword,
                            Specificulture = Specificulture,
                            Category       = c.Category,
                            DataType       = (int)c.DataType,
                            Description    = c.Description,
                            Priority       = c.Priority,
                            Status         = (int)c.Status,
                            DefaultValue   = c.DefaultValue
                        };
                        _context.Entry(cnf).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                    }
                }
                _context.SaveChanges();
            }
            return(result);
        }