save development progress
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Microservice;
|
||||
|
||||
namespace Examples.Billing.Microservice.Endpoints;
|
||||
|
||||
/// <summary>
|
||||
/// Request model for creating an invoice.
|
||||
/// </summary>
|
||||
public sealed record CreateInvoiceRequest
|
||||
{
|
||||
public required string CustomerId { get; init; }
|
||||
public required decimal Amount { get; init; }
|
||||
public string? Description { get; init; }
|
||||
public List<LineItem> LineItems { get; init; } = [];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Line item for an invoice.
|
||||
/// </summary>
|
||||
public sealed record LineItem
|
||||
{
|
||||
public required string Description { get; init; }
|
||||
public required decimal Amount { get; init; }
|
||||
public int Quantity { get; init; } = 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Response model after creating an invoice.
|
||||
/// </summary>
|
||||
public sealed record CreateInvoiceResponse
|
||||
{
|
||||
public required string InvoiceId { get; init; }
|
||||
public required DateTime CreatedAt { get; init; }
|
||||
public required string Status { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Endpoint for creating a new invoice.
|
||||
/// Demonstrates a typed endpoint with JSON request/response.
|
||||
/// </summary>
|
||||
[StellaEndpoint("POST", "/invoices", TimeoutSeconds = 30)]
|
||||
public sealed class CreateInvoiceEndpoint : IStellaEndpoint<CreateInvoiceRequest, CreateInvoiceResponse>
|
||||
{
|
||||
private readonly ILogger<CreateInvoiceEndpoint> _logger;
|
||||
|
||||
public CreateInvoiceEndpoint(ILogger<CreateInvoiceEndpoint> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task<CreateInvoiceResponse> HandleAsync(
|
||||
CreateInvoiceRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Creating invoice for customer {CustomerId} with amount {Amount}",
|
||||
request.CustomerId,
|
||||
request.Amount);
|
||||
|
||||
// Simulate invoice creation
|
||||
var invoiceId = $"INV-{Guid.NewGuid():N}".ToUpperInvariant()[..16];
|
||||
|
||||
return Task.FromResult(new CreateInvoiceResponse
|
||||
{
|
||||
InvoiceId = invoiceId,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
Status = "draft"
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Microservice;
|
||||
|
||||
namespace Examples.Billing.Microservice.Endpoints;
|
||||
|
||||
/// <summary>
|
||||
/// Request model for getting an invoice.
|
||||
/// </summary>
|
||||
public sealed record GetInvoiceRequest
|
||||
{
|
||||
public required string Id { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Response model for an invoice.
|
||||
/// </summary>
|
||||
public sealed record GetInvoiceResponse
|
||||
{
|
||||
public required string InvoiceId { get; init; }
|
||||
public required string CustomerId { get; init; }
|
||||
public required decimal Amount { get; init; }
|
||||
public required string Status { get; init; }
|
||||
public required DateTime CreatedAt { get; init; }
|
||||
public DateTime? PaidAt { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Endpoint for retrieving an invoice by ID.
|
||||
/// Demonstrates a GET endpoint with path parameters.
|
||||
/// </summary>
|
||||
[StellaEndpoint("GET", "/invoices/{id}", TimeoutSeconds = 10, RequiredClaims = ["invoices:read"])]
|
||||
public sealed class GetInvoiceEndpoint : IStellaEndpoint<GetInvoiceRequest, GetInvoiceResponse>
|
||||
{
|
||||
private readonly ILogger<GetInvoiceEndpoint> _logger;
|
||||
|
||||
public GetInvoiceEndpoint(ILogger<GetInvoiceEndpoint> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task<GetInvoiceResponse> HandleAsync(
|
||||
GetInvoiceRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Fetching invoice {InvoiceId}", request.Id);
|
||||
|
||||
// Simulate invoice lookup
|
||||
return Task.FromResult(new GetInvoiceResponse
|
||||
{
|
||||
InvoiceId = request.Id,
|
||||
CustomerId = "CUST-001",
|
||||
Amount = 199.99m,
|
||||
Status = "paid",
|
||||
CreatedAt = DateTime.UtcNow.AddDays(-7),
|
||||
PaidAt = DateTime.UtcNow.AddDays(-1)
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using StellaOps.Microservice;
|
||||
|
||||
namespace Examples.Billing.Microservice.Endpoints;
|
||||
|
||||
/// <summary>
|
||||
/// Endpoint for uploading attachments to an invoice.
|
||||
/// Demonstrates streaming upload using IRawStellaEndpoint.
|
||||
/// </summary>
|
||||
[StellaEndpoint("POST", "/invoices/{id}/attachments", SupportsStreaming = true, TimeoutSeconds = 300)]
|
||||
public sealed class UploadAttachmentEndpoint : IRawStellaEndpoint
|
||||
{
|
||||
private readonly ILogger<UploadAttachmentEndpoint> _logger;
|
||||
|
||||
public UploadAttachmentEndpoint(ILogger<UploadAttachmentEndpoint> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<RawResponse> HandleAsync(
|
||||
RawRequestContext context,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var invoiceId = context.PathParameters.GetValueOrDefault("id") ?? "unknown";
|
||||
|
||||
var contentType = context.Headers["Content-Type"] ?? "application/octet-stream";
|
||||
_logger.LogInformation(
|
||||
"Uploading attachment for invoice {InvoiceId}, Content-Type: {ContentType}",
|
||||
invoiceId,
|
||||
contentType);
|
||||
|
||||
// Read the streamed body
|
||||
long totalBytes = 0;
|
||||
var buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = await context.Body.ReadAsync(buffer, cancellationToken)) > 0)
|
||||
{
|
||||
totalBytes += bytesRead;
|
||||
// In a real implementation, you would write to storage here
|
||||
}
|
||||
|
||||
_logger.LogInformation(
|
||||
"Received {TotalBytes} bytes for invoice {InvoiceId}",
|
||||
totalBytes,
|
||||
invoiceId);
|
||||
|
||||
// Return success response
|
||||
var response = new
|
||||
{
|
||||
invoiceId,
|
||||
attachmentId = $"ATT-{Guid.NewGuid():N}"[..16].ToUpperInvariant(),
|
||||
size = totalBytes,
|
||||
uploadedAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
return RawResponse.Ok(JsonSerializer.Serialize(response));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user