Skip to content

Commit feafb4b

Browse files
committed
Merge
2 parents 3a8e68a + a8733ac commit feafb4b

17 files changed

Lines changed: 192 additions & 177 deletions

File tree

eShopOnContainers-ServicesAndWebApps.sln

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26228.0
4+
VisualStudioVersion = 15.0.26228.4
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{932D8224-11F6-4D07-B109-DA28AD288A63}"
77
EndProject
@@ -64,8 +64,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntegrationTests", "test\Se
6464
EndProject
6565
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FunctionalTests", "test\Services\FunctionalTests\FunctionalTests.csproj", "{CFE2FACB-4538-4B99-8A10-306F3882952D}"
6666
EndProject
67-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{47857844-D05A-4C37-BFB2-AF19B7EC418D}"
68-
EndProject
6967
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BuildingBlocks", "BuildingBlocks", "{DB0EFB20-B024-4E5E-A75C-52143C131D25}"
7068
EndProject
7169
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "EventBus", "EventBus", "{807BB76E-B2BB-47A2-A57B-3D1B20FF5E7F}"

src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,21 @@ public async Task<IActionResult> CatalogBrands()
130130
return Ok(items);
131131
}
132132

133+
[Route("edit")]
133134
[HttpPost]
134-
public async Task<IActionResult> Post([FromBody]CatalogItem value)
135+
public async Task<IActionResult> EditProduct([FromBody]CatalogItem product)
135136
{
136-
var item = await _context.CatalogItems.SingleOrDefaultAsync(i => i.Id == value.Id);
137+
var item = await _context.CatalogItems.SingleOrDefaultAsync(i => i.Id == product.Id);
137138

138139
if (item == null)
139140
{
140141
return NotFound();
141142
}
142143

143-
if (item.Price != value.Price)
144+
if (item.Price != product.Price)
144145
{
145146
var oldPrice = item.Price;
146-
item.Price = value.Price;
147+
item.Price = product.Price;
147148
_context.CatalogItems.Update(item);
148149

149150
var @event = new ProductPriceChangedIntegrationEvent(item.Id, item.Price, oldPrice);
@@ -158,10 +159,47 @@ public async Task<IActionResult> Post([FromBody]CatalogItem value)
158159
eventLogEntry.State = EventStateEnum.Published;
159160
_context.IntegrationEventLog.Update(eventLogEntry);
160161
await _context.SaveChangesAsync();
161-
}
162+
}
162163

163164
return Ok();
164-
}
165+
}
166+
167+
[Route("create")]
168+
[HttpPost]
169+
public async Task<IActionResult> CreateProduct([FromBody]CatalogItem product)
170+
{
171+
_context.CatalogItems.Add(
172+
new CatalogItem
173+
{
174+
CatalogBrandId = product.CatalogBrandId,
175+
CatalogTypeId = product.CatalogTypeId,
176+
Description = product.Description,
177+
Name = product.Name,
178+
PictureUri = product.PictureUri,
179+
Price = product.Price
180+
});
181+
182+
await _context.SaveChangesAsync();
183+
184+
return Ok();
185+
}
186+
187+
[Route("{id}")]
188+
[HttpDelete]
189+
public async Task<IActionResult> DeleteProduct(int id)
190+
{
191+
var product = _context.CatalogItems.SingleOrDefault(x => x.Id == id);
192+
193+
if (product == null)
194+
{
195+
return NotFound();
196+
}
197+
198+
_context.CatalogItems.Remove(product);
199+
await _context.SaveChangesAsync();
200+
201+
return Ok();
202+
}
165203

