Skip to content

Commit b425f90

Browse files
committed
Add product events handlers and fix bugs in Orders module
1 parent a065797 commit b425f90

24 files changed

Lines changed: 2059 additions & 30 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Ecommerce.Modules.Carts.Core.DAL;
2+
using Ecommerce.Shared.Abstractions.Events;
3+
using Microsoft.EntityFrameworkCore;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Ecommerce.Modules.Carts.Core.Events.External.Handlers
11+
{
12+
internal class ProductAddedToOrderHandler : IEventHandler<ProductAddedToOrder>
13+
{
14+
private readonly ICartsDbContext _dbContext;
15+
16+
public ProductAddedToOrderHandler(ICartsDbContext dbContext)
17+
{
18+
_dbContext = dbContext;
19+
}
20+
public async Task HandleAsync(ProductAddedToOrder @event)
21+
{
22+
var product = await _dbContext.Products.FirstOrDefaultAsync(p => p.Id == @event.ProductId);
23+
24+
if (product is not null && product.HasQuantity)
25+
{
26+
product.DecreaseQuantity(@event.Quantity);
27+
await _dbContext.SaveChangesAsync();
28+
}
29+
}
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Ecommerce.Modules.Carts.Core.DAL;
2+
using Ecommerce.Shared.Abstractions.Events;
3+
using Microsoft.EntityFrameworkCore;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Ecommerce.Modules.Carts.Core.Events.External.Handlers
11+
{
12+
internal class ProductRemovedFromOrderHandler : IEventHandler<ProductRemovedFromOrder>
13+
{
14+
private readonly ICartsDbContext _dbContext;
15+
16+
public ProductRemovedFromOrderHandler(ICartsDbContext dbContext)
17+
{
18+
_dbContext = dbContext;
19+
}
20+
public async Task HandleAsync(ProductRemovedFromOrder @event)
21+
{
22+
var product = await _dbContext.Products.FirstOrDefaultAsync(p => p.Id == @event.ProductId);
23+
24+
if (product is not null && product.HasQuantity)
25+
{
26+
product.IncreaseQuantity(@event.Quantity);
27+
await _dbContext.SaveChangesAsync();
28+
}
29+
}
30+
}
31+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Ecommerce.Shared.Abstractions.Events;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Ecommerce.Modules.Carts.Core.Events.External
9+
{
10+
public record class ProductAddedToOrder(Guid ProductId, int Quantity) : IEvent;
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Ecommerce.Shared.Abstractions.Events;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Ecommerce.Modules.Carts.Core.Events.External
9+
{
10+
public record class ProductRemovedFromOrder(Guid ProductId, int Quantity) : IEvent;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Ecommerce.Modules.Inventory.Domain.Auctions.Repositories;
2+
using Ecommerce.Modules.Inventory.Domain.Inventory.Repositories;
3+
using Ecommerce.Shared.Abstractions.Events;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Ecommerce.Modules.Inventory.Application.Inventory.Events.Externals.Handlers
11+
{
12+
internal class ProductAddedToOrderHandler : IEventHandler<ProductAddedToOrder>
13+
{
14+
private readonly IProductRepository _productRepository;
15+
private readonly IAuctionRepository _auctionRepository;
16+
17+
public ProductAddedToOrderHandler(IProductRepository productRepository, IAuctionRepository auctionRepository)
18+
{
19+
_productRepository = productRepository;
20+
_auctionRepository = auctionRepository;
21+
}
22+
public async Task HandleAsync(ProductAddedToOrder @event)
23+
{
24+
var product = await _productRepository.GetAsync(@event.ProductId);
25+
var auction = await _auctionRepository.GetAsync(@event.ProductId);
26+
27+
if (product is not null && product.HasQuantity)
28+
{
29+
product.DecreaseQuantity(@event.Quantity);
30+
}
31+
32+
if (auction is not null && auction.HasQuantity)
33+
{
34+
auction.DecreaseQuantity(@event.Quantity);
35+
}
36+
37+
await _productRepository.UpdateAsync();
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Ecommerce.Modules.Inventory.Domain.Auctions.Repositories;
2+
using Ecommerce.Modules.Inventory.Domain.Inventory.Repositories;
3+
using Ecommerce.Shared.Abstractions.Events;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace Ecommerce.Modules.Inventory.Application.Inventory.Events.Externals.Handlers
11+
{
12+
internal class ProductRemovedFromOrderHandler : IEventHandler<ProductRemovedFromOrder>
13+
{
14+
private readonly IProductRepository _productRepository;
15+
private readonly IAuctionRepository _auctionRepository;
16+
17+
public ProductRemovedFromOrderHandler(IProductRepository productRepository, IAuctionRepository auctionRepository)
18+
{
19+
_productRepository = productRepository;
20+
_auctionRepository = auctionRepository;
21+
}
22+
public async Task HandleAsync(ProductRemovedFromOrder @event)
23+
{
24+
var product = await _productRepository.GetAsync(@event.ProductId);
25+
var auction = await _auctionRepository.GetAsync(@event.ProductId);
26+
27+
if (product is not null && product.HasQuantity)
28+
{
29+
product.IncreaseQuantity(@event.Quantity);
30+
}
31+
32+
if (auction is not null && auction.HasQuantity)
33+
{
34+
auction.IncreaseQuantity(@event.Quantity);
35+
}
36+
37+
await _productRepository.UpdateAsync();
38+
}
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Ecommerce.Shared.Abstractions.Events;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Ecommerce.Modules.Inventory.Application.Inventory.Events.Externals
9+
{
10+
public record class ProductAddedToOrder(Guid ProductId, int Quantity) : IEvent;
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Ecommerce.Shared.Abstractions.Events;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Ecommerce.Modules.Inventory.Application.Inventory.Events.Externals
9+
{
10+
public record class ProductRemovedFromOrder(Guid ProductId, int Quantity) : IEvent;
11+
}

Ecommerce/src/Modules/Orders/Ecommerce.Modules.Orders.Api/Controllers/OrdersController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public async Task<ActionResult<ApiResponse<OrderDetailsDto>>> GetOrder([FromRout
8282
=> OkOrNotFound<OrderDetailsDto, Order>(await _mediator.Send(new GetOrder(orderId), cancellationToken), orderId.ToString());
8383

8484
[Authorize(Roles = "Admin, Manager, Employee")]
85-
[SwaggerOperation("Creates an order")]
86-
[SwaggerResponse(StatusCodes.Status201Created, "Creates an order and returns it's identifier", typeof(Guid))]
85+
[SwaggerOperation("Creates a draft order")]
86+
[SwaggerResponse(StatusCodes.Status201Created, "Creates a draft order and returns it's identifier", typeof(Guid))]
8787
[SwaggerResponse(StatusCodes.Status403Forbidden, "Access is forbidden for this user")]
8888
[SwaggerResponse(StatusCodes.Status401Unauthorized, "User not authorized")]
8989
[HttpPost]

Ecommerce/src/Modules/Orders/Ecommerce.Modules.Orders.Application/Orders/DTO/OrderDetailsDto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class OrderDetailsDto
1313
public CustomerDto? Customer { get; set; } = new();
1414
public decimal? TotalSum { get; set; }
1515
public IEnumerable<ProductDto>? Products { get; set; } = [];
16-
public string? Payment { get; set; } = string.Empty;
17-
public string? Status { get; set; } = string.Empty;
16+
public string? Payment { get; set; }
17+
public string Status { get; set; } = string.Empty;
1818
public string? ClientAdditionalInformation { get; set; }
1919
public string? CompanyAdditionalInformation { get; set; }
2020
public OrderInvoiceDto? Invoice { get; set; }

0 commit comments

Comments
 (0)