Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomExceptionHandler switch more readable and remove duplicate code #93

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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 @@ -23,13 +23,7 @@ public async ValueTask<bool> TryHandleAsync(HttpContext context, Exception excep
exception.GetType().Name,
context.Response.StatusCode = StatusCodes.Status500InternalServerError
),
ValidationException =>
(
exception.Message,
exception.GetType().Name,
context.Response.StatusCode = StatusCodes.Status400BadRequest
),
BadRequestException =>
ValidationException or BadRequestException =>
(
exception.Message,
exception.GetType().Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@ public async Task<CreateProductResult> Handle(CreateProductCommand command, Canc
//create Product entity from command object
//save to database
//return CreateProductResult result

var product = new Product
{
Name = command.Name,
Category = command.Category,
Description = command.Description,
ImageFile = command.ImageFile,
Price = command.Price
};

var product = command.Adapt<Product>();
//save to database
session.Store(product);
await session.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static async Task InitialiseDatabaseAsync(this WebApplication app)

await SeedAsync(context);
}

private static async Task SeedAsync(ApplicationDbContext context)
{
await SeedCustomerAsync(context);
Expand All @@ -24,28 +24,23 @@ private static async Task SeedAsync(ApplicationDbContext context)

private static async Task SeedCustomerAsync(ApplicationDbContext context)
{
if (!await context.Customers.AnyAsync())
{
await context.Customers.AddRangeAsync(InitialData.Customers);
await context.SaveChangesAsync();
}
if (await context.Customers.AnyAsync()) return;
await context.Customers.AddRangeAsync(InitialData.Customers);
await context.SaveChangesAsync();
}

private static async Task SeedProductAsync(ApplicationDbContext context)
{
if (!await context.Products.AnyAsync())
{
await context.Products.AddRangeAsync(InitialData.Products);
await context.SaveChangesAsync();
}
if (await context.Products.AnyAsync()) return;
await context.Products.AddRangeAsync(InitialData.Products);
await context.SaveChangesAsync();
}

private static async Task SeedOrdersWithItemsAsync(ApplicationDbContext context)
{
if (!await context.Orders.AnyAsync())
{
await context.Orders.AddRangeAsync(InitialData.OrdersWithItems);
await context.SaveChangesAsync();
}
if (await context.Orders.AnyAsync()) return;
await context.Orders.AddRangeAsync(InitialData.OrdersWithItems);
await context.SaveChangesAsync();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,24 @@ public void UpdateEntities(DbContext? context)

foreach (var entry in context.ChangeTracker.Entries<IEntity>())
{
if (entry.State == EntityState.Added)
{
entry.Entity.CreatedBy = "mehmet";
entry.Entity.CreatedAt = DateTime.UtcNow;
}
HandleEntryCreation(entry);
HandleEntryModification(entry);
}
}
private void HandleEntryCreation(EntityEntry<IEntity> entry)
{
if (entry.State != EntityState.Added) return;

if (entry.State == EntityState.Added || entry.State == EntityState.Modified || entry.HasChangedOwnedEntities())
{
entry.Entity.LastModifiedBy = "mehmet";
entry.Entity.LastModified = DateTime.UtcNow;
}
entry.Entity.CreatedBy = "mehmet";
entry.Entity.CreatedAt = DateTime.UtcNow;
}
private void HandleEntryModification(EntityEntry<IEntity> entry)
{
if (entry is { State: EntityState.Added | EntityState.Modified }
|| entry.HasChangedOwnedEntities())
{
entry.Entity.LastModifiedBy = "mehmet";
entry.Entity.LastModified = DateTime.UtcNow;
}
}
}
Expand Down