166204
private List<CatalogItem> ComposePicUri(List<CatalogItem> items) {
167205
var baseUri = _settings.Value.ExternalCatalogBaseUrl;

src/Web/WebMVC/Controllers/AccountController.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,11 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
1111
public class AccountController : Controller
1212
{
1313
private readonly IIdentityParser<ApplicationUser> _identityParser;
14-
public AccountController(IIdentityParser<ApplicationUser> identityParser)
15-
{
14+
public AccountController(IIdentityParser<ApplicationUser> identityParser) =>
1615
_identityParser = identityParser;
17-
}
18-
19-
public ActionResult Index()
20-
{
21-
return View();
22-
}
2316

17+
public ActionResult Index() => View();
18+
2419
[Authorize]
2520
public IActionResult SignIn(string returnUrl)
2621
{

src/Web/WebMVC/Controllers/CatalogController.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ public class CatalogController : Controller
1414
{
1515
private ICatalogService _catalogSvc;
1616

17-
public CatalogController(ICatalogService catalogSvc)
18-
{
17+
public CatalogController(ICatalogService catalogSvc) =>
1918
_catalogSvc = catalogSvc;
20-
}
2119

2220
public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilterApplied, int? page)
2321
{
@@ -35,7 +33,7 @@ public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilter
3533
ActualPage = page ?? 0,
3634
ItemsPerPage = catalog.Data.Count,
3735
TotalItems = catalog.Count,
38-
TotalPages = int.Parse(Math.Ceiling(((decimal)catalog.Count / itemsPage)).ToString())
36+
TotalPages = (int)Math.Ceiling(((decimal)catalog.Count / itemsPage))
3937
}
4038
};
4139

@@ -45,10 +43,7 @@ public async Task<IActionResult> Index(int? BrandFilterApplied, int? TypesFilter
4543
return View(vm);
4644
}
4745

48-
public IActionResult Error()
49-
{
50-
return View();
51-
}
46+
public IActionResult Error() => View();
5247
}
5348
}
5449

