Skip to content

Commit

Permalink
add outbox table to order command api (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
behdad088 authored Dec 25, 2024
1 parent 37f4ea3 commit 54db7a6
Show file tree
Hide file tree
Showing 37 changed files with 1,076 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,44 @@ public override async Task<IResult> HandleAsync(Request request)
ShippingAddress: MapAddress(request.ShippingAddress),
BillingAddress: MapAddress(request.BillingAddress),
OrderPayment: MapPayment(request.Payment),
OrderItems: request.OrderItems.Select(x =>
new OrderDto.OrderItem(
x.Id,
x.OrderId,
x.ProductId,
x.Quantity,
x.Price
)).ToList()));
OrderItems: MapOrderItems(request.OrderItems)));
}

private static OrderDto.Address MapAddress(AddressDto addressDto)
private static OrderDto.Address? MapAddress(AddressDto? addressDto)
{
return new OrderDto.Address(
Firstname: addressDto.Firstname,
Lastname: addressDto.Lastname,
EmailAddress: addressDto.EmailAddress,
AddressLine: addressDto.AddressLine,
Country: addressDto.Country,
State: addressDto.State,
ZipCode: addressDto.ZipCode);
return addressDto is null
? null
: new OrderDto.Address(
Firstname: addressDto.Firstname,
Lastname: addressDto.Lastname,
EmailAddress: addressDto.EmailAddress,
AddressLine: addressDto.AddressLine,
Country: addressDto.Country,
State: addressDto.State,
ZipCode: addressDto.ZipCode);
}

