Skip to content

Commit 4ef6b63

Browse files
Unai Zorrilla CastroUnai Zorrilla Castro
authored andcommitted
Review on 17/04/2017
1 parent 0aea1b2 commit 4ef6b63

37 files changed

Lines changed: 207 additions & 216 deletions

src/Services/Basket/Basket.API/BasketSettings.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
6-
namespace Microsoft.eShopOnContainers.Services.Basket.API
1+
namespace Microsoft.eShopOnContainers.Services.Basket.API
72
{
83
public class BasketSettings
94
{

src/Services/Basket/Basket.API/Infrastructure/ActionResults/InternalServerErrorObjectResult.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.AspNetCore.Mvc;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Threading.Tasks;
73

84
namespace Basket.API.Infrastructure.ActionResults
95
{

src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/OrderStartedIntegrationEventHandler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
33
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
44
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
75
using System.Threading.Tasks;
86

97
namespace Basket.API.IntegrationEvents.EventHandling
108
{
119
public class OrderStartedIntegrationEventHandler : IIntegrationEventHandler<OrderStartedIntegrationEvent>
1210
{
1311
private readonly IBasketRepository _repository;
12+
1413
public OrderStartedIntegrationEventHandler(IBasketRepository repository)
1514
{
16-
_repository = repository;
15+
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
1716
}
1817

1918
public async Task Handle(OrderStartedIntegrationEvent @event)

src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
22
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events;
33
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
4+
using System;
45
using System.Linq;
56
using System.Threading.Tasks;
67

@@ -9,24 +10,28 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even
910
public class ProductPriceChangedIntegrationEventHandler : IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>
1011
{
1112
private readonly IBasketRepository _repository;
13+
1214
public ProductPriceChangedIntegrationEventHandler(IBasketRepository repository)
1315
{
14-
_repository = repository;
16+
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
1517
}
1618

1719
public async Task Handle(ProductPriceChangedIntegrationEvent @event)
1820
{
19-
var userIds = await _repository.GetUsers();
21+
var userIds = await _repository.GetUsersAsync();
22+
2023
foreach (var id in userIds)
2124
{
2225
var basket = await _repository.GetBasketAsync(id);
26+
2327
await UpdatePriceInBasketItems(@event.ProductId, @event.NewPrice, @event.OldPrice, basket);
2428
}
2529
}
2630

2731
private async Task UpdatePriceInBasketItems(int productId, decimal newPrice, decimal oldPrice, CustomerBasket basket)
2832
{
2933
var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.ProductId) == productId).ToList();
34+
3035
if (itemsToUpdate != null)
3136
{
3237
foreach (var item in itemsToUpdate)

src/Services/Basket/Basket.API/IntegrationEvents/Events/OrderStartedIntegrationEvent.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
62

73
namespace Basket.API.IntegrationEvents.Events
84
{

src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Text;
52

63
namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events
74
{

src/Services/Basket/Basket.API/Model/IBasketRepository.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
1+
using System.Collections.Generic;
42
using System.Threading.Tasks;
53

64
namespace Microsoft.eShopOnContainers.Services.Basket.API.Model
75
{
86
public interface IBasketRepository
97
{
108
Task<CustomerBasket> GetBasketAsync(string customerId);
11-
Task<IEnumerable<string>> GetUsers();
9+
Task<IEnumerable<string>> GetUsersAsync();
1210
Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket);
1311
Task<bool> DeleteBasketAsync(string id);
1412
}

src/Services/Basket/Basket.API/Model/RedisBasketRepository.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using StackExchange.Redis;
1+
using Microsoft.Extensions.Logging;
62
using Microsoft.Extensions.Options;
73
using Newtonsoft.Json;
8-
using Microsoft.Extensions.Logging;
4+
using StackExchange.Redis;
5+
using System.Collections.Generic;
6+
using System.Linq;
97
using System.Net;
8+
using System.Threading.Tasks;
109

1110
namespace Microsoft.eShopOnContainers.Services.Basket.API.Model
1211
{
@@ -31,7 +30,7 @@ public async Task<bool> DeleteBasketAsync(string id)
3130
return await database.KeyDeleteAsync(id.ToString());
3231
}
3332

34-
public async Task<IEnumerable<string>> GetUsers()
33+
public async Task<IEnumerable<string>> GetUsersAsync()
3534
{
3635
var server = await GetServer();
3736

@@ -63,11 +62,12 @@ public async Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket)
6362
var created = await database.StringSetAsync(basket.BuyerId, JsonConvert.SerializeObject(basket));
6463
if (!created)
6564
{
66-
_logger.LogInformation("Problem persisting the item");
65+
_logger.LogInformation("Problem occur persisting the item.");
6766
return null;
6867
}
6968

70-
_logger.LogInformation("basket item persisted succesfully");
69+
_logger.LogInformation("Basket item persisted succesfully.");
70+
7171
return await GetBasketAsync(basket.BuyerId);
7272
}
7373

@@ -96,7 +96,9 @@ private async Task ConnectToRedisAsync()
9696
{
9797
//TODO: Need to make this more robust. Also want to understand why the static connection method cannot accept dns names.
9898
var ips = await Dns.GetHostAddressesAsync(_settings.ConnectionString);
99+
99100
_logger.LogInformation($"Connecting to database {_settings.ConnectionString} at IP {ips.First().ToString()}");
101+
100102
_redis = await ConnectionMultiplexer.ConnectAsync(ips.First().ToString());
101103
}
102104

src/Services/Basket/Basket.API/Startup.cs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
using System.Linq;
1+
using Basket.API.Infrastructure.Filters;
2+
using Basket.API.IntegrationEvents.EventHandling;
3+
using Basket.API.IntegrationEvents.Events;
24
using Microsoft.AspNetCore.Builder;
35
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
7+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
8+
using Microsoft.eShopOnContainers.Services.Basket.API.Auth.Server;
9+
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling;
10+
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events;
11+
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
412
using Microsoft.Extensions.Configuration;
513
using Microsoft.Extensions.DependencyInjection;
14+
using Microsoft.Extensions.HealthChecks;
615
using Microsoft.Extensions.Logging;
7-
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
8-
using StackExchange.Redis;
916
using Microsoft.Extensions.Options;
17+
using StackExchange.Redis;
18+
using System.Linq;
1019
using System.Net;
11-
using Microsoft.eShopOnContainers.Services.Basket.API.Auth.Server;
12-
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
13-
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events;
14-
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling;
15-
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
16-
using System;
17-
using Microsoft.Extensions.HealthChecks;
1820
using System.Threading.Tasks;
19-
using Basket.API.Infrastructure.Filters;
20-
using Basket.API.IntegrationEvents.Events;
21-
using Basket.API.IntegrationEvents.EventHandling;
2221

2322
namespace Microsoft.eShopOnContainers.Services.Basket.API
2423
{
@@ -59,17 +58,24 @@ public void ConfigureServices(IServiceCollection services)
5958
//and then creating the connection it seems reasonable to move
6059
//that cost to startup instead of having the first request pay the
6160
//penalty.
62-
services.AddSingleton<ConnectionMultiplexer>((sp) => {
63-
var config = sp.GetRequiredService<IOptionsSnapshot<BasketSettings>>().Value;
64-
var ips = Dns.GetHostAddressesAsync(config.ConnectionString).Result;
61+
services.AddSingleton<ConnectionMultiplexer>(sp => {
62+
var settings = sp.GetRequiredService<IOptions<BasketSettings>>().Value;
63+
var ips = Dns.GetHostAddressesAsync(settings.ConnectionString).Result;
64+
6565
return ConnectionMultiplexer.Connect(ips.First().ToString());
6666
});
6767

68+
services.AddSingleton<IEventBus>(sp =>
69+
{
70+
var settings = sp.GetRequiredService<IOptions<BasketSettings>>().Value;
71+
72+
return new EventBusRabbitMQ(settings.EventBusConnection);
73+
});
74+
6875
services.AddSwaggerGen();
69-
//var sch = new IdentitySecurityScheme();
76+
7077
services.ConfigureSwaggerGen(options =>
7178
{
72-
//options.AddSecurityDefinition("IdentityServer", sch);
7379
options.OperationFilter<AuthorizationHeaderParameterOperationFilter>();
7480
options.DescribeAllEnumsAsStrings();
7581
options.SingleApiVersion(new Swashbuckle.Swagger.Model.Info()
@@ -95,10 +101,7 @@ public void ConfigureServices(IServiceCollection services)
95101
services.AddTransient<IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>, ProductPriceChangedIntegrationEventHandler>();
96102
services.AddTransient<IIntegrationEventHandler<OrderStartedIntegrationEvent>, OrderStartedIntegrationEventHandler>();
97103

98-
var serviceProvider = services.BuildServiceProvider();
99-
var configuration = serviceProvider.GetRequiredService<IOptionsSnapshot<BasketSettings>>().Value;
100-
services.AddSingleton<IEventBus>(provider => new EventBusRabbitMQ(configuration.EventBusConnection));
101-
104+
102105
}
103106

104107
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -119,11 +122,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
119122
app.UseSwagger()
120123
.UseSwaggerUi();
121124

122-
var catalogPriceHandler = app.ApplicationServices.GetService<IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>>();
123-
var orderStartedHandler = app.ApplicationServices.GetService<IIntegrationEventHandler<OrderStartedIntegrationEvent>>();
124-
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
125-
eventBus.Subscribe<ProductPriceChangedIntegrationEvent>(catalogPriceHandler);
126-
eventBus.Subscribe<OrderStartedIntegrationEvent>(orderStartedHandler);
125+
ConfigureEventBus(app);
127126

128127
}
129128

@@ -136,6 +135,21 @@ protected virtual void ConfigureAuth(IApplicationBuilder app)
136135
ScopeName = "basket",
137136
RequireHttpsMetadata = false
138137
});
139-
}
138+
}
139+
140+
protected virtual void ConfigureEventBus(IApplicationBuilder app)
141+
{
142+
var catalogPriceHandler = app.ApplicationServices
143+
.GetService<IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>>();
144+
145+
var orderStartedHandler = app.ApplicationServices
146+
.GetService<IIntegrationEventHandler<OrderStartedIntegrationEvent>>();
147+
148+
var eventBus = app.ApplicationServices
149+
.GetRequiredService<IEventBus>();
150+
151+
eventBus.Subscribe<ProductPriceChangedIntegrationEvent>(catalogPriceHandler);
152+
eventBus.Subscribe<OrderStartedIntegrationEvent>(orderStartedHandler);
153+
}
140154
}
141155
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API
2+
{
3+
public class CatalogSettings
4+
{
5+
public string ExternalCatalogBaseUrl {get;set;}
6+
7+
public string EventBusConnection { get; set; }
8+
}
9+
}

0 commit comments

Comments
 (0)