private async Task <ProfileViewModel> GetProfileViewModelAsync(User user, int lastPage, int adsCount, string orderBy = "dateDesc", int page = 1) { var ads = await advertisementService.GetByUserIdAsync(user.Id, page, GlobalConstants.AdsOnPageCount, orderBy); ads = OrderAds(ads, orderBy); var viewModel = new ProfileViewModel { UserId = user.Id, Username = user.UserName, CurrentPage = page, LastPage = lastPage, TotalAdsCount = adsCount, OrderParam = "orderBy=" + orderBy, PageParam = "id=" + user.Id, Advertisements = new List <ListingViewModel>(), IsFollowedByLoggedInUser = await userService.IsFollowedByUser(User.FindFirstValue(ClaimTypes.NameIdentifier), user.Id) }; foreach (var ad in ads) { var category = await categoryService.GetByIdAsync(ad.CategoryId); string subCategoryName = null; if (await subCategoryService.ContainsByIdAsync(ad.SubCategoryId)) { var subCategory = await subCategoryService.GetByIdAsync(ad.SubCategoryId); subCategoryName = subCategory.Name; } var town = await townService.GetByIdAsync(ad.TownId); var adViewModel = new ListingViewModel { Address = ad.Address, CategoryName = category.Name, CreatedOn = ad.CreatedOn.ToString(GlobalConstants.DateTimeFormat), Id = ad.Id, Name = ad.Name, Price = ad.Price, SubCategoryName = subCategoryName, TownName = town.Name, Image = ad.Images.FirstOrDefault() }; viewModel.Advertisements.Add(adViewModel); } return(viewModel); }
public async Task <IViewComponentResult> InvokeAsync() { var userId = userManager.GetUserId(HttpContext.User); var ads = await advertisementService.GetLatestAsync(8, userId); var result = new List <ViewModels.Advertisement.ListingViewModel>(); foreach (var ad in ads) { var category = await categoryService.GetByIdAsync(ad.CategoryId); string subCategoryName = null; if (await subCategoryService.ContainsByIdAsync(ad.SubCategoryId)) { var subCategory = await subCategoryService.GetByIdAsync(ad.SubCategoryId); subCategoryName = subCategory.Name; } var town = await townService.GetByIdAsync(ad.TownId); var adViewModel = new ViewModels.Advertisement.ListingViewModel { Address = ad.Address, CategoryName = category.Name, CreatedOn = ad.CreatedOn.ToString(GlobalConstants.DateTimeFormat), Id = ad.Id, Name = ad.Name, Price = ad.Price, SubCategoryName = subCategoryName, TownName = town.Name, Image = ad.Images.FirstOrDefault() }; result.Add(adViewModel); } return(View(result)); }
public async Task <IActionResult> GetByCategory(string categoryId, string orderBy = "dateDesc", int page = 1) { if (page <= 0) { return(Redirect("/Home/Index")); } var adsCount = await advertisementService.GetCountByCategoryIdAsync(categoryId); var lastPage = adsCount / GlobalConstants.AdsOnPageCount + 1; if (adsCount % GlobalConstants.AdsOnPageCount == 0 && adsCount > 0) { lastPage -= 1; } if (page > lastPage) { return(Redirect("/Home/Index")); } var ads = await advertisementService.GetByCategoryIdAsync(categoryId, page, GlobalConstants.AdsOnPageCount, orderBy); ads = OrderAds(ads, orderBy); var result = new ListingPageViewModel(); result = await GetCategoriesAndSubCategoriesAsync(result); foreach (var ad in ads) { var category = await categoryService.GetByIdAsync(ad.CategoryId); string subCategoryName = null; if (await subCategoryService.ContainsByIdAsync(ad.SubCategoryId)) { var subCategory = await subCategoryService.GetByIdAsync(ad.SubCategoryId); subCategoryName = subCategory.Name; } var town = await townService.GetByIdAsync(ad.TownId); var adViewModel = new ListingViewModel { Address = ad.Address, CategoryName = category.Name, CreatedOn = ad.CreatedOn.ToString(GlobalConstants.DateTimeFormat), Id = ad.Id, Name = ad.Name, Price = ad.Price, SubCategoryName = subCategoryName, TownName = town.Name, Image = ad.Images.FirstOrDefault(), IsPromoted = ad.IsPromoted, }; result.Advertisements.Add(adViewModel); } result.CurrentPage = page; result.LastPage = lastPage; result.TotalAdsCount = adsCount; result.GetByParam = "GetByCategory"; result.OrderParam = "orderBy=" + orderBy; result.PageParam = "categoryId=" + categoryId; return(View("Listing", result)); }