src/Web/WebMVC/Extensions/HttpClientExtensions.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,14 @@ namespace Microsoft.eShopOnContainers.WebMVC.Extensions
1111
{
1212
public static class HttpClientExtensions
1313
{
14-
public static void SetBasicAuthentication(this HttpClient client, string userName, string password)
15-
{
14+
public static void SetBasicAuthentication(this HttpClient client, string userName, string password) =>
1615
client.DefaultRequestHeaders.Authorization = new BasicAuthenticationHeaderValue(userName, password);
17-
}
1816

19-
public static void SetToken(this HttpClient client, string scheme, string token)
20-
{
17+
public static void SetToken(this HttpClient client, string scheme, string token) =>
2118
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(scheme, token);
22-
}
2319

24-
public static void SetBearerToken(this HttpClient client, string token)
25-
{
20+
public static void SetBearerToken(this HttpClient client, string token) =>
2621
client.SetToken(JwtConstants.TokenType, token);
27-
}
2822
}
2923

3024
public class BasicAuthenticationHeaderValue : AuthenticationHeaderValue

src/Web/WebMVC/Extensions/SessionExtensions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88

99
public static class SessionExtensions
1010
{
11-
public static void SetObject(this ISession session, string key, object value)
12-
{
11+
public static void SetObject(this ISession session, string key, object value) =>
1312
session.SetString(key, JsonConvert.SerializeObject(value));
14-
}
1513

1614
public static T GetObject<T>(this ISession session, string key)
1715
{

src/Web/WebMVC/Services/BasketService.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ public async Task<Basket> GetBasket(ApplicationUser user)
3333

3434
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
3535

36-
var basketUrl = $"{_remoteServiceBaseUrl}/{user.Id.ToString()}";
36+
var basketUrl = $"{_remoteServiceBaseUrl}/{user.Id}";
3737
var dataString = await _apiClient.GetStringAsync(basketUrl);
38-
var response = JsonConvert.DeserializeObject<Basket>(dataString);
39-
if (response == null)
40-
{
41-
response = new Basket()
38+
// Use the ?? Null conditional operator to simplify the initialization of response
39+
var response = JsonConvert.DeserializeObject<Basket>(dataString) ??
40+
new Basket()
4241
{
4342
BuyerId = user.Id
4443
};
45-
}
4644

4745
return response;
4846
}
@@ -67,9 +65,12 @@ public async Task<Basket> SetQuantities(ApplicationUser user, Dictionary<string,
6765

6866
basket.Items.ForEach(x =>
6967
{
70-
var quantity = quantities.Where(y => y.Key == x.Id).FirstOrDefault();
71-
if (quantities.Where(y => y.Key == x.Id).Count() > 0)
72-
x.Quantity = quantity.Value;
68+
// Simplify this logic by using the
69+
// new out variable initializer.
70+
if (quantities.TryGetValue(x.Id, out var quantity))
71+
{
72+
x.Quantity = quantity;
73+
}
7374
});
7475

7576
return basket;
@@ -119,7 +120,7 @@ public async Task CleanBasket(ApplicationUser user)
119120
var token = await context.Authentication.GetTokenAsync("access_token");
120121

121122
_apiClient.Inst.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
122-
var basketUrl = $"{_remoteServiceBaseUrl}/{user.Id.ToString()}";
123+
var basketUrl = $"{_remoteServiceBaseUrl}/{user.Id}";
123124
var response = await _apiClient.DeleteAsync(basketUrl);
124125

125126
//CCE: response status code...

src/Web/WebMVC/Services/CatalogService.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ public async Task<IEnumerable<SelectListItem>> GetBrands()
6161
JArray brands = JArray.Parse(dataString);
6262
foreach (JObject brand in brands.Children<JObject>())
6363
{
64-
dynamic item = brand;
65-
items.Add(new SelectListItem() { Value = item.id, Text = item.brand });
64+
items.Add(new SelectListItem()
65+
{
66+
Value = brand.Value<string>("id"),
67+
Text = brand.Value<string>("brand")
68+
});
6669
}
6770

6871
return items;
@@ -79,10 +82,12 @@ public async Task<IEnumerable<SelectListItem>> GetTypes()
7982
JArray brands = JArray.Parse(dataString);
8083
foreach (JObject brand in brands.Children<JObject>())
8184
{
82-
dynamic item = brand;
83-
items.Add(new SelectListItem() { Value = item.id, Text = item.type });
85+
items.Add(new SelectListItem()
86+
{
87+
Value = brand.Value<string>("id"),
88+
Text = brand.Value<string>("type")
89+
});
8490
}
85-
8691
return items;
8792
}
8893
}

src/Web/WebMVC/Services/IdentityParser.cs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,31 @@ public class IdentityParser:IIdentityParser<ApplicationUser>
1212
{
1313
public ApplicationUser Parse(IPrincipal principal)
1414
{
15-
var user = new ApplicationUser();
16-
var claims = (ClaimsPrincipal)principal;
15+
// Pattern matching 'is' expression
16+
// assigns "claims" if "principal" is a "ClaimsPrincipal"
17+
if (principal is ClaimsPrincipal claims)
18+
{
19+
return new ApplicationUser
20+
{
1721

18-
user.CardHolderName = (claims.Claims.Where(x => x.Type == "card_holder").Count() > 0) ? claims.Claims.First(x => x.Type == "card_holder").Value : "";
19-
user.CardNumber = (claims.Claims.Where(x => x.Type == "card_number").Count() > 0) ? claims.Claims.First(x => x.Type == "card_number").Value : "";
20-
user.Expiration = (claims.Claims.Where(x => x.Type == "card_expiration").Count() > 0) ? claims.Claims.First(x => x.Type == "card_expiration").Value : "";
21-
user.CardType = (claims.Claims.Where(x => x.Type == "missing").Count() > 0) ? int.Parse(claims.Claims.First(x => x.Type == "missing").Value) : 0;
22-
user.City = (claims.Claims.Where(x => x.Type == "address_city").Count() > 0) ? claims.Claims.First(x => x.Type == "address_city").Value : "";
23-
user.Country = (claims.Claims.Where(x => x.Type == "address_country").Count() > 0) ? claims.Claims.First(x => x.Type == "address_country").Value : "";
24-
user.Email = (claims.Claims.Where(x => x.Type == "email").Count() > 0) ? claims.Claims.First(x => x.Type == "email").Value : "";
25-
user.Id = (claims.Claims.Where(x => x.Type == "sub").Count() > 0) ? claims.Claims.First(x => x.Type == "sub").Value : "";
26-
user.LastName = (claims.Claims.Where(x => x.Type == "last_name").Count() > 0) ? claims.Claims.First(x => x.Type == "last_name").Value : "";
27-
user.Name = (claims.Claims.Where(x => x.Type == "name").Count() > 0) ? claims.Claims.First(x => x.Type == "name").Value : "";
28-
user.PhoneNumber = (claims.Claims.Where(x => x.Type == "phone_number").Count() > 0) ? claims.Claims.First(x => x.Type == "phone_number").Value : "";
29-
user.SecurityNumber = (claims.Claims.Where(x => x.Type == "card_security_number").Count() > 0) ? claims.Claims.First(x => x.Type == "card_security_number").Value : "";
30-
user.State = (claims.Claims.Where(x => x.Type == "address_state").Count() > 0) ? claims.Claims.First(x => x.Type == "address_state").Value : "";
31-
user.Street = (claims.Claims.Where(x => x.Type == "address_street").Count() > 0) ? claims.Claims.First(x => x.Type == "address_street").Value : "";
32-
user.ZipCode = (claims.Claims.Where(x => x.Type == "address_zip_code").Count() > 0) ? claims.Claims.First(x => x.Type == "address_zip_code").Value : "";
33-
34-
return user;
22+
CardHolderName = claims.Claims.FirstOrDefault(x => x.Type == "card_holder")?.Value ?? "",
23+
CardNumber = claims.Claims.FirstOrDefault(x => x.Type == "card_number")?.Value ?? "",
24+
Expiration = claims.Claims.FirstOrDefault(x => x.Type == "card_expiration")?.Value ?? "",
25+
CardType = int.Parse(claims.Claims.FirstOrDefault(x => x.Type == "missing")?.Value ?? "0"),
26+
City = claims.Claims.FirstOrDefault(x => x.Type == "address_city")?.Value ?? "",
27+
Country = claims.Claims.FirstOrDefault(x => x.Type == "address_country")?.Value ?? "",
28+
Email = claims.Claims.FirstOrDefault(x => x.Type == "email")?.Value ?? "",
29+
Id = claims.Claims.FirstOrDefault(x => x.Type == "sub")?.Value ?? "",
30+
LastName = claims.Claims.FirstOrDefault(x => x.Type == "last_name")?.Value ?? "",
31+
Name = claims.Claims.FirstOrDefault(x => x.Type == "name")?.Value ?? "",
32+
PhoneNumber = claims.Claims.FirstOrDefault(x => x.Type == "phone_number")?.Value ?? "",
33+
SecurityNumber = claims.Claims.FirstOrDefault(x => x.Type == "card_security_number")?.Value ?? "",
34+
State = claims.Claims.FirstOrDefault(x => x.Type == "address_state")?.Value ?? "",
35+
Street = claims.Claims.FirstOrDefault(x => x.Type == "address_street")?.Value ?? "",
36+
ZipCode = claims.Claims.FirstOrDefault(x => x.Type == "address_zip_code")?.Value ?? ""
37+
};
38+
}
39+
throw new ArgumentException(message: "The principal must be a ClaimsPrincipal", paramName: nameof(principal));
3540
}
3641
}
3742
}

