public bool DeletePageContent(Guid id)
        {
            try
            {
                using (var context = new DeviserDbContext(DbOptions))
                {
                    var dbpageContent = GetDeletedPageContent(id);

                    if (dbpageContent != null)
                    {
                        context.PageContent.Remove(dbpageContent);

                        //ContentPermission
                        var contentPermission = context.ContentPermission
                                                .Where(p => p.PageContentId == id)
                                                .ToList();
                        context.ContentPermission.RemoveRange(contentPermission);

                        //PageContentTranslation
                        var contentTranslation = context.PageContentTranslation
                                                 .Where(p => p.PageContentId == dbpageContent.Id)
                                                 .ToList();
                        context.PageContentTranslation.RemoveRange(contentTranslation);

                        context.SaveChanges();
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error occured while deleting page content", ex);
            }
            return(false);
        }
예제 #2
0
        /// <summary>
        /// Add permissions only if its not exist in db
        /// </summary>
        /// <param name="pagePermissions"></param>
        /// <returns></returns>
        public IList <ModulePermission> AddModulePermissions(IList <ModulePermission> pagePermissions)
        {
            using var context = new DeviserDbContext(_dbOptions);
            if (pagePermissions == null || pagePermissions.Count <= 0)
            {
                throw new InvalidOperationException($"Invalid parameter, Parameter {nameof(pagePermissions)} cannot be empty");
            }
            var dbModulePermissions = _mapper.Map <IList <Entities.ModulePermission> >(pagePermissions);
            //Filter new permissions which are not in db and add all of them
            var toAdd = dbModulePermissions.Where(modulePermission => !context.ModulePermission.Any(dbPermission =>
                                                                                                    dbPermission.PermissionId == modulePermission.PermissionId &&
                                                                                                    dbPermission.PageModuleId == modulePermission.PageModuleId &&
                                                                                                    dbPermission.RoleId == modulePermission.RoleId)).ToList();

            if (toAdd.Count > 0)
            {
                foreach (var permission in toAdd)
                {
                    //permission.Page = null;
                    if (permission.Id == Guid.Empty)
                    {
                        permission.Id = Guid.NewGuid();
                    }
                    context.ModulePermission.Add(permission);
                }
            }

            context.SaveChanges();

            //Refresh cache
            GetPageModules(true);

            return(_mapper.Map <IList <ModulePermission> >(toAdd));
        }
예제 #3
0
        public List <Page> GetPages()
        {
            try
            {
                //var cacheName = nameof(GetPages);
                //var result = GetResultFromCache<List<Page>>(cacheName);
                //if (result != null)
                //{
                //    return result;
                //}

                using (var context = new DeviserDbContext(DbOptions))
                {
                    var dbResult = context.Page
                                   .Where(e => e.ParentId != null).AsNoTracking()
                                   //.Include("PageTranslations").Include("ChildPages").Include("PageModules").Include("PageModules.Module")
                                   .Include(p => p.PagePermissions)
                                   .Include(p => p.PageTranslation)
                                   .Include(p => p.PageModule).ThenInclude(pm => pm.Module)
                                   .OrderBy(p => p.PageOrder)
                                   .ToList();

                    var result = Mapper.Map <List <Page> >(dbResult);
                    //AddResultToCache(cacheName, result);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error occured while getting all pages", ex);
            }
            return(null);
        }
예제 #4
0
        public bool PublishPage(Guid id)
        {
            try
            {
                using (var context = new DeviserDbContext(DbOptions))
                {
                    var permission = context.PagePermission
                                     .Where(p => p.PageId == id && p.RoleId == Globals.AllUsersRoleId).FirstOrDefault();

                    if (permission == null)
                    {
                        Entities.PagePermission addpermission = new Entities.PagePermission();
                        addpermission.PageId       = id;
                        addpermission.PermissionId = Globals.PageViewPermissionId;
                        addpermission.RoleId       = Globals.AllUsersRoleId;
                        context.PagePermission.Add(addpermission);
                    }
                    context.SaveChanges();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error occured while publishing the page", ex);
                return(false);
            }
        }
예제 #5
0
        public Page GetPageAndPageTranslations(Guid pageId)
        {
            try
            {
                //var cacheName = $"{nameof(GetPageAndPageTranslations)}_{pageId}";
                //var result = GetResultFromCache<Page>(cacheName);
                //if (result != null)
                //{
                //    return result;
                //}

                using (var context = new DeviserDbContext(DbOptions))
                {
                    var dbResult = context.Page
                                   .Where(e => e.Id == pageId).AsNoTracking()
                                   .Include(p => p.PageTranslation)
                                   .FirstOrDefault();

                    var result = Mapper.Map <Page>(dbResult);
                    //AddResultToCache(cacheName, result);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error occured while calling GetPageAndPageTranslations", ex);
            }
            return(null);
        }
        //Custom Field Declaration
        public List <SiteSetting> GetSettings()
        {
            try
            {
                //var cacheName = nameof(GetSettings);
                //var result = GetResultFromCache<List<SiteSetting>>(cacheName);
                //if (result != null)
                //{
                //    return result;
                //}

                using (var context = new DeviserDbContext(_dbOptions))
                {
                    var dbResult = context.SiteSetting.ToList();
                    var result   = Mapper.Map <List <SiteSetting> >(dbResult);
                    //AddResultToCache(cacheName, result);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error occured while getting GetSettings", ex);
            }
            return(null);
        }
 /// <summary>
 /// It updates given list of page content if the content exisit in db, else it adds the content
 /// </summary>
 /// <param name="pageContents">
 /// List of page contents
 /// </param>
 public void AddOrUpdate(List <PageContent> pageContents)
 {
     try
     {
         var dbPageContents = Mapper.Map <List <Entities.PageContent> >(pageContents);
         using (var context = new DeviserDbContext(DbOptions))
         {
             foreach (var content in dbPageContents)
             {
                 content.LastModifiedDate = DateTime.Now;
                 if (context.PageContent.Any(pc => pc.Id == content.Id))
                 {
                     //content exist, therefore update the content
                     context.PageContent.Update(content);
                 }
                 else
                 {
                     context.PageContent.Add(content);
                 }
             }
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         _logger.LogError("Error occured while updating contents", ex);
         throw;
     }
 }
 /// <summary>
 /// It creates new ContentType
 /// </summary>
 /// <param name="contentType"></param>
 /// <returns></returns>
 public ContentType CreateContentType(ContentType contentType)
 {
     try
     {
         using (var context = new DeviserDbContext(DbOptions))
         {
             var dbContentType = Mapper.Map <Entities.ContentType>(contentType);
             dbContentType.Id = Guid.NewGuid();
             if (dbContentType.ContentTypeProperties != null && dbContentType.ContentTypeProperties.Count > 0)
             {
                 foreach (var ctp in dbContentType.ContentTypeProperties)
                 {
                     ctp.Property      = null;
                     ctp.ContentTypeId = dbContentType.Id;
                 }
             }
             dbContentType.CreatedDate = dbContentType.LastModifiedDate = DateTime.Now;
             var result = context.ContentType.Add(dbContentType).Entity;
             context.SaveChanges();
             return(Mapper.Map <ContentType>(result));
         }
     }
     catch (Exception ex)
     {
         _logger.LogError("Error occured while creating ContentType", ex);
     }
     return(null);
 }
예제 #9
0
        private void UpdatePageTreeTree(DeviserDbContext context, Entities.Page page)
        {
            if (page != null && page.ChildPage != null)
            {
                context.Page.Update(page);
                context.SaveChanges();

                if (page.ChildPage.Count > 0)
                {
                    foreach (var child in page.ChildPage)
                    {
                        if (child != null && child.ChildPage != null)
                        {
                            if (child.ChildPage.Count > 0)
                            {
                                UpdatePageTreeTree(context, child);
                            }
                            else if (child.Id != Guid.Empty)
                            {
                                context.Page.Update(page);
                                context.SaveChanges();
                            }
                        }
                    }
                }
            }
        }
        public void InsertData(DbContextOptions <DeviserDbContext> dbOption)
        {
            using var context = new DeviserDbContext(dbOption);
            var dataSeeder = new DataSeeder(context);

            dataSeeder.InsertData();
        }
예제 #11
0
        private Entities.Page GetDeletedPage(Guid id)
        {
            using var context = new DeviserDbContext(_dbOptions);
            var page = context.Page.First(p => p.Id == id && !p.IsActive);

            return(page);
        }
예제 #12
0
        public User GetUser(Guid userId)
        {
            try
            {
                using (var context = new DeviserDbContext(DbOptions))
                {
                    var result = context.Users
                                 .Where(e => e.Id == userId)
                                 .Include(u => u.UserRoles)
                                 .FirstOrDefault();
                    var returnResult = Mapper.Map <User>(result);

                    if (result.UserRoles != null && result.UserRoles.Count > 0)
                    {
                        returnResult.Roles = new List <Role>();
                        foreach (var userRole in result.UserRoles)
                        {
                            if (userRole != null)
                            {
                                var role = context.Roles.FirstOrDefault(e => e.Id == userRole.RoleId);
                                returnResult.Roles.Add(Mapper.Map <Role>(role));
                            }
                        }
                    }

                    return(returnResult);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error occured while calling GetUser", ex);
            }
            return(null);
        }
        ///// <summary>
        ///// Get page contents by container
        ///// </summary>
        ///// <param name="containerId"></param>
        ///// <returns></returns>
        //public List<PageContent> GetByContainer(Guid containerId)
        //{
        //    try
        //    {
        //        using (var context = new DeviserDBContext(dbOptions))
        //        {
        //            IEnumerable<PageContent> returnData = context.PageContent
        //                       .AsNoTracking()
        //                       .Where(e => e.ContainerId == containerId && !e.IsDeleted)
        //                       .ToList();
        //            return new List<PageContent>(returnData);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        logger.LogError("Error occured while getting GetByContainer", ex);
        //    }
        //    return null;
        //}

        /// <summary>
        /// Get all page contents for given pageId and cultureCode.
        /// Including PageContentTranslation, ContentType, ContentDataType, ContentTypeProperties, Property
        /// </summary>
        /// <param name="pageId"></param>
        /// <param name="cultureCode"></param>
        /// <returns></returns>
        public List <PageContent> Get(Guid pageId, string cultureCode)
        {
            try
            {
                using (var context = new DeviserDbContext(DbOptions))
                {
                    var result = context.PageContent
                                 .Include(pc => pc.PageContentTranslation)
                                 .Include(pc => pc.ContentType)
                                 .Include(pc => pc.ContentType).ThenInclude(pc => pc.ContentTypeProperties).ThenInclude(ctp => ctp.Property)
                                 .Include(pc => pc.ContentPermissions)
                                 .Where(e => e.PageId == pageId && !e.IsDeleted)
                                 .ToList();

                    foreach (var pageContent in result)
                    {
                        if (pageContent.PageContentTranslation != null)
                        {
                            pageContent.PageContentTranslation = pageContent.PageContentTranslation.Where(t => t.CultureCode == cultureCode).ToList();
                        }
                    }

                    return(Mapper.Map <List <PageContent> >(result));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error occured while getting Get", ex);
            }
            return(null);
        }
        public IDictionary <string, string> GetSettingsAsDictionary()
        {
            using var context = new DeviserDbContext(_dbOptions);
            var result = context.SiteSetting.ToDictionary(s => s.SettingName, v => v.SettingValue);

            return(result);
        }
        //Custom Field Declaration
        public List <User> GetUsers()
        {
            using var context = new DeviserDbContext(_dbOptions);
            var dbUsers = context.Users
                          .Include(u => u.UserRoles)
                          .ToList();
            var result = _mapper.Map <List <User> >(dbUsers);

            foreach (var user in dbUsers)
            {
                if (user.UserRoles == null || user.UserRoles.Count <= 0)
                {
                    continue;
                }

                var targetUser = result.First(u => u.Id == user.Id);
                targetUser.Roles = new List <Role>();
                foreach (var userRole in user.UserRoles)
                {
                    if (userRole == null)
                    {
                        continue;
                    }

                    var role = context.Roles.FirstOrDefault(e => e.Id == userRole.RoleId);
                    targetUser.Roles.Add(_mapper.Map <Role>(role));
                }
            }
            return(result);
        }
예제 #16
0
        public bool DeletePageModule(Guid id)
        {
            try
            {
                using (var context = new DeviserDbContext(DbOptions))
                {
                    var dbpageModule = GetDeletedPageModule(id);

                    if (dbpageModule != null)
                    {
                        context.PageModule.Remove(dbpageModule);
                        var pageModulePermissions = context.ModulePermission
                                                    .Where(p => p.PageModuleId == id)
                                                    .ToList();
                        context.ModulePermission.RemoveRange(pageModulePermissions);
                        context.SaveChanges();
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error occured while deleting page module", ex);
            }

            return(false);
        }
예제 #17
0
 public void UpdatePageModules(List <PageModule> pageModules)
 {
     try
     {
         using (var context = new DeviserDbContext(DbOptions))
         {
             var dbPageModules = Mapper.Map <List <Entities.PageModule> >(pageModules);
             foreach (var pageModule in dbPageModules)
             {
                 if (context.PageModule.Any(pm => pm.Id == pageModule.Id))
                 {
                     //page module exist, therefore update it
                     context.PageModule.Update(pageModule);
                     //UpdateModulePermission(pageModule, context); //Here, intensions is mostly to update the container. Moreover, permissions might not be included in each page module object.
                 }
                 else
                 {
                     context.PageModule.Add(pageModule);
                 }
             }
             context.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         _logger.LogError("Error occured while calling UpdatePageModules", ex);
         throw;
     }
 }
        public bool IsPropertyExist(string propertyName)
        {
            using var context = new DeviserDbContext(_dbOptions);
            var result = context.Property.Count(e => e.Name == propertyName);

            return(result > 0);
        }
        public List <OptionList> GetOptionLists()
        {
            using var context = new DeviserDbContext(_dbOptions);
            var result = context.OptionList.ToList();

            return(_mapper.Map <List <OptionList> >(result));
        }
예제 #20
0
        public void InstallPlatform(InstallModel installModel)
        {
            string connectionString = GetConnectionString(installModel);
            string settingFile      = Path.Combine(_hostingEnvironment.ContentRootPath, $"appsettings.{_hostingEnvironment.EnvironmentName}.json");
            DbContextOptionsBuilder dbContextOptionsBuilder = GetDbContextOptionsBuilder(installModel);
            DbContextOptions        dbOption = dbContextOptionsBuilder.Options;

            _isInstallInProgress = true;
            _installModel        = installModel;
            if (!IsDatabaseExistsFor(connectionString))
            {
                //Creating database
                using (var context = new DeviserDbContext(dbOption))
                {
                    context.Database.Migrate();
                }

                //Insert data
                InsertData(dbOption);

                //Migrate module
                MigrateModuleContexts(installModel);


                //Create user account
                _userManager = _serviceProvider.GetService <UserManager <Entities.User> >();

                var user = new Entities.User {
                    UserName = installModel.AdminEmail, Email = installModel.AdminEmail
                };
                var result = _userManager.CreateAsync(user, installModel.AdminPassword).GetAwaiter().GetResult();
                if (result.Succeeded)
                {
                    //Assign user to admin role
                    _userManager.AddToRoleAsync(user, "Administrators");
                }
            }

            //Write intall settings
            WriteInstallSettings(installModel);

            //Update connection string
            //Writing to appsettings.json file
            string  json    = File.ReadAllText(settingFile);
            JObject jsonObj = JObject.Parse(json);

            jsonObj["ConnectionStrings"]["DefaultConnection"] = connectionString;
            string output = JsonConvert.SerializeObject(jsonObj, Formatting.Indented);

            File.WriteAllText(settingFile, output);

            //Updating it in cache
            //_configuration["ConnectionStrings:DefaultConnection"] = connectionString;

            //Success no exceptions were thrown
            _dbContextOptions        = dbOption;
            _dbContextOptionsBuilder = dbContextOptionsBuilder;
            _isInstallInProgress     = false;
        }
예제 #21
0
        /// <summary>
        /// Get page content translations for given translationId
        /// </summary>
        /// <param name="pageContentId"></param>
        /// <returns></returns>
        public PageContentTranslation GetTranslation(Guid pageContentId)
        {
            using var context = new DeviserDbContext(_dbOptions);
            var result = context.PageContentTranslation
                         .FirstOrDefault(t => t.Id == pageContentId && t.IsActive);

            return(_mapper.Map <PageContentTranslation>(result));
        }
        public List <SiteSetting> GetSettings()
        {
            using var context = new DeviserDbContext(_dbOptions);
            var dbResult = context.SiteSetting.ToList();
            var result   = _mapper.Map <List <SiteSetting> >(dbResult);

            return(result);
        }
        public OptionList GetOptionList(Guid optionListId)
        {
            using var context = new DeviserDbContext(_dbOptions);
            var result = context.OptionList
                         .FirstOrDefault(e => e.Id == optionListId);

            return(_mapper.Map <OptionList>(result));
        }
        public OptionList GetOptionList(string listName)
        {
            using var context = new DeviserDbContext(_dbOptions);
            var result = context.OptionList
                         .FirstOrDefault(e => e.Name.ToLower() == listName.ToLower());

            return(_mapper.Map <OptionList>(result));
        }
        public Role GetRoleByName(string roleName)
        {
            using var context = new DeviserDbContext(_dbOptions);
            var result = context.Roles
                         .FirstOrDefault(e => e.Name == roleName);

            return(_mapper.Map <Role>(result));
        }
        public Role GetRole(Guid roleId)
        {
            using var context = new DeviserDbContext(_dbOptions);
            var result = context.Roles
                         .FirstOrDefault(e => e.Id == roleId);

            return(_mapper.Map <Role>(result));
        }
예제 #27
0
        public ModuleView GetModuleView(Guid moduleViewId)
        {
            using var context = new DeviserDbContext(_dbOptions);
            var result = context.ModuleView
                         .FirstOrDefault(m => m.Id == moduleViewId);

            return(_mapper.Map <ModuleView>(result));
        }
예제 #28
0
 public void InsertData(DbContextOptions dbOption)
 {
     using (var context = new DeviserDbContext(dbOption))
     {
         var dataSeeder = new DataSeeder(context, _hostingEnvironment);
         dataSeeder.InsertData();
     }
 }
예제 #29
0
        public Layout GetLayout(Guid layoutId)
        {
            using var context = new DeviserDbContext(_dbOptions);
            var result = context.Layout
                         .FirstOrDefault(e => e.Id == layoutId);

            return(_mapper.Map <Layout>(result));
        }
예제 #30
0
        public Page GetPageAndDependencies(Guid pageId, bool includeChild = true)
        {
            try
            {
                //var cacheName = $"{nameof(GetPageAndDependencies)}_{pageId}";
                //var result = GetResultFromCache<Page>(cacheName);
                //if (result != null)
                //{
                //    return result;
                //}

                using (var context = new DeviserDbContext(DbOptions))
                {
                    Entities.Page dbResult;
                    if (includeChild)
                    {
                        dbResult = context.Page
                                   .Where(e => e.Id == pageId).AsNoTracking()
                                   .Include(p => p.PageTranslation)
                                   .Include(p => p.PagePermissions)
                                   .Include(p => p.Layout)
                                   .Include(p => p.PageContent).ThenInclude(pc => pc.PageContentTranslation)
                                   .Include(p => p.PageContent).ThenInclude(pc => pc.ContentType)
                                   .Include(p => p.PageContent).ThenInclude(pc => pc.ContentType).ThenInclude(ct => ct.ContentTypeProperties).ThenInclude(ctp => ctp.Property).ThenInclude(p => p.OptionList)
                                   .Include(p => p.PageContent).ThenInclude(pc => pc.ContentPermissions)
                                   .Include(p => p.PageModule).ThenInclude(pm => pm.Module)
                                   .Include(p => p.PageModule).ThenInclude(pm => pm.ModulePermissions)
                                   .OrderBy(p => p.Id)
                                   .FirstOrDefault();
                    }
                    else
                    {
                        dbResult = context.Page.Where(e => e.Id == pageId).AsNoTracking().FirstOrDefault();
                    }



                    if (dbResult.PageModule != null)
                    {
                        dbResult.PageModule = dbResult.PageModule.Where(pm => !pm.IsDeleted).ToList();
                    }

                    if (dbResult.PageContent != null)
                    {
                        dbResult.PageContent = dbResult.PageContent.Where(pc => !pc.IsDeleted).ToList();
                    }

                    var result = Mapper.Map <Page>(dbResult);
                    //AddResultToCache(cacheName, result);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error occured while calling GetPageAndDependencies", ex);
            }
            return(null);
        }