public async Task <IActionResult> IndexAsync(int?categoryId, string key) { List <SharingListModel> models = new List <SharingListModel>(); List <Sharing> sharings = new List <Sharing>(); if (categoryId == null && string.IsNullOrWhiteSpace(key)) { sharings = await _sharingService.GetAllSortedByDateAsync(); } else if (!string.IsNullOrWhiteSpace(key)) { sharings = await _sharingService.SearchSharingAsync(key); } else { sharings = await _sharingService.GetAllByCategoryIdAsync((int)categoryId); } foreach (var item in sharings) { string userId = item.UserId.ToString(); SharingListModel model = _mapper.Map <SharingListModel>(item); var owner = await _userManager.FindByIdAsync(model.UserId.ToString()); model.UserName = owner.UserName; models.Add(model); } return(View(models)); }
public async Task <IViewComponentResult> InvokeAsync() { List <Category> categories = await _categoryService.GetAllAsync(); List <CategoryListModel> models = new List <CategoryListModel>(); foreach (var item in categories) { List <Sharing> sharings = await _sharingService.GetAllByCategoryIdAsync(item.Id); CategoryListModel model = new CategoryListModel { Id = item.Id, Name = item.Name, Number = sharings.Count }; models.Add(model); } return(View(models)); }