-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/BuildingBlocks/BuildingBlocks/Pagination/PaginatedItems.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace BuildingBlocks.Pagination; | ||
|
||
public class PaginatedItems<TEntity>(int pageIndex, int pageSize, long count, IEnumerable<TEntity> data) where TEntity : class | ||
{ | ||
[JsonPropertyName("page_index")] | ||
public int PageIndex { get; } = pageIndex; | ||
|
||
[JsonPropertyName("page_size")] | ||
public int PageSize { get; } = pageSize; | ||
|
||
[JsonPropertyName("count")] | ||
public long Count { get; } = count; | ||
|
||
[JsonPropertyName("data")] | ||
public IEnumerable<TEntity> Data { get; } = data; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/BuildingBlocks/BuildingBlocks/Pagination/PaginationRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace BuildingBlocks.Pagination; | ||
|
||
public record PaginationRequest( | ||
[FromQuery(Name = "page_size")] | ||
int PageSize = 10, | ||
[FromQuery( Name = "page_index")] | ||
int PageIndex = 0); |
49 changes: 49 additions & 0 deletions
49
...r.Command.Application/Orders/Queries/GetOrderByCustomer/GetOrderByCustomerQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace Order.Command.Application.Orders.Queries.GetOrderByCustomer; | ||
|
||
public record GetOrderByCustomerQuery(Guid CustomerId) : IQuery<GetOrderByCustomerResult>; | ||
|
||
public record GetOrderByCustomerResult(IEnumerable<OrderDto> Orders); | ||
|
||
public class GetOrderByCustomerQueryHandler(IApplicationDbContext dbContext) | ||
: IQueryHandler<GetOrderByCustomerQuery, GetOrderByCustomerResult> | ||
{ | ||
public async Task<GetOrderByCustomerResult> Handle(GetOrderByCustomerQuery query, | ||
CancellationToken cancellationToken) | ||
{ | ||
var orders = await dbContext.Orders | ||
.Include(x => x.OrderItems) | ||
.AsNoTracking() | ||
.Where(x => x.CustomerId!.Value == query.CustomerId) | ||
.OrderBy(x => x.OrderName.Value) | ||
.ToListAsync(cancellationToken); | ||
|
||
var result = MapResult(orders); | ||
return new GetOrderByCustomerResult(result); | ||
} | ||
|
||
private static OrderDto[] MapResult(IReadOnlyCollection<Domain.Models.Order> orders) | ||
{ | ||
var result = orders.Select(x => new OrderDto( | ||
x.Id.Value, | ||
x.CustomerId!.Value, | ||
x.OrderName.Value, | ||
MapAddress(x.ShippingAddress), | ||
MapAddress(x.BillingAddress), | ||
MapPayment(x.Payment), | ||
x.Status.Value, | ||
MapOrderItems(x.OrderItems))).ToArray(); | ||
|
||
return result; | ||
} | ||
|
||
private static AddressDto MapAddress(Address address) => | ||
new(address.FirstName, address.LastName, address.EmailAddress, address.AddressLine, address.Country, | ||
address.State, address.ZipCode); | ||
|
||
private static PaymentDto MapPayment(Payment payment) => | ||
new(payment.CardName, payment.CardNumber, payment.Expiration, payment.CVV, payment.PaymentMethod); | ||
|
||
private static List<OrderItemsDto> MapOrderItems(IReadOnlyCollection<OrderItem> orderItems) => | ||
orderItems.Select(x => | ||
new OrderItemsDto(x.Id.Value, x.OrderId.Value, x.ProductId.Value, x.Quantity, x.Price.Value)).ToList(); | ||
} |
58 changes: 58 additions & 0 deletions
58
...Order.Command/Order.Command.Application/Orders/Queries/GetOrders/GetOrdersQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using BuildingBlocks.Pagination; | ||
|
||
namespace Order.Command.Application.Orders.Queries.GetOrders; | ||
|
||
public record GetOrdersQuery(PaginationRequest PaginationRequest) : IQuery<GetOrdersResult>; | ||
public record GetOrdersResult(PaginatedItems<OrderDto> Orders); | ||
|
||
public class GetOrdersQueryHandler(IApplicationDbContext dbContext) : IQueryHandler<GetOrdersQuery, GetOrdersResult> | ||
{ | ||
public async Task<GetOrdersResult> Handle(GetOrdersQuery request, CancellationToken cancellationToken) | ||
{ | ||
var pageIndex = request.PaginationRequest.PageIndex; | ||
var pageSize = request.PaginationRequest.PageSize; | ||
var totalCount = await dbContext.Orders.LongCountAsync(cancellationToken); | ||
|
||
var orders = await dbContext.Orders | ||
.Include(x => x.OrderItems) | ||
.AsNoTracking() | ||
.OrderBy(x => x.OrderName.Value) | ||
.Skip(pageSize * pageIndex) | ||
.Take(pageSize) | ||
.ToListAsync(cancellationToken); | ||
|
||
var orderItems = MapResult(orders, pageIndex, pageSize, totalCount); | ||
|
||
return new GetOrdersResult(orderItems); | ||
} | ||
|
||
private static PaginatedItems<OrderDto> MapResult( | ||
IReadOnlyCollection<Domain.Models.Order> orders, | ||
int pageIndex, | ||
int pageSize, | ||
long totalCount) | ||
{ | ||
var ordersDto = orders.Select(x => new OrderDto( | ||
x.Id.Value, | ||
x.CustomerId!.Value, | ||
x.OrderName.Value, | ||
MapAddress(x.ShippingAddress), | ||
MapAddress(x.BillingAddress), | ||
MapPayment(x.Payment), | ||
x.Status.Value, | ||
MapOrderItems(x.OrderItems))).ToArray(); | ||
|
||
return new PaginatedItems<OrderDto>(pageIndex, pageSize, totalCount, ordersDto); | ||
} | ||
|
||
private static AddressDto MapAddress(Address address) => | ||
new(address.FirstName, address.LastName, address.EmailAddress, address.AddressLine, address.Country, | ||
address.State, address.ZipCode); | ||
|
||
private static PaymentDto MapPayment(Payment payment) => | ||
new(payment.CardName, payment.CardNumber, payment.Expiration, payment.CVV, payment.PaymentMethod); | ||
|
||
private static List<OrderItemsDto> MapOrderItems(IReadOnlyCollection<OrderItem> orderItems) => | ||
orderItems.Select(x => | ||
new OrderItemsDto(x.Id.Value, x.OrderId.Value, x.ProductId.Value, x.Quantity, x.Price.Value)).ToList(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters