Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ private static HttpRequestMessage CreateRequestMessage(
else
{
#endif
message.Headers.Accept.Clear();
foreach (var contentType in request.Accept)
{
message.Headers.Accept.Add(contentType);
}
message.Headers.Accept.Clear();
foreach (var contentType in request.Accept)
{
message.Headers.Accept.Add(contentType);
}
#if FUSION
}
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Buffers;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Runtime.CompilerServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private static OperationExecutionNode CreateOperationExecutionNode(
List<string>? variableBuffer)
{
var requirements = operationStep.Requirements.IsEmpty
? Array.Empty<OperationRequirement>()
? []
: operationStep.Requirements.OrderBy(t => t.Key).Select(t => t.Value).ToArray();

var forwardedVariables = Array.Empty<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ public async ValueTask<ProcessPaymentResult> HandleAsync(
var invoice = await db.Invoices.FirstOrDefaultAsync(
i => i.Id == command.InvoiceId, cancellationToken);
if (invoice is null)
{
return new ProcessPaymentResult(false, Error: "Invoice not found");
}

if (invoice.Status == InvoiceStatus.Paid)
{
return new ProcessPaymentResult(false, Error: "Invoice already paid");
}

var payment = new Payment
{
Expand Down
1 change: 0 additions & 1 deletion src/Mocha/src/Demo/Demo.Billing/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Demo.Billing.Data;
using Demo.Billing.Handlers;
using Demo.Billing.Queries;
using Demo.Contracts.Events;
using Microsoft.EntityFrameworkCore;
using Mocha;
using Mocha.EntityFrameworkCore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Demo.Catalog.Entities;
using Demo.Contracts.Commands;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Mocha;
using Mocha.Mediator;

Expand Down Expand Up @@ -32,10 +31,14 @@ public async ValueTask<InitiateReturnResult> HandleAsync(
var order = await db.Orders.Include(o => o.Product)
.FirstOrDefaultAsync(o => o.Id == command.OrderId, cancellationToken);
if (order is null)
{
return new InitiateReturnResult(false, Error: "Order not found");
}

if (order.Status != OrderStatus.Delivered && order.Status != OrderStatus.Shipping)
{
return new InitiateReturnResult(false, Error: $"Order cannot be returned in status: {order.Status}");
}

logger.LogInformation("Creating return label for order {OrderId}", command.OrderId);

Expand All @@ -56,7 +59,9 @@ public async ValueTask<InitiateReturnResult> HandleAsync(
cancellationToken);

if (!labelResponse.Success)
{
return new InitiateReturnResult(false, Error: $"Failed to create return label: {labelResponse.FailureReason}");
}

order.Status = OrderStatus.ReturnInitiated;
order.UpdatedAt = DateTimeOffset.UtcNow;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Demo.Contracts.Events;
using Microsoft.Extensions.Logging;
using Mocha;
using Mocha.Mediator;

Expand Down
4 changes: 4 additions & 0 deletions src/Mocha/src/Demo/Demo.Catalog/Commands/PlaceOrderCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ public async ValueTask<PlaceOrderResult> HandleAsync(

var product = await db.Products.FindAsync(command.ProductId);
if (product is null)
{
return new PlaceOrderResult(false, Error: "Product not found");
}

if (product.StockQuantity < command.Quantity)
{
return new PlaceOrderResult(false, Error: "Insufficient stock");
}

var order = new OrderRecord
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Demo.Catalog.Data;
using Demo.Catalog.Entities;
using Demo.Contracts.Saga;
using Microsoft.Extensions.Logging;
using Mocha;
using Mocha.Mediator;

Expand All @@ -28,7 +27,9 @@ public async ValueTask<RequestQuickRefundResult> HandleAsync(
{
var order = await db.Orders.FindAsync([command.OrderId], cancellationToken);
if (order is null)
{
return new RequestQuickRefundResult(false, Error: "Order not found");
}

logger.LogInformation("Initiating quick refund saga for order {OrderId}", command.OrderId);

Expand Down
11 changes: 8 additions & 3 deletions src/Mocha/src/Demo/Demo.Catalog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
using Demo.Catalog.Handlers;
using Demo.Catalog.Queries;
using Demo.Catalog.Sagas;
using Demo.Contracts.Commands;
using Demo.Contracts.Events;
using Demo.Contracts.Saga;
using Microsoft.EntityFrameworkCore;
using Mocha;
using Mocha.EntityFrameworkCore;
Expand Down Expand Up @@ -118,7 +115,9 @@ await sender.QueryAsync(new GetOrderByIdQuery(id)) is { } order
new RequestQuickRefundCommand(request.OrderId, request.Amount, request.Reason));

if (!result.Success)
{
return result.Error == "Order not found" ? Results.NotFound(result.Error) : Results.Problem(result.Error);
}

return Results.Ok(result.Response);
});
Expand All @@ -132,9 +131,15 @@ await sender.QueryAsync(new GetOrderByIdQuery(id)) is { } order
if (!result.Success)
{
if (result.Error!.Contains("not found"))
{
return Results.NotFound(result.Error);
}

if (result.Error.Contains("cannot be returned"))
{
return Results.BadRequest(result.Error);
}

return Results.Problem(result.Error);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Demo.Shipping.Data;
using Demo.Shipping.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Mocha;
using Mocha.Mediator;

Expand All @@ -27,10 +26,14 @@ public async ValueTask<ReceiveReturnPackageResult> HandleAsync(
var returnShipment = await db.ReturnShipments.FirstOrDefaultAsync(
r => r.Id == command.ReturnId, cancellationToken);
if (returnShipment is null)
{
return new ReceiveReturnPackageResult(false, Error: "Return shipment not found");
}

if (returnShipment.Status == ReturnShipmentStatus.Received)
{
return new ReceiveReturnPackageResult(false, Error: "Return package already received");
}

returnShipment.Status = ReturnShipmentStatus.Received;
returnShipment.ReceivedAt = DateTimeOffset.UtcNow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ public async ValueTask<ShipShipmentResult> HandleAsync(
var shipment = await db.Shipments.FirstOrDefaultAsync(
s => s.Id == command.ShipmentId, cancellationToken);
if (shipment is null)
{
return new ShipShipmentResult(false, Error: "Shipment not found");
}

if (shipment.Status == ShipmentStatus.Shipped)
{
return new ShipShipmentResult(false, Error: "Shipment already shipped");
}

shipment.Status = ShipmentStatus.Shipped;
shipment.Carrier = command.Carrier;
Expand Down
2 changes: 0 additions & 2 deletions src/Mocha/src/Demo/Demo.Shipping/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Demo.Contracts.Events;
using Demo.Shipping.Commands;
using Demo.Shipping.Data;
using Demo.Shipping.Handlers;
using Demo.Shipping.Queries;
using Microsoft.EntityFrameworkCore;
using Mocha;
using Mocha.EntityFrameworkCore;
using Mocha.Mediator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class PostgresTransportConfiguration : MessagingTransportConfiguration
public PostgresTransportConfiguration()
{
Name = DefaultName;
base.Schema = DefaultSchema;
Schema = DefaultSchema;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Text;

namespace Mocha.Transport.Postgres;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ protected override void OnAfterInitialized(IMessagingSetupContext context)

var builder = new UriBuilder
{
Scheme = Schema, Host = configuration.Host, Port = configuration.Port, Path = "/"
Scheme = Schema,
Host = configuration.Host,
Port = configuration.Port,
Path = "/"
};
_topology = new PostgresMessagingTopology(
this,
Expand Down Expand Up @@ -225,7 +228,8 @@ public override TransportDescription Describe()
"outbound",
new Dictionary<string, object?>
{
["autoDelete"] = queue.AutoDelete, ["autoProvision"] = queue.AutoProvision
["autoDelete"] = queue.AutoDelete,
["autoProvision"] = queue.AutoProvision
}));
}

Expand Down Expand Up @@ -352,15 +356,17 @@ protected override DispatchEndpoint CreateDispatchEndpoint()
var queueName = context.Naming.GetSendEndpointName(route.MessageType.RuntimeType);
configuration = new PostgresDispatchEndpointConfiguration
{
QueueName = queueName, Name = "q/" + queueName
QueueName = queueName,
Name = "q/" + queueName
};
}
else if (route.Kind == OutboundRouteKind.Publish)
{
var topicName = context.Naming.GetPublishEndpointName(route.MessageType.RuntimeType);
configuration = new PostgresDispatchEndpointConfiguration
{
TopicName = topicName, Name = "t/" + topicName
TopicName = topicName,
Name = "t/" + topicName
};
}

Expand All @@ -385,7 +391,9 @@ protected override DispatchEndpoint CreateDispatchEndpoint()
var instanceEndpointName = context.Naming.GetInstanceEndpoint(context.Host.InstanceId);
configuration = new PostgresDispatchEndpointConfiguration
{
Kind = DispatchEndpointKind.Reply, QueueName = instanceEndpointName, Name = "Replies"
Kind = DispatchEndpointKind.Reply,
QueueName = instanceEndpointName,
Name = "Replies"
};
}

Expand All @@ -398,15 +406,17 @@ protected override DispatchEndpoint CreateDispatchEndpoint()
{
configuration = new PostgresDispatchEndpointConfiguration
{
TopicName = new string(topicName), Name = "t/" + new string(topicName)
TopicName = new string(topicName),
Name = "t/" + new string(topicName)
};
}

if (kind is "q" && name is var queueName)
{
configuration = new PostgresDispatchEndpointConfiguration
{
QueueName = new string(queueName), Name = "q/" + new string(queueName)
QueueName = new string(queueName),
Name = "q/" + new string(queueName)
};
}
}
Expand All @@ -421,15 +431,17 @@ protected override DispatchEndpoint CreateDispatchEndpoint()
{
configuration = new PostgresDispatchEndpointConfiguration
{
TopicName = new string(topicName), Name = "t/" + new string(topicName)
TopicName = new string(topicName),
Name = "t/" + new string(topicName)
};
}

if (kind is "q" && name is var queueName)
{
configuration = new PostgresDispatchEndpointConfiguration
{
QueueName = new string(queueName), Name = "q/" + new string(queueName)
QueueName = new string(queueName),
Name = "q/" + new string(queueName)
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Mocha.Transport.Postgres.Tests.Helpers;
using Npgsql;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Mocha.Transport.Postgres.Tests.Helpers;
using Npgsql;

namespace Mocha.Transport.Postgres.Tests.Connection;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Microsoft.Extensions.DependencyInjection;
using Mocha.Transport.Postgres.Tests.Helpers;

namespace Mocha.Transport.Postgres.Tests;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Buffers;
using System.Collections.Immutable;
using System.Text.Json;
using Mocha.Middlewares;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,13 @@ private static PostgresMessageItem CreateMessageItem(
DateTime? sentTime = null,
int deliveryCount = 0)
{
ReadOnlyMemory<byte> headersBytes = headers is not null
var headersBytes = headers is not null
? Encoding.UTF8.GetBytes(JsonSerializer.Serialize(headers))
: ReadOnlyMemory<byte>.Empty;

return new PostgresMessageItem(
TransportMessageId: Guid.NewGuid(),
Body: body ?? Array.Empty<byte>(),
Body: body ?? [],
Headers: headersBytes,
QueueId: 1,
SentTime: sentTime ?? DateTime.UtcNow,
Expand Down
Loading