-
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.
Legacy - Mediator Behaviors, Entities, Domain proj (2024-06-20) (#44)
* added Legacy Domain project as dependency 🫠 * standard mediator pipeline behaviors * migrate data command, cmd response, and behavior * every domain has a constants file, right, right!? * removed migrations * entity changes * introduced Promotion + config * further Legacy entity changes * further Legacy changes! * chores; relational/EF Core fixes, disabled data seeding for now
- Loading branch information
1 parent
fd16213
commit 717c943
Showing
41 changed files
with
469 additions
and
1,431 deletions.
There are no files selected for viewing
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
src/Legacy/Legacy.Api/Infrastructure/Behaviors/LoggingBehavior.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 MediatR.Pipeline; | ||
|
||
namespace Legacy.Api.Infrastructure.Behaviors; | ||
|
||
public class LoggingBehavior<TRequest> : IRequestPreProcessor<TRequest> | ||
where TRequest : notnull | ||
{ | ||
private readonly ILogger<TRequest> _logger; | ||
|
||
public LoggingBehavior(ILoggerFactory logger) | ||
{ | ||
_logger = logger.CreateLogger<TRequest>(); | ||
} | ||
|
||
public Task Process(TRequest request, CancellationToken ct) | ||
{ | ||
var requestName = typeof(TRequest).Name; | ||
|
||
_logger.LogInformation("Request: {Name} {@Request}", requestName, request); | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Legacy/Legacy.Api/Infrastructure/Behaviors/PerformanceBehavior.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,38 @@ | ||
using System.Diagnostics; | ||
using MediatR; | ||
|
||
namespace Legacy.Api.Infrastructure.Behaviors; | ||
|
||
public class PerformanceBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : notnull | ||
{ | ||
private readonly ILogger<TRequest> _logger; | ||
private readonly Stopwatch _timer; | ||
|
||
public PerformanceBehavior(ILoggerFactory logger) | ||
{ | ||
_logger = logger.CreateLogger<TRequest>(); | ||
_timer = new Stopwatch(); | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken ct) | ||
{ | ||
_timer.Start(); | ||
|
||
var response = await next(); | ||
|
||
_timer.Stop(); | ||
|
||
var elapsedMilliseconds = _timer.ElapsedMilliseconds; | ||
|
||
if (elapsedMilliseconds <= 500) | ||
return response; | ||
|
||
var requestName = typeof(TRequest).Name; | ||
|
||
_logger.LogWarning("Long Running Request: {Name} ({ElapsedMilliseconds} milliseconds) {@Request}", | ||
requestName, elapsedMilliseconds, request); | ||
|
||
return response; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Legacy/Legacy.Api/Infrastructure/Behaviors/UnhandledExceptionBehavior.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,32 @@ | ||
using MediatR; | ||
|
||
namespace Legacy.Api.Infrastructure.Behaviors; | ||
|
||
public class UnhandledExceptionBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> | ||
where TRequest : notnull | ||
{ | ||
private readonly ILogger<TRequest> _logger; | ||
|
||
public UnhandledExceptionBehavior(ILoggerFactory logger) | ||
{ | ||
_logger = logger.CreateLogger<TRequest>(); | ||
} | ||
|
||
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken ct) | ||
{ | ||
try | ||
{ | ||
return await next(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
var requestName = typeof(TRequest).Name; | ||
|
||
_logger.LogError(ex, | ||
"Request: Unhandled Exception for Request {Name} {@Request}", | ||
requestName, request); | ||
|
||
throw; | ||
} | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
src/Legacy/Legacy.Api/UseCases/Database/MigrateDataResponse.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,3 @@ | ||
namespace Legacy.Api.UseCases.Database; | ||
|
||
public record MigrateDataResponse(); |
10 changes: 10 additions & 0 deletions
10
src/Legacy/Legacy.Api/UseCases/Database/MigrateDatabaseCommand.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,10 @@ | ||
using MediatR; | ||
|
||
namespace Legacy.Api.UseCases.Database; | ||
|
||
public record MigrateDatabaseCommand : IRequest<MigrateDataResponse> | ||
{ | ||
public static Func<IMediator, Task<MigrateDataResponse>> EndpointHandler | ||
=> mediator | ||
=> mediator.Send(new MigrateDatabaseCommand()); | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Legacy/Legacy.Api/UseCases/Database/MigrateDatabaseCommandHandler.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,35 @@ | ||
using Legacy.Data.DbContexts; | ||
using MediatR; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Legacy.Api.UseCases.Database; | ||
|
||
public class MigrateDatabaseCommandHandler : IRequestHandler<MigrateDatabaseCommand, MigrateDataResponse> | ||
{ | ||
private readonly CatalogDbContext _catalogDbContext; | ||
private readonly InventoryDbContext _inventoryDbContext; | ||
private readonly ListingDbContext _listingDbContext; | ||
private readonly OrderingDbContext _orderingDbContext; | ||
|
||
public MigrateDatabaseCommandHandler( | ||
CatalogDbContext catalogDbContext, | ||
InventoryDbContext inventoryDbContext, | ||
ListingDbContext listingDbContext, | ||
OrderingDbContext orderingDbContext) | ||
{ | ||
_catalogDbContext = catalogDbContext; | ||
_inventoryDbContext = inventoryDbContext; | ||
_listingDbContext = listingDbContext; | ||
_orderingDbContext = orderingDbContext; | ||
} | ||
|
||
public async Task<MigrateDataResponse> Handle(MigrateDatabaseCommand command, CancellationToken ct) | ||
{ | ||
await _catalogDbContext.Database.MigrateAsync(ct); | ||
await _inventoryDbContext.Database.MigrateAsync(ct); | ||
await _listingDbContext.Database.MigrateAsync(ct); | ||
await _orderingDbContext.Database.MigrateAsync(ct); | ||
|
||
return new MigrateDataResponse(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Legacy.Data.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace Legacy.Data.Configurations; | ||
|
||
public class CartConfig : IEntityTypeConfiguration<Cart> | ||
{ | ||
public void Configure(EntityTypeBuilder<Cart> builder) | ||
{ | ||
builder.HasKey(e => e.Id); | ||
|
||
builder.Property(e => e.IsLocked) | ||
.IsRequired(true) | ||
.HasDefaultValue(false); | ||
|
||
builder.Property(e => e.CustomerId); | ||
builder.HasOne<Customer>(e => e.Customer); | ||
|
||
builder.Property(e => e.DeliveryAddressId); | ||
builder.HasOne<Address>(e => e.DeliveryAddress); | ||
} | ||
} |
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,35 @@ | ||
using Legacy.Data.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace Legacy.Data.Configurations; | ||
|
||
public class CustomerConfig : IEntityTypeConfiguration<Customer> | ||
{ | ||
public void Configure(EntityTypeBuilder<Customer> builder) | ||
{ | ||
builder.HasKey(e => e.Id); | ||
|
||
builder.Property(e => e.FirstName) | ||
.IsRequired() | ||
.HasMaxLength(128); | ||
|
||
builder.Property(e => e.LastName) | ||
.IsRequired() | ||
.HasMaxLength(128); | ||
|
||
builder.Property(e => e.Email) | ||
.IsRequired() | ||
.HasMaxLength(255); | ||
|
||
builder.Property(e => e.ShippingAddressId); | ||
builder.HasOne<Address>(e => e.ShippingAddress) | ||
.WithMany() | ||
.OnDelete(DeleteBehavior.Restrict);; | ||
|
||
builder.Property(e => e.BillingAddressId); | ||
builder.HasOne<Address>(e => e.BillingAddress) | ||
.WithMany() | ||
.OnDelete(DeleteBehavior.Restrict);; | ||
} | ||
} |
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
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,17 @@ | ||
using Legacy.Data.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
|
||
namespace Legacy.Data.Configurations; | ||
|
||
public class PromotionConfig : IEntityTypeConfiguration<Promotion> | ||
{ | ||
public void Configure(EntityTypeBuilder<Promotion> builder) | ||
{ | ||
builder.HasKey(e => e.Id); | ||
|
||
|
||
builder.Property(e => e.ClaimedByCustomerId); | ||
builder.HasOne<Customer>(e => e.ClaimedByCustomer); | ||
} | ||
} |
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
Oops, something went wrong.