Skip to content

Commit

Permalink
get orders (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
behdad088 authored Oct 12, 2024
1 parent 3bdaa57 commit 59ea581
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/BuildingBlocks/BuildingBlocks/Pagination/PaginatedItems.cs
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;
}
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);
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();
}
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public async Task<GetOrdersByNameResult> Handle(GetOrdersByNameQuery query, Canc
{
var orders = await dbContext.Orders
.Include(x => x.OrderItems)
.AsNoTracking()
.Where(x => x.OrderName.Value == query.Name)
.OrderBy(x => x.OrderName)
.OrderBy(x => x.OrderName.Value)
.ToListAsync(cancellationToken);

var result = MapResult(orders);
Expand Down

0 comments on commit 59ea581

Please sign in to comment.