private static OrderDto.Payment MapPayment(PaymentDto paymentDto)
private static OrderDto.Payment? MapPayment(PaymentDto? paymentDto)
{
return new OrderDto.Payment(
return paymentDto is null ? null : new OrderDto.Payment(
paymentDto.CardName,
paymentDto.CardNumber,
paymentDto.Expiration,
paymentDto.Cvv,
paymentDto.PaymentMethod);
}

private static List<OrderDto.OrderItem>? MapOrderItems(List<OrderItemsDto>? orderItems)
{
return orderItems?.Select(x =>
new OrderDto.OrderItem(
x.Id,
x.OrderId,
x.ProductId,
x.Quantity,
x.Price
)).ToList();
}

private static Response MapResult(CreateOrderResult result)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ namespace Order.Command.API.Endpoints.CreateOrder;

public record Request
{
[property: JsonPropertyName("id")] public string Id { get; set; }
[property: JsonPropertyName("id")]
public string? Id { get; set; }

[property: JsonPropertyName("customer_id")]
public string CustomerId { get; set; }
public string? CustomerId { get; set; }

[property: JsonPropertyName("order_name")]
public string OrderName { get; set; }
public string? OrderName { get; set; }

[property: JsonPropertyName("shipping_Address")]
public AddressDto ShippingAddress { get; set; }
public AddressDto? ShippingAddress { get; set; }

[property: JsonPropertyName("billing_address")]
public AddressDto BillingAddress { get; set; }
public AddressDto? BillingAddress { get; set; }

[property: JsonPropertyName("payment")]
public PaymentDto Payment { get; set; }
public PaymentDto? Payment { get; set; }

[property: JsonPropertyName("order_items")]
public List<OrderItemsDto> OrderItems { get; set; }
public List<OrderItemsDto>? OrderItems { get; set; }
};

public record AddressDto(
Expand All @@ -34,7 +35,8 @@ public record AddressDto(
string AddressLine,
[property: JsonPropertyName("country")]
string Country,
[property: JsonPropertyName("state")] string State,
[property: JsonPropertyName("state")]
string State,
[property: JsonPropertyName("zip_code")]
string ZipCode);

Expand All @@ -57,7 +59,8 @@ public record OrderItemsDto(
string ProductId,
[property: JsonPropertyName("quantity")]
int? Quantity,
[property: JsonPropertyName("price")] decimal? Price);
[property: JsonPropertyName("price")]
decimal? Price);

public record Response(
[property: JsonPropertyName("order_id")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
using Order.Command.Application.Orders.Commands.DeleteOrder;
using Order.Command.Domain.Models.ValueObjects;

namespace Order.Command.API.Endpoints.DeleteOrder;

public class Endpoint : EndpointBase<Request, Response>
public class Endpoint : EndpointBase<Request>
{
public override void MapEndpoint()
{
Delete("/orders/{order_id}", HandleAsync);
Name("DeleteOrder");
Produces(StatusCodes.Status202Accepted);
Produces(StatusCodes.Status204NoContent);
ProducesProblem(StatusCodes.Status400BadRequest);
ProducesProblem(StatusCodes.Status404NotFound);
ProducesProblem(StatusCodes.Status412PreconditionFailed);
Summary("Delete an existing order.");
Description("Delete an existing order");
}

public override async Task<IResult> HandleAsync(Request request)
{
var command = MapToCommand(request);
var result = await SendAsync(command).ConfigureAwait(false);
var response = MapToResponse(result);
return TypedResults.Ok(response);
}
var eTag = Context.Request.Headers.IfMatch;

private static DeleteOrderCommand MapToCommand(Request request)
{
return new DeleteOrderCommand(request.OrderId);
var command = MapToCommand(request, eTag);
await SendAsync(command).ConfigureAwait(false);
return TypedResults.NoContent();
}

private static Response MapToResponse(DeleteOrderResult result)
private static DeleteOrderCommand MapToCommand(Request request, string? etag)
{
return new Response(result.IsSuccess);
return new DeleteOrderCommand(request.OrderId, etag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,5 @@ namespace Order.Command.API.Endpoints.DeleteOrder;

public record Request
{
[FromRoute(Name = "order_id")]
public string OrderId { get; set; }
}

public record Response(
[property: JsonPropertyName("is_success")]
bool IsSuccess);
[FromRoute(Name = "order_id")] public string? OrderId { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Order.Command.Application.Dtos;
using Order.Command.Application.Orders.Commands.UpdateOrder;

namespace Order.Command.API.Endpoints.UpdateOrder;
Expand All @@ -11,23 +12,64 @@ public override void MapEndpoint()
Produces(StatusCodes.Status204NoContent);
ProducesProblem(StatusCodes.Status400BadRequest);
ProducesProblem(StatusCodes.Status404NotFound);
ProducesProblem(StatusCodes.Status412PreconditionFailed);
Summary("Update an existing order.");
Description("Update an existing order");
}

public override async Task<IResult> HandleAsync(Request request)
{
var command = MapToCommand(request);
var result = await SendAsync(command).ConfigureAwait(false);
var response = MapToResponse(result);
return TypedResults.Ok(response);
var eTag = Context.Request.Headers.IfMatch;

var command = MapToCommand(request, eTag);
await SendAsync(command).ConfigureAwait(false);

return TypedResults.NoContent();
}

private static UpdateOrderCommand MapToCommand(Request request, string? eTag)
{
return new UpdateOrderCommand(new UpdateOrderDto(
request.Id,
request.CustomerId,
request.OrderName,
MapAddress(request.ShippingAddress),
MapAddress(request.BillingAddress),
MapPayment(request.Payment),
request.Status,
eTag,
request.OrderItems.Select(x => new OrderItems(
x.Id,
x.OrderId,
x.ProductId,
x.Quantity,
x.Price)).ToList()
));
}

private static UpdateOrderCommand MapToCommand(Request request)
private static AddressDto MapAddress(Address addressDto)
{
return new UpdateOrderCommand(request.Order);
return new AddressDto(
Firstname: addressDto.Firstname,
Lastname: addressDto.Lastname,
EmailAddress: addressDto.EmailAddress,
AddressLine: addressDto.AddressLine,
Country: addressDto.Country,
State: addressDto.State,
ZipCode: addressDto.ZipCode);
}

private static PaymentDto MapPayment(Payment paymentDto)
{
return new PaymentDto(
paymentDto.CardName,
paymentDto.CardNumber,
paymentDto.Expiration,
paymentDto.Cvv,
paymentDto.PaymentMethod);
}


private static Response MapToResponse(UpdateOrderResult result)
{
return new Response(result.IsSuccess);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,65 @@ namespace Order.Command.API.Endpoints.UpdateOrder;

public record Request
{
[FromBody] public UpdateOrderDto Order { get; set; }
[property: JsonPropertyName("id")] public string Id { get; set; }

Check warning on line 8 in src/Services/Order.Command/Order.Command.API/Endpoints/UpdateOrder/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

[property: JsonPropertyName("customer_id")]
public string? CustomerId { get; set; }

[property: JsonPropertyName("order_name")]
public string OrderName { get; set; }

Check warning on line 14 in src/Services/Order.Command/Order.Command.API/Endpoints/UpdateOrder/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'OrderName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

[property: JsonPropertyName("shipping_Address")]
public Address ShippingAddress { get; set; }

Check warning on line 17 in src/Services/Order.Command/Order.Command.API/Endpoints/UpdateOrder/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ShippingAddress' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

[property: JsonPropertyName("billing_address")]
public Address BillingAddress { get; set; }

Check warning on line 20 in src/Services/Order.Command/Order.Command.API/Endpoints/UpdateOrder/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'BillingAddress' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

[property: JsonPropertyName("payment")]
public Payment Payment { get; set; }

Check warning on line 23 in src/Services/Order.Command/Order.Command.API/Endpoints/UpdateOrder/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Payment' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

[property: JsonPropertyName("status")] public string Status { get; set; }

Check warning on line 25 in src/Services/Order.Command/Order.Command.API/Endpoints/UpdateOrder/Models.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Status' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

[property: JsonPropertyName("order_items")]
public List<OrderItem> OrderItems { get; set; }
}

public record Address(
[property: JsonPropertyName("firstname")]
string Firstname,
[property: JsonPropertyName("lastname")]
string Lastname,
[property: JsonPropertyName("email_address")]
string EmailAddress,
[property: JsonPropertyName("address_line")]
string AddressLine,
[property: JsonPropertyName("country")]
string Country,
[property: JsonPropertyName("state")] string State,
[property: JsonPropertyName("zip_code")]
string ZipCode);

public record Payment(
[property: JsonPropertyName("card_name")]
string CardName,
[property: JsonPropertyName("card_number")]
string CardNumber,
[property: JsonPropertyName("expiration")]
string Expiration,
[property: JsonPropertyName("cvv")] string Cvv,
[property: JsonPropertyName("payment_method")]
int PaymentMethod);

public record OrderItem(
[property: JsonPropertyName("id")] string Id,
[property: JsonPropertyName("order_id")]
string OrderId,
[property: JsonPropertyName("product_id")]
string ProductId,
[property: JsonPropertyName("quantity")]
int? Quantity,
[property: JsonPropertyName("price")] decimal? Price);

public record Response(
[property: JsonPropertyName("success")]
bool IsSuccess);
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ public interface IApplicationDbContext
{
DbSet<Domain.Models.Order> Orders { get; }
DbSet<OrderItem> OrderItems { get; }

DbSet<Outbox> Outboxes { get; }

DatabaseFacade Database { get; }

Task<int> SaveChangesAsync(CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Order.Command.Application.Dtos;

public record UpdateOrderDto(
[property: JsonPropertyName("id")] Ulid Id,
[property: JsonPropertyName("id")] string Id,
[property: JsonPropertyName("customer_id")]
Guid? CustomerId,
string? CustomerId,
[property: JsonPropertyName("order_name")]
string OrderName,
[property: JsonPropertyName("shipping_Address")]
Expand All @@ -15,5 +15,6 @@ public record UpdateOrderDto(
[property: JsonPropertyName("payment")]
PaymentDto Payment,
[property: JsonPropertyName("status")] string Status,
[property: JsonPropertyName("etag")] string? Version,
[property: JsonPropertyName("order_items")]
List<OrderItems> OrderItems);
Loading

0 comments on commit 54db7a6

Please sign in to comment.