Skip to content

Commit 1002356

Browse files
author
Carlos Cañizares Estévez
committed
Fixed some issues in catalog filters and improve catalog views.
1 parent 49e85dc commit 1002356

18 files changed

Lines changed: 188 additions & 113 deletions

File tree

src/Web/WebMVC/Controllers/CartController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ public async Task<IActionResult> Index(Dictionary<string, int> quantities, strin
4949
return View(vm);
5050
}
5151

52-
public async Task<IActionResult> AddToCart(string productId)
52+
public async Task<IActionResult> AddToCart(CatalogItem productDetails)
5353
{
5454
var user = await _userManager.GetUserAsync(HttpContext.User);
55-
var productDetails = _catalogSvc.GetCatalogItem(productId);
55+
//var productDetails = _catalogSvc.GetCatalogItem(productId);
5656
var product = new BasketItem()
5757
{
5858
Id = Guid.NewGuid().ToString(),
5959
Quantity = 1,
6060
ProductName = productDetails.Name,
6161
PictureUrl = productDetails.PictureUri,
6262
UnitPrice = productDetails.Price,
63-
ProductId = productId
63+
ProductId = productDetails.Id
6464
};
6565
_basketSvc.AddItemToBasket(user, product);
6666
return RedirectToAction("Index", "Catalog");

src/Web/WebMVC/Models/Basket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public Basket()
1818

1919
public decimal Total()
2020
{
21-
return Items.Sum(x => x.UnitPrice * x.Quantity);
21+
return Math.Round(Items.Sum(x => x.UnitPrice * x.Quantity),2);
2222
}
2323
}
2424
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using BikeSharing_Private_Web_Site.Services.Pagination;
2+
using Microsoft.AspNetCore.Mvc.Rendering;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.eShopOnContainers.WebMVC.Models.CartViewModels
9+
{
10+
public class CartComponentViewModel
11+
{
12+
public int ItemsCount { get; set; }
13+
public string Disabled { get { return (ItemsCount == 0) ? "is-disabled" : ""; } }
14+
}
15+
}

src/Web/WebMVC/Models/CatalogViewModels/IndexViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public class IndexViewModel
1212
public IEnumerable<CatalogItem> CatalogItems { get; set; }
1313
public IEnumerable<SelectListItem> Brands { get; set; }
1414
public IEnumerable<SelectListItem> Types { get; set; }
15-
public int BrandFilterApplied { get; set; }
16-
public int TypesFilterApplied { get; set; }
15+
public int? BrandFilterApplied { get; set; }
16+
public int? TypesFilterApplied { get; set; }
1717
public PaginationInfo PaginationInfo { get; set; }
1818
}
1919
}

src/Web/WebMVC/Services/CatalogService.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public int TotalItems
3030

