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

Catch duplicated key constraint exception #262

Merged
merged 1 commit into from
Jul 10, 2024
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
12 changes: 11 additions & 1 deletion ClubService.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ClubService.API;
using ClubService.API.ApplicationConfigurations;
using ClubService.Domain.Repository;
using ClubService.Infrastructure.DbContexts;
using Microsoft.AspNetCore.Authentication;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -66,7 +67,16 @@
app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "ClubServiceV1"); });

await eventStoreDbContext.Database.EnsureCreatedAsync();
await eventStoreDbContext.SeedTestData();

try
{
await eventStoreDbContext.SeedTestData();
}
catch (DbUpdateException)
{
var logger = services.GetRequiredService<ILoggerService<Program>>();
logger.LogDuplicateSeedData();
}

await readStoreDbContext.Database.EnsureDeletedAsync();
await readStoreDbContext.Database.EnsureCreatedAsync();
Expand Down
1 change: 1 addition & 0 deletions ClubService.Domain/Repository/ILoggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace ClubService.Domain.Repository;

public interface ILoggerService<T>

Check warning on line 6 in ClubService.Domain/Repository/ILoggerService.cs

View workflow job for this annotation

GitHub Actions / build

'T' is not used in the interface. (https://rules.sonarsource.com/csharp/RSPEC-2326)
{
void LogDeleteAdmin(Guid id);
void LogAdminNotFound(Guid id);
Expand Down Expand Up @@ -64,4 +64,5 @@
void LogJsonMissingProperties(string jsonValue);
void LogEmailMessageRelayStop();
void LogSystemOperatorRegistered(Guid id);
void LogDuplicateSeedData();
}
5 changes: 5 additions & 0 deletions ClubService.Infrastructure/Logging/LoggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,9 @@ public void LogSystemOperatorRegistered(Guid id)
{
logger.LogInformation("Registered system operator with id '{id}'.", id);
}

public void LogDuplicateSeedData()
{
logger.LogInformation("Seed data already seeded");
}
}
Loading