public async Task <IActionResult> GetMenuSections(Guid menuId) { PaginatedEntitiesResult <SidebarMenuSectionItemViewModel> entitiesResult = await this.adminMenuService.GetAllMenuSectionsAsync <SidebarMenuSectionItemViewModel>(menuId); AllEntitiesViewModel model = new AllEntitiesViewModel(); model.SingleEntityName = "Sidebar Section"; model.Title = "Menu Sidebar Sections"; this.ViewData[BreadcrumbPageTitlePlaceholder] = model.Title; List <TableRowActionViewModel> actions = new List <TableRowActionViewModel>(); actions.Add(TableMapper.CreateAction("Link Items", MaterialDesignIcons.ViewList, Color.DarkSlateBlue, TableRowActionMethod.Get, $"/admin/system/navigation-menus/sidebar-menu-link-items/sections/{{0}}/link-items", "[Id]")); actions.Add(TableMapper.DetailsAction($"/admin/system/navigation-menus/sidebar-menu-section-items/{{0}}", "[Id]")); actions.Add(TableMapper.EditAction($"/admin/system/navigation-menus/sidebar-menu-section-items/{{0}}/edit", "[Id]")); actions.Add(TableMapper.DeleteAction($"/admin/system/navigation-menus/sidebar-menu-section-items/{{0}}/delete", "[Id]")); model.Table = TableMapper.DtoMapper <SidebarMenuSectionItemViewModel>(entitiesResult, actions.ToArray()); model.Table.SetPaginationRedirection("Admin", this.GetType().Name.Replace("Controller", string.Empty), nameof(this.GetAll)); model.NavigationActions.Add(new NavigationActionViewModel { Name = $"Create {model.SingleEntityName}", ActionUrl = $"/admin/system/navigation-menus/sidebar-menu-section-items/create?SchemeId={menuId}", Icon = MaterialDesignIcons.Plus, Method = HttpMethod.Get, }); return(this.View("AbstractViews/GetAll", model)); }
public async Task <PaginatedEntitiesResult <TEntityDto> > FilterEntitiesAsync <TEntity, TEntityDto>(FilterQueryRequest filterQuery) where TEntity : class, IEntityBase, new() { PaginatedEntitiesResult <TEntityDto> result = new PaginatedEntitiesResult <TEntityDto>(); try { Expression <Func <TEntity, bool> > searchQueryExpression = null; if (filterQuery.FilterQueryStringItems != null && filterQuery.FilterQueryStringItems.Count > 0) { searchQueryExpression = GetEntitySearchQueryExpressionByFilterQueryRequest <TEntity>(filterQuery); } else if (!string.IsNullOrEmpty(filterQuery.SearchQuery)) { searchQueryExpression = GetEntitySearchQueryExpression <TEntity>(filterQuery.SearchQuery); } var orderExpression = GetOrderExpressionByFilterQueryRequest <TEntity>(filterQuery); var firstLevelIncludeQuery = GetFirstLevelIncludeQuery <TEntity>(); if (searchQueryExpression != null) { result.Count = (await standardRepository.GetAllAsync <TEntity>()).Count(); } else { result.Count = (await standardRepository.QueryAsync <TEntity>(searchQueryExpression)).Count(); } result.CurrentPage = filterQuery.Page.HasValue ? filterQuery.Page.Value : 1; if (filterQuery.Page.HasValue) { result.PageSize = filterQuery.PageSize.HasValue ? filterQuery.PageSize.Value : PaginationPageSize; } else { result.PageSize = result.Count; } IEnumerable <TEntity> entities = null; if (searchQueryExpression != null) { entities = await this.standardRepository.QueryPageAsync <TEntity>(result.StartRow, result.PageSize, searchQueryExpression, orderExpression, firstLevelIncludeQuery); } else { entities = await this.standardRepository.GetPageAsync <TEntity>(result.StartRow, result.PageSize, orderExpression, firstLevelIncludeQuery); } result.Entities = this.mapper.Map <IEnumerable <TEntityDto> >(entities); } catch (Exception ex) { await LogErrorAsync(ex, nameof(FilterEntitiesAsync)); } return(result); }
public async Task <PaginatedEntitiesResult <TEntityDto> > GetAllEntitiesPaginatedAsync <TEntity, TEntityDto>( int page, string searchQuery = null, string sortProperty = null) where TEntity : class, IEntityBase, new() { PaginatedEntitiesResult <TEntityDto> result = new PaginatedEntitiesResult <TEntityDto>(); try { if (!string.IsNullOrEmpty(searchQuery)) { searchQuery = searchQuery.Trim(); } var standardRepository = this.Uow.GetStandardRepository(); if (string.IsNullOrWhiteSpace(searchQuery)) { result.Count = await standardRepository.CountAsync <TEntity>(null); } else { var searchQueryExpression = this.GetEntitySearchQueryExpression <TEntity>(searchQuery); result.Count = standardRepository.EnumerableCount <TEntity>(searchQueryExpression.Compile()); } result.CurrentPage = page; result.PageSize = this.PaginationPageSize; var orderByExpression = this.GetOrderExpressionByFilterQueryRequest <TEntity, TEntityDto>(sortProperty); IEnumerable <TEntity> entities = null; var firstLevelIncludeQuery = this.GetFirstLevelIncludeQuery <TEntity>(); if (string.IsNullOrWhiteSpace(searchQuery)) { entities = await standardRepository.GetPageAsync <TEntity>(result.StartRow, this.PaginationPageSize, orderByExpression, firstLevelIncludeQuery); } else { entities = await standardRepository.QueryPageAsync <TEntity>(result.StartRow, this.PaginationPageSize, this.GetEntitySearchQueryExpression <TEntity>(searchQuery), orderByExpression, firstLevelIncludeQuery); } var dtoEntities = this.Mapper.Map <IEnumerable <TEntityDto> >(entities); result.Entities = dtoEntities; } catch (Exception ex) { await this.LogErrorAsync(ex, nameof(this.GetAllEntitiesPaginatedAsync)); } return(result); }
public async Task <PaginatedEntitiesResult <TEntityDto> > GetAllEntitiesPaginatedAsync <TEntity, TEntityDto>(int page, string searchQuery = null, bool showDeleted = false) where TEntity : class, IEntityBase, new() { PaginatedEntitiesResult <TEntityDto> result = new PaginatedEntitiesResult <TEntityDto>(); try { if (!string.IsNullOrEmpty(searchQuery)) { searchQuery = searchQuery.Trim(); } var standardRepository = this.Uow.GetStandardRepository(); if (string.IsNullOrWhiteSpace(searchQuery)) { result.Count = await standardRepository.CountAsync <TEntity>(x => x.Deleted == showDeleted); } else { var searchQueryExpression = this.GetEntitySearchQueryExpression <TEntity>(searchQuery, showDeleted); result.Count = await standardRepository.CountAsync <TEntity>(searchQueryExpression); } result.CurrentPage = page; result.PageSize = PaginationPageSize; IEnumerable <TEntity> entities = null; var firstLevelIncludeQuery = this.GetFirstLevelIncludeQuery <TEntity>(); if (string.IsNullOrWhiteSpace(searchQuery)) { entities = await standardRepository.GetPageAsync <TEntity>(result.StartRow, PaginationPageSize, null, firstLevelIncludeQuery, showDeleted); } else { entities = await standardRepository.QueryPageAsync <TEntity>(result.StartRow, PaginationPageSize, this.GetEntitySearchQueryExpression <TEntity>(searchQuery, showDeleted), null, firstLevelIncludeQuery); } var dtoEntities = this.Mapper.Map <IEnumerable <TEntityDto> >(entities); result.Entities = dtoEntities; } catch (Exception ex) { await this.LogErrorAsync(ex, nameof(this.GetAllEntitiesPaginatedAsync)); } return(result); }
public virtual async Task <IActionResult> GetAll([FromQuery(Name = "p")] int page = 1, [FromQuery(Name = "q")] string query = null) { PaginatedEntitiesResult <TEntityDto> entitiesResult = await this.entityManager.GetAllEntitiesPaginatedAsync <TEntity, TEntityDto>(page, query); AllEntitiesViewModel model = new AllEntitiesViewModel(); model.SingleEntityName = StringFunctions.SplitWordsByCapitalLetters(typeof(TEntity).Name); model.Title = typeof(TEntity).Name.ToLower().EndsWith("s") ? $"{model.SingleEntityName}es" : $"{model.SingleEntityName}s"; ViewData[BreadcrumbPageTitlePlaceholder] = model.Title; List <TableRowActionViewModel> actions = new List <TableRowActionViewModel>(); TableViewActionsInit(ref actions); model.Table = TableMapper.DtoMapper <TEntityDto>(entitiesResult, actions.ToArray()); model.Table.SetPaginationRedirection("Admin", this.GetType().Name.Replace("Controller", string.Empty), nameof(GetAll)); ViewData.Add("searchQuery", query); InitNavigationActionsIntoListPage(ref model); return(View("AbstractViews/GetAll", model)); }
public async Task <PaginatedEntitiesResult <TLinkItemModel> > GetAllLinkItemsAsync <TLinkItemModel>(Guid sectionId) { try { var linkItems = await this.StandardRepository.QueryAsync <SidebarNavigationLinkItem>( x => x.ParentSectionId == sectionId, x => x.OrderBy(y => y.Order)); var linkItemDtos = this.Mapper.Map <IEnumerable <TLinkItemModel> >(linkItems); PaginatedEntitiesResult <TLinkItemModel> result = new PaginatedEntitiesResult <TLinkItemModel>(); result.Entities = linkItemDtos?.ToList(); result.CurrentPage = 1; result.Count = result.EntitiesCount; result.PageSize = result.EntitiesCount; return(result); } catch (Exception) { return(null); } }
public async Task <PaginatedEntitiesResult <TSectionModel> > GetAllMenuSectionsAsync <TSectionModel>(Guid menuId) { try { var sections = await this.StandardRepository.QueryAsync <SidebarMenuSectionItem>( x => x.AdminNavigationSchemeId == menuId, x => x.OrderBy(y => y.Order), x => x.Include(y => y.Children)); var sectionDtos = this.Mapper.Map <IEnumerable <TSectionModel> >(sections); PaginatedEntitiesResult <TSectionModel> result = new PaginatedEntitiesResult <TSectionModel>(); result.Entities = sectionDtos?.ToList(); result.CurrentPage = 1; result.Count = result.EntitiesCount; result.PageSize = result.EntitiesCount; return(result); } catch (Exception) { return(null); } }
public static TableViewModel DtoMapper <T>(PaginatedEntitiesResult <T> entitiesResult, params TableRowActionViewModel[] actions) { Type dtoType = typeof(T); TableViewModel tableViewModel = new TableViewModel(); var properties = dtoType.GetProperties(); List <TableCellAttribute> dtoAttributes = new List <TableCellAttribute>(); foreach (var property in properties) { if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0) { dtoAttributes.Add((TableCellAttribute)property.GetCustomAttributes(typeof(TableCellAttribute), false).FirstOrDefault()); } } List <string> tableHeaders = dtoAttributes.OrderBy(x => x.Order).Select(x => x.Name).Distinct().ToList(); tableHeaders.ForEach(x => { tableViewModel.Header.AddCell(x); }); if (entitiesResult.EntitiesCount > 0) { foreach (var entity in entitiesResult.Entities) { TableRowViewModel tableRow = new TableRowViewModel(); foreach (var property in properties) { if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0) { TableCellAttribute propertyAttribute = (TableCellAttribute)property.GetCustomAttributes(typeof(TableCellAttribute), false).FirstOrDefault(); tableRow.AddCell(propertyAttribute.Order, property.GetValue(entity), propertyAttribute.Type); } } foreach (var action in actions) { List <string> parsedParameters = new List <string>(); foreach (var parameter in action.RawParameters) { if (parameter.StartsWith("[", StringComparison.OrdinalIgnoreCase) && parameter.EndsWith("]", StringComparison.OrdinalIgnoreCase)) { string propertyName = parameter.Substring(1, parameter.Length - 2); string propertyValue = entity.GetType().GetProperty(propertyName).GetValue(entity).ToString(); parsedParameters.Add(propertyValue); } else { parsedParameters.Add(parameter); } } tableRow.AddAction(action, parsedParameters); } tableViewModel.AddRow(tableRow); } } tableViewModel.SetPagination(entitiesResult.CurrentPage, entitiesResult.Pages); return(tableViewModel); }
public static TableViewModel DtoMapper <T>(PaginatedEntitiesResult <T> entitiesResult, params TableRowActionViewModel[] actions) { Type dtoType = typeof(T); TableViewModel tableViewModel = new TableViewModel(); var properties = dtoType.GetProperties(); List <TableCellAttribute> dtoAttributes = new List <TableCellAttribute>(); string defaultOrderPropertyName = null; FilterOrderType? defaultOrderType = null; var tableCellAttributePropertyNames = new List <string>(); var sortablePropertyAttributes = new List <SortablePropertyAttribute>(); foreach (var property in properties) { var defaultOrderAttribute = (TableDefaultOrderPropertyAttribute)property.GetCustomAttributes(typeof(TableDefaultOrderPropertyAttribute), false).FirstOrDefault(); if (defaultOrderAttribute != null) { defaultOrderPropertyName = property.Name; defaultOrderType = defaultOrderAttribute.OrderType; } if (property.GetCustomAttributes(typeof(SortablePropertyAttribute), false).Length > 0) { var orderPropertyAttribute = (SortablePropertyAttribute)property.GetCustomAttributes(typeof(SortablePropertyAttribute), false).First(); tableCellAttributePropertyNames.Add(property.Name); sortablePropertyAttributes.Add(orderPropertyAttribute); } else if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0) { sortablePropertyAttributes.Add(null); tableCellAttributePropertyNames.Add(null); } if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0) { dtoAttributes.Add((TableCellAttribute)property.GetCustomAttributes(typeof(TableCellAttribute), false).FirstOrDefault()); } } SortTableCellAttributesAndPropertyNamesByOrderWeight(dtoAttributes, tableCellAttributePropertyNames, sortablePropertyAttributes); var tableCellNames = dtoAttributes .Select(x => x.Name) .Distinct() .ToList(); for (var i = 0; i < tableCellNames.Count; i++) { var propertyName = tableCellAttributePropertyNames[i]; var tableCellName = tableCellNames[i]; var orderPropertyAttribute = sortablePropertyAttributes[i]; if (orderPropertyAttribute != null) { if (propertyName == defaultOrderPropertyName) { tableViewModel.Header.AddCell(tableCellName, orderPropertyAttribute.OrderArgument, true, defaultOrderType); } else { tableViewModel.Header.AddCell(tableCellName, orderPropertyAttribute.OrderArgument, false); } } else { tableViewModel.Header.AddCell(tableCellName); } } if (entitiesResult.EntitiesCount > 0) { foreach (var entity in entitiesResult.Entities) { TableRowViewModel tableRow = new TableRowViewModel(); foreach (var property in properties) { if (property.GetCustomAttributes(typeof(EntityIdentifierAttribute), false).Length > 0) { tableRow.Identifier = property.GetValue(entity)?.ToString(); } if (property.GetCustomAttributes(typeof(TableCellAttribute), false).Length > 0) { TableCellAttribute propertyAttribute = (TableCellAttribute)property.GetCustomAttributes(typeof(TableCellAttribute), false).FirstOrDefault(); if (propertyAttribute?.Type == TableCellType.Flag) { tableRow.AddCell( propertyAttribute.Order, property.GetValue(entity), propertyAttribute.Type, propertyAttribute.Editable, property.Name, propertyAttribute.TextForTrueValue, propertyAttribute.TextForFalseValue); } else { tableRow.AddCell( propertyAttribute.Order, property.GetValue(entity), propertyAttribute.Type, propertyAttribute.Editable, property.Name, property.PropertyType.IsEnum ? property.PropertyType : null); } } } foreach (var action in actions) { List <string> parsedParameters = new List <string>(); foreach (var parameter in action.RawParameters) { if (parameter.StartsWith("[", StringComparison.OrdinalIgnoreCase) && parameter.EndsWith("]", StringComparison.OrdinalIgnoreCase)) { string propertyName = parameter.Substring(1, parameter.Length - 2); string propertyValue = entity.GetType().GetProperty(propertyName)?.GetValue(entity).ToString(); parsedParameters.Add(propertyValue); } else { parsedParameters.Add(parameter); } } tableRow.AddAction(action, parsedParameters); } tableViewModel.AddRow(tableRow); } } tableViewModel.SetPagination(entitiesResult.CurrentPage, entitiesResult.Pages); return(tableViewModel); }