src/Web/WebMVC/Services/Utilities/HttpApiClient.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,18 @@ public HttpApiClient()
1616
_client = new HttpClient();
1717
_logger = new LoggerFactory().CreateLogger(nameof(HttpApiClientWrapper));
1818
}
19+
20+
public Task<string> GetStringAsync(string uri) =>
21+
_client.GetStringAsync(uri);
1922

20-
public async Task<string> GetStringAsync(string uri)
21-
{
22-
return await HttpInvoker(async () =>
23-
await _client.GetStringAsync(uri));
24-
}
25-
26-
public async Task<HttpResponseMessage> PostAsync<T>(string uri, T item)
23+
public Task<HttpResponseMessage> PostAsync<T>(string uri, T item)
2724
{
2825
var contentString = new StringContent(JsonConvert.SerializeObject(item), System.Text.Encoding.UTF8, "application/json");
29-
return await HttpInvoker(async () =>
30-
await _client.PostAsync(uri, contentString));
31-
}
32-
33-
public async Task<HttpResponseMessage> DeleteAsync(string uri)
34-
{
35-
return await HttpInvoker(async () =>
36-
await _client.DeleteAsync(uri));
26+
return _client.PostAsync(uri, contentString);
3727
}
3828

39-
private async Task<T> HttpInvoker<T>(Func<Task<T>> action)
40-
{
41-
return await action();
42-
}
29+
public Task<HttpResponseMessage> DeleteAsync(string uri) =>
30+
_client.DeleteAsync(uri);
4331
}
4432
}
4533

0 commit comments

Comments
 (0)