public async Task <IActionResult> Wishlist(int page = 1) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (page <= 0) { return(Redirect("/User/Wishlist")); } var adsCount = await userAdWishlistService.GetWishlistCountAsync(userId); var lastPage = adsCount / GlobalConstants.AdsOnPageCount + 1; if (adsCount % GlobalConstants.AdsOnPageCount == 0 && adsCount > 0) { lastPage -= 1; } if (page > lastPage) { return(Redirect("/User/Wishlist")); } var ads = await userAdWishlistService.GetUserWishlistAsync(userId, page, GlobalConstants.AdsOnPageCount); var viewModel = new WishlistViewModel { CurrentPage = page, TotalAdsCount = adsCount, LastPage = lastPage, }; foreach (var ad in ads) { var category = await categoryService.GetByIdAsync(ad.CategoryId); var subCategoryName = await subCategoryService.GetByIdAsync(ad.SubCategoryId); var town = await townService.GetByIdAsync(ad.TownId); viewModel.Advertisements.Add(new WishlistAdViewModel { Category = $"{category.Name} -> {subCategoryName.Name}", CreatedOn = ad.CreatedOn.ToString(GlobalConstants.DateTimeFormat), Id = ad.Id, Name = ad.Name, Price = ad.Price, UserId = ad.UserId, UserName = userManager.FindByIdAsync(ad.UserId).GetAwaiter().GetResult().UserName }); } return(View(viewModel)); }
private async Task AddUsers(IList <VehicleUser> users, string roleName) { foreach (var user in users) { var vm = new UserViewModel(user); if (user.GroupId != null) { vm.GroupName = (await _groupService.GetByIdAsync(user.GroupId.Value)).Name; } if (user.TownId != null) { vm.TownName = (await _townService.GetByIdAsync(user.TownId.Value)).Name; } vm.Type = roleName; UserList.Add(vm); } }
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 GetByIdAsync_WithInvalidId_ShouldThrowArgumentException() { Assert.ThrowsAsync <ArgumentException>(async() => await service.GetByIdAsync("invalidId")); }
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)); }