-
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.
order command api - add ConcurrencyToken
- Loading branch information
Showing
12 changed files
with
183 additions
and
17 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/Services/Order.Command/Order.Command.API/Endpoints/GetOrderById/Endpoint.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,37 @@ | ||
using Order.Command.Application.Orders.Queries.GetOrderById; | ||
|
||
namespace Order.Command.API.Endpoints.GetOrderById; | ||
|
||
public class Endpoint : EndpointBase<Request, Response> | ||
{ | ||
public override void MapEndpoint() | ||
{ | ||
Get("/orders/{id}", HandleAsync); | ||
Name("GetOrdersById"); | ||
Produces(); | ||
ProducesProblem(StatusCodes.Status400BadRequest); | ||
ProducesProblem(StatusCodes.Status404NotFound); | ||
Summary("Gets orders by Id."); | ||
Description("Gets orders by Id"); | ||
} | ||
|
||
public override async Task<IResult> HandleAsync(Request request) | ||
{ | ||
var query = MapToQuery(request); | ||
var result = await SendAsync(query).ConfigureAwait(false); | ||
Context.Response.Headers.ETag = $"W/\"{result.Order.Version}\""; | ||
|
||
var response = MapToResponse(result); | ||
return TypedResults.Ok(response); | ||
} | ||
|
||
private static GetOrdersByIdQuery MapToQuery(Request request) | ||
{ | ||
return new GetOrdersByIdQuery(request.Id); | ||
} | ||
|
||
private static Response MapToResponse(GetOrdersByIdResult result) | ||
{ | ||
return new Response(result.Order); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Services/Order.Command/Order.Command.API/Endpoints/GetOrderById/Models.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,11 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Order.Command.Application.Orders.Queries.GetOrderById; | ||
|
||
namespace Order.Command.API.Endpoints.GetOrderById; | ||
|
||
public record Request | ||
{ | ||
[FromRoute(Name = "id")] public string? Id { get; set; } | ||
} | ||
|
||
public record Response(GetOrderByIdDto Order); |
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
23 changes: 23 additions & 0 deletions
23
...es/Order.Command/Order.Command.Application/Orders/Queries/GetOrderById/GetOrderByIdDto.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,23 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Order.Command.Application.Orders.Queries.GetOrderById; | ||
|
||
public record GetOrderByIdDto( | ||
[property: JsonPropertyName("id")] | ||
Ulid Id, | ||
[property: JsonPropertyName("customer_id")] | ||
Guid? CustomerId, | ||
[property: JsonPropertyName("order_name")] | ||
string OrderName, | ||
[property: JsonPropertyName("shipping_Address")] | ||
AddressDto ShippingAddress, | ||
[property: JsonPropertyName("billing_address")] | ||
AddressDto BillingAddress, | ||
[property: JsonPropertyName("payment")] | ||
PaymentDto Payment, | ||
[property: JsonPropertyName("status")] | ||
string Status, | ||
[property: JsonPropertyName("version")] | ||
int Version, | ||
[property: JsonPropertyName("order_items")] | ||
List<OrderItems> OrderItems); |
62 changes: 62 additions & 0 deletions
62
...Command/Order.Command.Application/Orders/Queries/GetOrderById/GetOrderByIdQueryHandler.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,62 @@ | ||
using BuildingBlocks.Exceptions; | ||
using Order.Command.Application.Exceptions; | ||
|
||
namespace Order.Command.Application.Orders.Queries.GetOrderById; | ||
|
||
public record GetOrdersByIdQuery(string? Id) : IQuery<GetOrdersByIdResult>; | ||
|
||
public record GetOrdersByIdResult(GetOrderByIdDto Order); | ||
|
||
public class GetOrderByIdHandler(IApplicationDbContext dbContext) : IQueryHandler<GetOrdersByIdQuery, GetOrdersByIdResult> | ||
{ | ||
public async Task<GetOrdersByIdResult> Handle(GetOrdersByIdQuery request, CancellationToken cancellationToken) | ||
{ | ||
var orderId = Ulid.Parse(request.Id); | ||
var order = await dbContext.Orders | ||
.Include(x => x.OrderItems) | ||
.AsNoTracking() | ||
.FirstOrDefaultAsync(x => x.Id.Equals(OrderId.From(orderId)), cancellationToken); | ||
|
||
if (order is null) | ||
throw new OrderNotFoundExceptions(orderId); | ||
|
||
var result = MapResult(order); | ||
return new GetOrdersByIdResult(result); | ||
} | ||
|
||
private static GetOrderByIdDto MapResult(Domain.Models.Order order) | ||
{ | ||
var result = new GetOrderByIdDto( | ||
order.Id.Value, | ||
order.CustomerId.Value, | ||
order.OrderName.Value, | ||
MapAddress(order.ShippingAddress), | ||
MapAddress(order.BillingAddress), | ||
MapPayment(order.Payment), | ||
order.Status.Value, | ||
order.RowVersion.Value, | ||
MapOrderItems(order.OrderItems)); | ||
|
||
return result; | ||
} | ||
|
||
private static AddressDto MapAddress(Address address) | ||
{ | ||
return new AddressDto(address.FirstName, address.LastName, address.EmailAddress, address.AddressLine, | ||
address.Country, | ||
address.State, address.ZipCode); | ||
} | ||
|
||
private static PaymentDto MapPayment(Payment payment) | ||
{ | ||
return new PaymentDto(payment.CardName, payment.CardNumber, payment.Expiration, payment.CVV, | ||
payment.PaymentMethod); | ||
} | ||
|
||
private static List<OrderItems> MapOrderItems(IReadOnlyCollection<OrderItem> orderItems) | ||
{ | ||
return orderItems.Select(x => | ||
new OrderItems(x.Id.Value.ToString(), x.OrderId.Value.ToString(), x.ProductId.Value.ToString(), x.Quantity, | ||
x.Price.Value)).ToList(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...Services/Order.Command/Order.Command.Application/Orders/Queries/GetOrderById/Validator.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,11 @@ | ||
using FluentValidation; | ||
|
||
namespace Order.Command.Application.Orders.Queries.GetOrderById; | ||
|
||
public class Validator : AbstractValidator<GetOrdersByIdQuery> | ||
{ | ||
public Validator() | ||
{ | ||
RuleFor(x => x.Id).NotEmpty().Must(x => Ulid.TryParse(x, out _)).WithMessage("Invalid order id"); | ||
} | ||
} |
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
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
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
8 changes: 3 additions & 5 deletions
8
...ations/20241102205101_Initial.Designer.cs → ...ations/20241217213839_Initial.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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