Skip to content

Commit 82e927d

Browse files
committed
basket checkout event message sent
1 parent aee0ab4 commit 82e927d

File tree

10 files changed

+119
-78
lines changed

10 files changed

+119
-78
lines changed

src/Basket/Basket.API/Basket.API.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.1</TargetFramework>
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
1112
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
1213
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
1314
<PackageReference Include="StackExchange.Redis" Version="2.1.30" />

src/Basket/Basket.API/Controllers/BasketController.cs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
using Basket.API.Entities;
1+
using AutoMapper;
2+
using Basket.API.Entities;
23
using Basket.API.Repositories.Interfaces;
4+
using EventBusRabbitMQ.Common;
5+
using EventBusRabbitMQ.Events;
6+
using EventBusRabbitMQ.Producer;
37
using Microsoft.AspNetCore.Mvc;
48
using Microsoft.Extensions.Logging;
59
using System;
@@ -13,11 +17,15 @@ namespace Basket.API.Controllers
1317
public class BasketController : ControllerBase
1418
{
1519
private readonly IBasketRepository _repository;
20+
private readonly EventBusRabbitMQProducer _eventBus;
1621
private readonly ILogger<BasketController> _logger;
22+
private readonly IMapper _mapper;
1723

18-
public BasketController(IBasketRepository repository, ILogger<BasketController> logger)
24+
public BasketController(IBasketRepository repository, EventBusRabbitMQProducer eventBus, IMapper mapper, ILogger<BasketController> logger)
1925
{
2026
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
27+
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
28+
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
2129
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
2230
}
2331

@@ -49,44 +57,36 @@ public async Task<IActionResult> DeleteBasketByIdAsync(string userName)
4957
[ProducesResponseType((int)HttpStatusCode.BadRequest)]
5058
public async Task<ActionResult> Checkout([FromBody] BasketCheckout basketCheckout)
5159
{
60+
// get total price of the basket
5261
// remove the basket
53-
// send checkout event to rabbitMq
62+
// send checkout event to rabbitMq
5463

55-
var userName = "swn"; // _identityService.GetUserIdentity();
64+
var basket = await _repository.GetBasket(basketCheckout.UserName);
65+
var totalPrice = basket.TotalPrice;
5666

57-
var basketRemoved = await _repository.DeleteBasket(userName);
67+
var basketRemoved = await _repository.DeleteBasket(basketCheckout.UserName);
5868
if (!basketRemoved)
5969
{
6070
return BadRequest();
6171
}
6272

63-
//basketCheckout.RequestId = Guid.NewGuid();
64-
//basketCheckout.Buyer = userId;
65-
//basketCheckout.City = "asd";
66-
//basketCheckout.Country = "asd";
73+
// Once basket is checkout, sends an integration event to
74+
// ordering.api to convert basket to order and proceeds with
75+
// order creation process
6776

68-
//_eventBus.PublishBasketCheckout("basketCheckoutQueue", basketCheckout);
69-
70-
// TODO : burayı alttaki gibi yapılacak -- rabbitMQ kısmı ayrı bir class library yapılıp BasketCheckoutAcceptedIntegrationEvent class ı yapılıp 2 tarafta onu kullanacak
71-
72-
//var userName = this.HttpContext.User.FindFirst(x => x.Type == ClaimTypes.Name).Value;
73-
74-
//var eventMessage = new UserCheckoutAcceptedIntegrationEvent(userId, userName, basketCheckout.City, basketCheckout.Street,
75-
// basketCheckout.State, basketCheckout.Country, basketCheckout.ZipCode, basketCheckout.CardNumber, basketCheckout.CardHolderName,
76-
// basketCheckout.CardExpiration, basketCheckout.CardSecurityNumber, basketCheckout.CardTypeId, basketCheckout.Buyer, basketCheckout.RequestId, basket);
77-
78-
//// Once basket is checkout, sends an integration event to
79-
//// ordering.api to convert basket to order and proceeds with
80-
//// order creation process
81-
//try
82-
//{
83-
// _eventBus.Publish(eventMessage);
84-
//}
85-
//catch (Exception ex)
86-
//{
87-
// _logger.LogError(ex, "ERROR Publishing integration event: {IntegrationEventId} from {AppName}", eventMessage.Id, "asd");
88-
// throw;
89-
//}
77+
var eventMessage = _mapper.Map<BasketCheckoutEvent>(basketCheckout);
78+
eventMessage.RequestId = Guid.NewGuid();
79+
eventMessage.TotalPrice = totalPrice;
80+
81+
try
82+
{
83+
_eventBus.PublishBasketCheckout(EventBusConstants.BasketCheckoutQueue, eventMessage);
84+
}
85+
catch (Exception ex)
86+
{
87+
_logger.LogError(ex, "ERROR Publishing integration event: {EventId} from {AppName}", eventMessage.RequestId, "Basket");
88+
throw;
89+
}
9090

9191
return Accepted();
9292
}
Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
6-
namespace Basket.API.Entities
1+
namespace Basket.API.Entities
72
{
83
public class BasketCheckout
9-
{
4+
{
5+
public string UserName { get; set; }
6+
public decimal TotalPrice { get; set; }
7+
8+
// BillingAddress
9+
public string FirstName { get; set; }
10+
public string LastName { get; set; }
11+
public string EmailAddress { get; set; }
12+
public string AddressLine { get; set; }
13+
public string Country { get; set; }
14+
public string State { get; set; }
15+
public string ZipCode { get; set; }
16+
17+
// Payment
18+
public string CardName { get; set; }
19+
public string CardNumber { get; set; }
20+
public string Expiration { get; set; }
21+
public string CVV { get; set; }
22+
public int PaymentMethod { get; set; }
1023
}
1124
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using AutoMapper;
2+
using Basket.API.Entities;
3+
using EventBusRabbitMQ.Events;
4+
5+
namespace Basket.API.Mapping
6+
{
7+
public class BasketMapping : Profile
8+
{
9+
public BasketMapping()
10+
{
11+
CreateMap<BasketCheckout, BasketCheckoutEvent>().ReverseMap();
12+
}
13+
}
14+
}

src/Basket/Basket.API/Startup.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
1+
using AutoMapper;
52
using Basket.API.Data;
63
using Basket.API.Data.Interfaces;
74
using Basket.API.Repositories;
85
using Basket.API.Repositories.Interfaces;
96
using EventBusRabbitMQ;
7+
using EventBusRabbitMQ.Producer;
108
using Microsoft.AspNetCore.Builder;
119
using Microsoft.AspNetCore.Hosting;
12-
using Microsoft.AspNetCore.Mvc;
1310
using Microsoft.Extensions.Configuration;
1411
using Microsoft.Extensions.DependencyInjection;
1512
using Microsoft.Extensions.Hosting;
16-
using Microsoft.Extensions.Logging;
1713
using Microsoft.OpenApi.Models;
1814
using RabbitMQ.Client;
1915
using StackExchange.Redis;
@@ -32,11 +28,10 @@ public Startup(IConfiguration configuration)
3228
// This method gets called by the runtime. Use this method to add services to the container.
3329
public void ConfigureServices(IServiceCollection services)
3430
{
35-
services.AddControllers();
31+
services.AddControllers();
3632

3733
#region Redis Dependencies
38-
39-
// add redis
34+
4035
services.AddSingleton<ConnectionMultiplexer>(sp =>
4136
{
4237
var configuration = ConfigurationOptions.Parse(Configuration.GetConnectionString("Redis"), true);
@@ -49,6 +44,8 @@ public void ConfigureServices(IServiceCollection services)
4944

5045
services.AddTransient<IBasketContext, BasketContext>();
5146
services.AddTransient<IBasketRepository, BasketRepository>();
47+
48+
services.AddAutoMapper(typeof(Startup));
5249

5350
#endregion
5451

@@ -68,22 +65,24 @@ public void ConfigureServices(IServiceCollection services)
6865
{
6966
var factory = new ConnectionFactory()
7067
{
71-
HostName = Configuration["EventBusHostName"]
68+
HostName = Configuration["EventBus:HostName"]
7269
};
7370

74-
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
71+
if (!string.IsNullOrEmpty(Configuration["EventBus:UserName"]))
7572
{
76-
factory.UserName = Configuration["EventBusUserName"];
73+
factory.UserName = Configuration["EventBus:UserName"];
7774
}
7875

79-
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
76+
if (!string.IsNullOrEmpty(Configuration["EventBus:Password"]))
8077
{
81-
factory.Password = Configuration["EventBusPassword"];
78+
factory.Password = Configuration["EventBus:Password"];
8279
}
8380

8481
return new RabbitMQConnection(factory);
8582
});
8683

84+
services.AddSingleton<EventBusRabbitMQProducer>();
85+
8786
#endregion
8887

8988
}

src/Basket/Basket.API/appsettings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,11 @@
99
"Microsoft.Hosting.Lifetime": "Information"
1010
}
1111
},
12-
"AllowedHosts": "*"
12+
"AllowedHosts": "*",
13+
14+
"EventBus": {
15+
"HostName": "localhost",
16+
"UserName": "guest",
17+
"Password": "guest"
18+
}
1319
}

src/Common/EventBusRabbitMQ/Common/BasketCheckout.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Common/EventBusRabbitMQ/Consumer/EventBusRabbitMQConsumer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using EventBusRabbitMQ.Common;
2+
using EventBusRabbitMQ.Events;
23
using Newtonsoft.Json;
34
using RabbitMQ.Client;
45
using RabbitMQ.Client.Events;
@@ -34,7 +35,7 @@ private void ReceivedEvent(object sender, BasicDeliverEventArgs e)
3435
if (e.RoutingKey == EventBusConstants.BasketCheckoutQueue)
3536
{
3637
var message = Encoding.UTF8.GetString(e.Body);
37-
BasketCheckout basketCheckout = JsonConvert.DeserializeObject<BasketCheckout>(message);
38+
var basketCheckoutEvent = JsonConvert.DeserializeObject<BasketCheckoutEvent>(message);
3839

3940
// TODO : stuff
4041
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace EventBusRabbitMQ.Events
4+
{
5+
public class BasketCheckoutEvent
6+
{
7+
public Guid RequestId { get; set; }
8+
public string UserName { get; set; }
9+
public decimal TotalPrice { get; set; }
10+
11+
// BillingAddress
12+
public string FirstName { get; set; }
13+
public string LastName { get; set; }
14+
public string EmailAddress { get; set; }
15+
public string AddressLine { get; set; }
16+
public string Country { get; set; }
17+
public string State { get; set; }
18+
public string ZipCode { get; set; }
19+
20+
// Payment
21+
public string CardName { get; set; }
22+
public string CardNumber { get; set; }
23+
public string Expiration { get; set; }
24+
public string CVV { get; set; }
25+
public int PaymentMethod { get; set; }
26+
}
27+
}

src/Common/EventBusRabbitMQ/Producer/EventBusRabbitMQProducer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using EventBusRabbitMQ.Common;
1+
using EventBusRabbitMQ.Events;
22
using Newtonsoft.Json;
33
using RabbitMQ.Client;
44
using System;
@@ -15,7 +15,7 @@ public EventBusRabbitMQProducer(IRabbitMQConnection connection)
1515
_connection = connection ?? throw new ArgumentNullException(nameof(connection));
1616
}
1717

18-
public void PublishBasketCheckout(string queueName, BasketCheckout publishModel)
18+
public void PublishBasketCheckout(string queueName, BasketCheckoutEvent publishModel)
1919
{
2020
using (var channel = _connection.CreateModel())
2121
{
@@ -38,6 +38,6 @@ public void PublishBasketCheckout(string queueName, BasketCheckout publishModel)
3838
};
3939
channel.ConfirmSelect();
4040
}
41-
}
41+
}
4242
}
4343
}

0 commit comments

Comments
 (0)