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(); } } } } } }
/// <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)); }
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); } }
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); }
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); }
/// <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; } }
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; } }
/// <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); }
public ModuleView CreateModuleView(ModuleView moduleView) { using var context = new DeviserDbContext(_dbOptions); var dbModuleView = _mapper.Map <Entities.ModuleView>(moduleView); var result = context.ModuleView.Add(dbModuleView).Entity; context.SaveChanges(); return(_mapper.Map <ModuleView>(result)); }
public Layout CreateLayout(Layout layout) { using var context = new DeviserDbContext(_dbOptions); var dbLayout = _mapper.Map <Entities.Layout>(layout); var result = context.Layout.Add(dbLayout).Entity; context.SaveChanges(); return(_mapper.Map <Layout>(result)); }
public LayoutType UpdateLayoutType(LayoutType layoutType) { try { using (var context = new DeviserDbContext(DbOptions)) { var dbLayoutType = Mapper.Map <Entities.LayoutType>(layoutType); if (dbLayoutType.LayoutTypeProperties != null && dbLayoutType.LayoutTypeProperties.Count > 0) { var toRemoveFromClient = dbLayoutType.LayoutTypeProperties.Where(clientProp => context.LayoutTypeProperty.Any(dbProp => clientProp.LayoutTypeId == dbProp.LayoutTypeId && clientProp.PropertyId == dbProp.PropertyId)).ToList(); var layoutTypeProperties = context.LayoutTypeProperty.Where(ctp => ctp.LayoutTypeId == dbLayoutType.Id).ToList(); //List<Entities.LayoutTypeProperty> newLayoutTypeProperities = dbLayoutType.LayoutTypeProperties.Where(newProp => context.LayoutTypeProperty.Any(ctp => ctp.LayoutTypeId == newProp.LayoutTypeId && ctp.PropertyId != newProp.PropertyId)).ToList(); List <Entities.LayoutTypeProperty> toRemoveFromDb = null; if (layoutTypeProperties != null && layoutTypeProperties.Count > 0) { toRemoveFromDb = layoutTypeProperties.Where(dbProp => !dbLayoutType.LayoutTypeProperties.Any(clientProp => dbProp.PropertyId == clientProp.PropertyId)).ToList(); } if (toRemoveFromClient != null && toRemoveFromClient.Count > 0) { foreach (var layoutTypeProp in toRemoveFromClient) { //ContentTypeProperty exist in db, therefore remove it from contentType (client source) dbLayoutType.LayoutTypeProperties.Remove(layoutTypeProp); } } if (toRemoveFromDb != null && toRemoveFromDb.Count > 0) { //ContentTypeProperty is not exist in contentType (client source), because client has been removed it. Therefor, remove it from db. context.LayoutTypeProperty.RemoveRange(toRemoveFromDb); } if (dbLayoutType.LayoutTypeProperties != null && dbLayoutType.LayoutTypeProperties.Count > 0) { context.LayoutTypeProperty.AddRange(dbLayoutType.LayoutTypeProperties); } } dbLayoutType.LastModifiedDate = DateTime.Now; var result = context.LayoutType.Update(dbLayoutType).Entity; context.SaveChanges(); return(Mapper.Map <LayoutType>(result)); } } catch (Exception ex) { _logger.LogError("Error occured while updating LayoutType", ex); } return(null); }
public PageModule UpdateModulePermission(PageModule pageModule) { if (pageModule.ModulePermissions == null || pageModule.ModulePermissions.Count <= 0) { throw new InvalidOperationException("PageModule ModulePermissions cannot be null or empty"); } var dbPageModule = _mapper.Map <Entities.PageModule>(pageModule); //Assuming all permissions have same pageModuleId var pageModuleId = dbPageModule.Id; var modulePermissions = dbPageModule.ModulePermissions; using var context = new DeviserDbContext(_dbOptions); //Update InheritViewPermissions only var dbPageContent = context.PageModule.First(pc => pc.Id == pageModuleId); dbPageContent.InheritViewPermissions = dbPageModule.InheritViewPermissions; dbPageContent.InheritEditPermissions = dbPageModule.InheritEditPermissions; //Filter deleted permissions in UI and delete all of them var toDelete = context.ModulePermission .Where(dbPermission => dbPermission.PageModuleId == pageModuleId) .ToList() .Where(dbPermission => !modulePermissions.Any(modulePermission => modulePermission.PermissionId == dbPermission.PermissionId && modulePermission.RoleId == dbPermission.RoleId)) .ToList(); if (toDelete.Count > 0) { context.ModulePermission.RemoveRange(toDelete); } //Filter new permissions which are not in db and add all of them var toAdd = modulePermissions.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(GetPageModule(pageModule.Id)); }
public Property UpdateProperty(Property property) { using var context = new DeviserDbContext(_dbOptions); var dbProperty = _mapper.Map <Entities.Property>(property); //property.LastModifiedDate = DateTime.Now; var result = context.Property.Update(dbProperty).Entity; context.SaveChanges(); return(_mapper.Map <Property>(result)); }
public bool DeleteLayout(Guid layoutId) { using var context = new DeviserDbContext(_dbOptions); var layout = GetLayout(layoutId); var dbLayout = _mapper.Map <Entities.Layout>(layout); context.Layout.Remove(dbLayout); context.SaveChanges(); return(true); }
public List <SiteSetting> UpdateSetting(List <SiteSetting> settings) { using var context = new DeviserDbContext(_dbOptions); var dbSettings = _mapper.Map <List <Entities.SiteSetting> >(settings); context.SiteSetting.UpdateRange(dbSettings); context.SaveChanges(); var result = context.SiteSetting.ToList(); return(_mapper.Map <List <SiteSetting> >(result)); }
/// <summary> /// Creates page content /// </summary> /// <param name="pageContent"></param> /// <returns></returns> public PageContent Create(PageContent pageContent) { using var context = new DeviserDbContext(_dbOptions); var dbPageContent = _mapper.Map <Entities.PageContent>(pageContent); dbPageContent.LastModifiedDate = dbPageContent.CreatedDate = DateTime.Now; var result = context.PageContent.Add(dbPageContent).Entity; context.SaveChanges(); return(_mapper.Map <PageContent>(result)); }
public void UpdateModulePermission(PageModule pageModule) { if (pageModule.ModulePermissions != null && pageModule.ModulePermissions.Count > 0) { var dbPageModule = Mapper.Map <Entities.PageModule>(pageModule); //Assuming all permissions have same pageModuleId var pageModuleId = dbPageModule.Id; var modulePermissions = dbPageModule.ModulePermissions; try { using (var context = new DeviserDbContext(DbOptions)) { //Update InheritViewPermissions only var dbPageContent = context.PageModule.First(pc => pc.Id == pageModuleId); dbPageContent.InheritViewPermissions = dbPageModule.InheritViewPermissions; dbPageContent.InheritEditPermissions = dbPageModule.InheritEditPermissions; //Filter deleted permissions in UI and delete all of them var toDelete = context.ModulePermission.Where(dbPermission => dbPermission.PageModuleId == pageModuleId && !modulePermissions.Any(modulePermission => modulePermission.PermissionId == dbPermission.PermissionId && modulePermission.RoleId == dbPermission.RoleId)).ToList(); if (toDelete != null && toDelete.Count > 0) { context.ModulePermission.RemoveRange(toDelete); } //Filter new permissions which are not in db and add all of them var toAdd = modulePermissions.Where(modulePermission => !context.ModulePermission.Any(dbPermission => dbPermission.PermissionId == modulePermission.PermissionId && dbPermission.PageModuleId == modulePermission.PageModuleId && dbPermission.RoleId == modulePermission.RoleId)).ToList(); if (toAdd != null && 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(); } } catch (Exception ex) { _logger.LogError("Error occured while updating module persmissions", ex); throw; } } }
public PageContent SoftDeletePageContent(Guid id) { using var context = new DeviserDbContext(_dbOptions); var dbPageContent = GetPageContent(context, id); dbPageContent.IsActive = false; var result = context.PageContent.Update(dbPageContent).Entity; context.SaveChanges(); return(_mapper.Map <PageContent>(result)); }
public LayoutType CreateLayoutType(LayoutType layoutType) { using var context = new DeviserDbContext(_dbOptions); var dbLayoutType = _mapper.Map <Entities.LayoutType>(layoutType); dbLayoutType.CreatedDate = dbLayoutType.LastModifiedDate = DateTime.Now; var result = context.LayoutType.Add(dbLayoutType).Entity; context.SaveChanges(); return(GetLayoutType(result.Id)); }
public Module Create(Module module) { using var context = new DeviserDbContext(_dbOptions); var dbModule = _mapper.Map <Entities.Module>(module); dbModule.CreatedDate = dbModule.LastModifiedDate = DateTime.Now; var result = context.Module.Add(dbModule).Entity; context.SaveChanges(); return(_mapper.Map <Module>(result)); }
/// <summary> /// Create page translation /// </summary> /// <param name="contentTranslation"></param> /// <returns></returns> public PageContentTranslation CreateTranslation(PageContentTranslation contentTranslation) { using var context = new DeviserDbContext(_dbOptions); var dbPageContentTranslation = _mapper.Map <Entities.PageContentTranslation>(contentTranslation); dbPageContentTranslation.CreatedDate = dbPageContentTranslation.LastModifiedDate = DateTime.Now; var result = context.PageContentTranslation.Add(dbPageContentTranslation).Entity; context.SaveChanges(); return(_mapper.Map <PageContentTranslation>(result)); }
public OptionList UpdateOptionList(OptionList optionList) { using var context = new DeviserDbContext(_dbOptions); var dbOptionList = _mapper.Map <Entities.OptionList>(optionList); dbOptionList.LastModifiedDate = DateTime.Now; var result = context.OptionList.Update(dbOptionList).Entity; context.SaveChanges(); return(_mapper.Map <OptionList>(result)); }
public void AddOrUpdatePageModules(IList <PageModule> pageModules) { using var context = new DeviserDbContext(_dbOptions); var dbPageModules = _mapper.Map <IList <Entities.PageModule> >(pageModules); var pageModuleIds = context.PageModule.AsNoTracking().Select(pc => pc.Id).ToHashSet(); foreach (var pageModule in dbPageModules) { //if (context.PageModule.Any(pm => pm.Id == pageModule.Id)) if (pageModuleIds.Contains(pageModule.Id)) { var dbPageModule = context.PageModule.First(pc => pc.Id == pageModule.Id); dbPageModule.ContainerId = pageModule.ContainerId; dbPageModule.SortOrder = pageModule.SortOrder; } else { context.PageModule.Add(new Entities.PageModule() { Id = pageModule.Id, PageId = pageModule.PageId, ContainerId = pageModule.ContainerId, ModuleId = pageModule.ModuleId, ModuleViewId = pageModule.ModuleViewId, SortOrder = pageModule.SortOrder, IsActive = true, InheritEditPermissions = true, InheritViewPermissions = true }); var adminPermissions = new List <Entities.ModulePermission>() { new Entities.ModulePermission() { PageModuleId = pageModule.Id, RoleId = Globals.AdministratorRoleId, PermissionId = Globals.ModuleViewPermissionId, }, new Entities.ModulePermission() { PageModuleId = pageModule.Id, RoleId = Globals.AdministratorRoleId, PermissionId = Globals.ModuleEditPermissionId, } }; context.ModulePermission.AddRange(adminPermissions); } } context.SaveChanges(); //Refresh cache GetPageModules(true); }
/// <summary> /// It updates only SortOrder and ContainerId of provided list of page contents. If a page content does not exist in db, it adds it. /// </summary> /// <param name="pageContents"> /// List of page contents /// </param> public void AddOrUpdate(List <PageContent> pageContents) { var dbPageContents = _mapper.Map <List <Entities.PageContent> >(pageContents); using var context = new DeviserDbContext(_dbOptions); var pageContentIds = context.PageContent.AsNoTracking().Select(pc => pc.Id).ToHashSet(); foreach (var content in dbPageContents) { content.LastModifiedDate = DateTime.Now; //if (context.PageContent.AsNoTracking().Any(pc => pc.Id == content.Id)) if (pageContentIds.Contains(content.Id)) { //content exist, therefore update the content var pageContent = context.PageContent.First(pc => pc.Id == content.Id); pageContent.ContainerId = content.ContainerId; pageContent.SortOrder = content.SortOrder; } else { context.PageContent.Add(new Entities.PageContent() { Id = content.Id, PageId = content.PageId, ContainerId = content.ContainerId, ContentTypeId = content.ContentTypeId, SortOrder = content.SortOrder, IsActive = true, InheritEditPermissions = true, InheritViewPermissions = true }); var adminPermissions = new List <Entities.ContentPermission>() { new Entities.ContentPermission() { PageContentId = content.Id, RoleId = Globals.AdministratorRoleId, PermissionId = Globals.ContentViewPermissionId, }, new Entities.ContentPermission() { PageContentId = content.Id, RoleId = Globals.AdministratorRoleId, PermissionId = Globals.ContentEditPermissionId, } }; AddContentPermissions(context, adminPermissions); } } context.SaveChanges(); }
public bool DraftPage(Guid id) { using var context = new DeviserDbContext(_dbOptions); var permission = context.PagePermission.FirstOrDefault(p => p.PageId == id && p.RoleId == Globals.AllUsersRoleId); if (permission != null) { context.PagePermission.Remove(permission); } context.SaveChanges(); return(true); }
public PageModule UpdatePageModule(PageModule pageModule) { using var context = new DeviserDbContext(_dbOptions); var dbPageModule = _mapper.Map <Entities.PageModule>(pageModule); var result = context.PageModule.Update(dbPageModule).Entity; context.SaveChanges(); //Refresh cache GetPageModules(true); return(_mapper.Map <PageModule>(result)); }
/// <summary> /// Updates following properties of the Page only: /// 1. Page.IsCurrentPage /// 2. Page.LayoutId /// </summary> /// <param name="page"></param> /// <returns></returns> public Page UpdatePageActiveAndLayout(Page page) { using var context = new DeviserDbContext(_dbOptions); var dbPage = context.Page.First(p => p.Id == page.Id); dbPage.LayoutId = page.LayoutId; dbPage.IsActive = page.IsActive; var result = context.Page.Update(dbPage).Entity; context.SaveChanges(); //Refresh cache GetPagesFlat(true); return(_mapper.Map <Page>(result)); }
public Language CreateLanguage(Language language) { using var context = new DeviserDbContext(_dbOptions); var dbLanguage = _mapper.Map <Entities.Language>(language); dbLanguage.CreatedDate = dbLanguage.LastModifiedDate = DateTime.Now; dbLanguage.IsActive = true; var result = context.Language.Add(dbLanguage).Entity; context.SaveChanges(); //Refresh Language Cache GetLanguages(true); return(_mapper.Map <Language>(result)); }
/// <summary> /// Add content permissions /// </summary> /// <param name="contentPermissions"></param> /// <returns></returns> public List <ContentPermission> AddContentPermissions(List <ContentPermission> contentPermissions) { if (contentPermissions == null || contentPermissions.Count <= 0) { throw new InvalidOperationException($"Invalid parameter {nameof(contentPermissions)}"); } using var context = new DeviserDbContext(_dbOptions); var dbContentPermissions = _mapper.Map <List <Entities.ContentPermission> >(contentPermissions); //Filter new permissions which are not in db and add all of them AddContentPermissions(context, dbContentPermissions); context.SaveChanges(); return(_mapper.Map <List <ContentPermission> >(contentPermissions)); }
public PageContentTranslation UpdateTranslation(PageContentTranslation pageContentTranslation) { using var context = new DeviserDbContext(_dbOptions); var dbPageContentTranslation = _mapper.Map <Entities.PageContentTranslation>(pageContentTranslation); if (!context.PageContentTranslation.Any(t => t.Id == dbPageContentTranslation.Id)) { throw new InvalidOperationException($"Parameter {nameof(pageContentTranslation)} is not found"); } dbPageContentTranslation.LastModifiedDate = DateTime.Now; var result = context.PageContentTranslation.Update(dbPageContentTranslation).Entity; context.SaveChanges(); return(_mapper.Map <PageContentTranslation>(result)); }