public Task <IViewComponentResult> InvokeAsync(IEnumerable <object> data, ListComponentFieldTypes expression) { ListComponentViewModel list = new ListComponentViewModel() { Items = data, Expression = expression.ToString() }; return(Task.FromResult <IViewComponentResult>(View(list))); }
/// <summary> /// Retrieve components from the component database /// </summary> /// <param name="listComponentViewModel">View Model containng information to shape the list request</param> /// <returns></returns> public async Task <ListComponentResponse> ListComponents(ListComponentViewModel listComponentViewModel) { // TODO Number 1 Sorting and Paging var components = await ComponentDatabase.GetComponents(); var pageSize = listComponentViewModel.PageSize.GetValueOrDefault(9999); var pageNo = listComponentViewModel.PageNumber.GetValueOrDefault(0); var sortBy = listComponentViewModel.SortField ?? SortFieldConstants.SORT_FIELD_ID; var sortDirection = listComponentViewModel.SortDir ?? SortDirConstants.SORT_DIR_ASC; if (sortBy == SortFieldConstants.SORT_FIELD_ID && sortDirection == SortDirConstants.SORT_DIR_ASC) { components = components.OrderBy(x => x.Id).Skip(pageSize * pageNo).Take(pageSize).ToList(); } else if (sortBy == SortFieldConstants.SORT_FIELD_ID && sortDirection == SortDirConstants.SORT_DIR_DESC) { components = components.OrderByDescending(x => x.Id).Skip(pageSize * pageNo).Take(pageSize).ToList(); } else if (sortBy == SortFieldConstants.SORT_FIELD_NAME && sortDirection == SortDirConstants.SORT_DIR_ASC) { components = components.OrderBy(x => x.Name).ThenBy(x => x.Id).Skip(pageSize * pageNo).Take(pageSize).ToList(); } else if (sortBy == SortFieldConstants.SORT_FIELD_NAME && sortDirection == SortDirConstants.SORT_DIR_DESC) { components = components.OrderByDescending(x => x.Name).ThenByDescending(x => x.Id).Skip(pageSize * pageNo).Take(pageSize).ToList(); } else if (sortBy == SortFieldConstants.SORT_FIELD_TYPE && sortDirection == SortDirConstants.SORT_DIR_ASC) { components = components.OrderBy(x => x.Type).ThenBy(x => x.Id).Skip(pageSize * pageNo).Take(pageSize).ToList(); } else if (sortBy == SortFieldConstants.SORT_FIELD_TYPE && sortDirection == SortDirConstants.SORT_DIR_DESC) { components = components.OrderByDescending(x => x.Type).ThenByDescending(x => x.Id).Skip(pageSize * pageNo).Take(pageSize).ToList(); } else if (sortBy == SortFieldConstants.SORT_FIELD_STATUS && sortDirection == SortDirConstants.SORT_DIR_ASC) { components = components.OrderBy(x => x.Status).ThenBy(x => x.Id).Skip(pageSize * pageNo).Take(pageSize).ToList(); } else if (sortBy == SortFieldConstants.SORT_FIELD_STATUS && sortDirection == SortDirConstants.SORT_DIR_DESC) { components = components.OrderByDescending(x => x.Status).ThenByDescending(x => x.Id).Skip(pageSize * pageNo).Take(pageSize).ToList(); } return(new ListComponentResponse() { Total = components.Count(), Components = components }); }
public async Task TestSortingByType() { ListComponentViewModel listComponentViewModel = new ListComponentViewModel() { SortDir = SortDirConstants.SORT_DIR_ASC, SortField = SortFieldConstants.SORT_FIELD_TYPE, }; IComponentService service = new ComponentService(); var result = (await service.ListComponents(listComponentViewModel)).Components.First(); Assert.Equal("Audio", result.Type); }
public async Task TestSorting() { ListComponentViewModel listComponentViewModel = new ListComponentViewModel() { SortDir = SortDirConstants.SORT_DIR_DESC, SortField = SortFieldConstants.SORT_FIELD_ID }; IComponentService service = new ComponentService(); var result = (await service.ListComponents(listComponentViewModel)).Components.First(); Assert.Equal(99, result.Id); }
public async Task TestPageSize() { ListComponentViewModel listComponentViewModel = new ListComponentViewModel() { PageSize = 5, PageNumber = 0 }; IComponentService service = new ComponentService(); var result = await service.ListComponents(listComponentViewModel); Assert.Equal(listComponentViewModel.PageSize, result.Components.Count()); }
public async Task TestChangeStatus() { ListComponentViewModel listComponentViewModel = new ListComponentViewModel() { PageSize = 100, PageNumber = 0 }; IComponentService service = new ComponentService(); var component = (await service.ListComponents(listComponentViewModel)).Components.First(x => x.Id == 1); await service.UpdateComponent(component.Id, new UpdateComponentViewModel() { Status = "CHANGED" }); var upldatedComponent = (await service.ListComponents(listComponentViewModel)).Components.First(x => x.Id == 1); Assert.Equal("CHANGED", upldatedComponent.Status); }
public async Task <ListComponentResponse> GetComponents(ListComponentViewModel listComponentViewModel) { return(await ComponentDatabase.GetComponents()); }
public async Task <ActionResult> Get([FromQuery] ListComponentViewModel listComponentView) { var components = await _componentService.ListComponents(listComponentView); return(Ok(components)); }