3131
public CatalogService(IOptions<AppSettings> settings) {
3232
_settings = settings;
33-
_remoteServiceBaseUrl = $"{_settings.Value.CatalogUrl}api/v1/catalog/";
33+
_remoteServiceBaseUrl = $"{_settings.Value.CatalogUrl}/api/v1/catalog/";
3434

3535
#region fake data
3636
_items = new List<CatalogItem>()
@@ -79,7 +79,11 @@ public async Task<Catalog> GetCatalogItems(int page,int take, int? brand, int? t
7979
var filterQs = "";
8080

8181
if (brand.HasValue || type.HasValue)
82-
filterQs = $"/type/{type ?? null}/brand/{brand ?? null}";
82+
{
83+
var brandQs = (brand.HasValue) ? brand.Value.ToString() : "null";
84+
var typeQs = (type.HasValue) ? type.Value.ToString() : "null";
85+
filterQs = $"/type/{typeQs}/brand/{brandQs}";
86+
}
8387

8488
var catalogUrl = $"{_remoteServiceBaseUrl}items{filterQs}?pageIndex={page}&pageSize={take}";
8589
var dataString = await _apiClient.GetStringAsync(catalogUrl);
@@ -98,7 +102,7 @@ public async Task<IEnumerable<SelectListItem>> GetBrands()
98102
var dataString = await _apiClient.GetStringAsync(url);
99103

100104
var items = new List<SelectListItem>();
101-
items.Add(new SelectListItem() { Value = "0", Text = "All", Selected = true });
105+
items.Add(new SelectListItem() { Value = null, Text = "All", Selected = true });
102106

103107
JArray brands = JArray.Parse(dataString);
104108
foreach (JObject brand in brands.Children<JObject>())
@@ -117,7 +121,7 @@ public async Task<IEnumerable<SelectListItem>> GetTypes()
117121
var dataString = await _apiClient.GetStringAsync(url);
118122

119123
var items = new List<SelectListItem>();
120-
items.Add(new SelectListItem() { Value = "0", Text = "All", Selected = true });
124+
items.Add(new SelectListItem() { Value = null, Text = "All", Selected = true });
121125

122126
JArray brands = JArray.Parse(dataString);
123127
foreach (JObject brand in brands.Children<JObject>())

src/Web/WebMVC/ViewComponents/Cart.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Mvc;
22
using Microsoft.eShopOnContainers.WebMVC.Models;
3+
using Microsoft.eShopOnContainers.WebMVC.Models.CartViewModels;
34
using Microsoft.eShopOnContainers.WebMVC.Services;
45
using System;
56
using System.Collections.Generic;
@@ -20,7 +21,11 @@ public Cart(IBasketService cartSvc)
2021
public async Task<IViewComponentResult> InvokeAsync(ApplicationUser user)
2122
{
2223
var itemsInCart = await ItemsInCartAsync(user);
23-
return View(itemsInCart);
24+
var vm = new CartComponentViewModel()
25+
{
26+
ItemsCount = itemsInCart
27+
};
28+
return View(vm);
2429
}
2530
private Task<int> ItemsInCartAsync(ApplicationUser user)
2631
{

src/Web/WebMVC/Views/Catalog/Index.cshtml

Lines changed: 22 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -24,87 +24,29 @@
2424
</div>
2525
</div>
2626
</div>
27-
2827
<div class="container home-catalog-container">
29-
<div class="container es-pager-top">
30-
<div class="row">
31-
<div class="col-xs-4">
32-
<nav>
33-
<ul>
34-
<li class="page-item">
35-
<a class="text previous @Model.PaginationInfo.Previous" id="Previous"
36-
href="@Url.Action("Index","Catalog", new { page = Model.PaginationInfo.ActualPage -1 })"
37-
aria-label="Previous">
38-
<span>Previous</span>
39-
</a>
40-
</li>
41-
</ul>
42-
</nav>
43-
</div>
44-
<div class="col-xs-4 u-align-center"><span>Showing @Html.DisplayFor(modelItem => modelItem.PaginationInfo.ItemsPerPage) of @Html.DisplayFor(modelItem => modelItem.PaginationInfo.TotalItems) products - Page @(Model.PaginationInfo.ActualPage + 1) of @Html.DisplayFor(x => x.PaginationInfo.TotalPages)</span></div>
45-
<div class="col-xs-4">
46-
<nav>
47-
<ul>
48-
<li class="page-item">
49-
<a class="text next @Model.PaginationInfo.Next" id="Next"
50-
href="@Url.Action("Index","Catalog", new { page = Model.PaginationInfo.ActualPage + 1 })"
51-
aria-label="Next">
52-
<span>Next</span>
53-
</a>
54-
</li>
55-
</ul>
56-
</nav>
57-
</div>
28+
@if (Model.CatalogItems.Count() > 0)
29+
{
30+
<div class="container es-pager-top">
31+
@Html.Partial("_pagination", Model)
5832
</div>
59-
</div>
60-
<div class="row">
61-
@foreach (var catalogItem in Model.CatalogItems)
62-
{
63-
<div class="col-sm-4 home-catalog-item">
64-
<form asp-controller="Cart" asp-action="AddToCart" asp-route-productId="@catalogItem.Id">
65-
<div class="home-catalog-item-image">
66-
<img src="@catalogItem.PictureUri" />
67-
<input type="submit" value="[ ADD TO CART ]" class="btn-brand home-catalog-item-image-addCart" />
68-
</div>
69-
<div class="home-catalog-item-title">
70-
<span>@catalogItem.Name</span>
71-
</div>
72-
<div class="home-catalog-item-price">
73-
<span>@catalogItem.Price.ToString("N2")</span>
74-
</div>
75-
</form>
76-
</div>
77-
}
78-
</div>
79-
<div class="container es-pager-bottom">
8033
<div class="row">
81-
<div class="col-xs-4">
82-
<nav>
83-
<ul>
84-
<li class="page-item">
85-
<a class="text previous @Model.PaginationInfo.Previous" id="Previous"
86-
href="@Url.Action("Index","Catalog", new { page = Model.PaginationInfo.ActualPage + -1 })"
87-
aria-label="Previous">
88-
<span>Previous</span>
89-
</a>
90-
</li>
91-
</ul>
92-
</nav>
93-
</div>
94-
<div class="col-xs-4 u-align-center"><span>Showing @Html.DisplayFor(modelItem => modelItem.PaginationInfo.ItemsPerPage) of @Html.DisplayFor(modelItem => modelItem.PaginationInfo.TotalItems) products - Page @(Model.PaginationInfo.ActualPage + 1) of @Html.DisplayFor(x => x.PaginationInfo.TotalPages)</span></div>
95-
<div class="col-xs-4">
96-
<nav>
97-
<ul>
98-
<li class="page-item">
99-
<a class="text next @Model.PaginationInfo.Next" id="Next"
100-
href="@Url.Action("Index","Catalog", new { page = Model.PaginationInfo.ActualPage + 1 })"
101-
aria-label="Next">
102-
<span>Next</span>
103-
</a>
104-
</li>
105-
</ul>
106-
</nav>
107-
</div>
34+
@foreach (var catalogItem in Model.CatalogItems)
35+
{
36+
<div class="col-xs-12 col-sm-6 col-lg-4 home-catalog-item">
37+
@Html.Partial("_product", catalogItem)
38+
</div>
39+
}
40+
10841
</div>
109-
</div>
110-
</div>
42+
<div class="container es-pager-bottom">
43+
@Html.Partial("_pagination", Model)
44+
</div>
45+
}
46+
else
47+
{
48+
<div class="home-catalog-noResults">
49+
THERE ARE NO RESULTS THAT MATCH YOUR SEARCH
50+
</div>
51+
}
52+
</div>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
@model Microsoft.eShopOnContainers.WebMVC.Models.CatalogViewModels.IndexViewModel
2+
3+
<div class="row">
4+
<div class="col-xs-4">
5+
<nav>
6+
<ul>
7+
<li class="page-item">
8+
<a class="text previous @Model.PaginationInfo.Previous" id="Previous"
9+
href="@Url.Action("Index","Catalog", new { page = Model.PaginationInfo.ActualPage -1 })"
10+
aria-label="Previous">
11+
<span>Previous</span>
12+
</a>
13+
</li>
14+
</ul>
15+
</nav>
16+
</div>
17+
<div class="col-xs-4 u-align-center"><span>Showing @Html.DisplayFor(modelItem => modelItem.PaginationInfo.ItemsPerPage) of @Html.DisplayFor(modelItem => modelItem.PaginationInfo.TotalItems) products - Page @(Model.PaginationInfo.ActualPage + 1) of @Html.DisplayFor(x => x.PaginationInfo.TotalPages)</span></div>
18+
<div class="col-xs-4">
19+
<nav>
20+
<ul>
21+
<li class="page-item">
22+
<a class="text next @Model.PaginationInfo.Next" id="Next"
23+
href="@Url.Action("Index","Catalog", new { page = Model.PaginationInfo.ActualPage + 1 })"
24+
aria-label="Next">
25+
<span>Next</span>
26+
</a>
27+
</li>
28+
</ul>
29+
</nav>
30+
</div>
31+
</div>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
@model Microsoft.eShopOnContainers.WebMVC.Models.CatalogViewModels.IndexViewModel
2+
3+
<div class="row">
4+
<div class="col-xs-4">
5+
<nav>
6+
<ul>
7+
<li class="page-item">
8+
<a class="text previous @Model.PaginationInfo.Previous" id="Previous"
9+
href="@Url.Action("Index","Catalog", new { page = Model.PaginationInfo.ActualPage -1 })"
10+
aria-label="Previous">
11+
<span>Previous</span>
12+
</a>
13+
</li>
14+
</ul>
15+
</nav>
16+
</div>
17+
<div class="col-xs-4 u-align-center"><span>Showing @Html.DisplayFor(modelItem => modelItem.PaginationInfo.ItemsPerPage) of @Html.DisplayFor(modelItem => modelItem.PaginationInfo.TotalItems) products - Page @(Model.PaginationInfo.ActualPage + 1) of @Html.DisplayFor(x => x.PaginationInfo.TotalPages)</span></div>
18+
<div class="col-xs-4">
19+
<nav>
20+
<ul>
21+
<li class="page-item">
22+
<a class="text next @Model.PaginationInfo.Next" id="Next"
23+
href="@Url.Action("Index","Catalog", new { page = Model.PaginationInfo.ActualPage + 1 })"
24+
aria-label="Next">
25+
<span>Next</span>
26+
</a>
27+
</li>
28+
</ul>
29+
</nav>
30+
</div>
31+
</div>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@model CatalogItem
2+
3+
<form asp-controller="Cart" asp-action="AddToCart">
4+
<div class="home-catalog-item-image">
5+
<img src="@Model.PictureUri" />
6+
<input type="submit" value="[ ADD TO CART ]" class="btn-brand home-catalog-item-image-addCart" />
7+
</div>
8+
<div class="home-catalog-item-title">
9+
<span>@Model.Name</span>
10+
</div>
11+
<div class="home-catalog-item-price">
12+
<span>@Model.Price.ToString("N2")</span>
13+
</div>
14+
<input type="hidden" asp-for="@Model.CatalogBrand" name="brand"/>
15+
<input type="hidden" asp-for="@Model.CatalogBrandId" name="brandId" />
16+
<input type="hidden" asp-for="@Model.CatalogType" name="type" />
17+
<input type="hidden" asp-for="@Model.CatalogTypeId" name="typeId" />
18+
<input type="hidden" asp-for="@Model.Description" name="description" />
19+
<input type="hidden" asp-for="@Model.Id" name="id" />
20+
<input type="hidden" asp-for="@Model.Name" name="name" />
21+
<input type="hidden" asp-for="@Model.PictureUri" name="pictureUri" />
22+
<input type="hidden" asp-for="@Model.Price" name="price" />
23+
</form>

0 commit comments

Comments
 (0)