-
Notifications
You must be signed in to change notification settings - Fork 4
Add EfCore database in AuthService #6
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
Open
petrukhinandrew
wants to merge
1
commit into
main
Choose a base branch
from
petrukhin/auth-db
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using AuthService.Domain; | ||
|
|
||
| namespace AuthService.DataAccess.EF; | ||
|
|
||
| public class AuthDbContext : DbContext | ||
| { | ||
| public AuthDbContext(DbContextOptions<AuthDbContext> options) : base(options) | ||
| { | ||
| } | ||
|
|
||
| public DbSet<InsuranceAgent> InsuranceAgents { get; set; } | ||
|
|
||
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
| { | ||
| optionsBuilder.EnableSensitiveDataLogging(); | ||
| } | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| modelBuilder.Entity<InsuranceAgent>(entity => | ||
| { | ||
| entity.HasKey(e => e.Id); | ||
| entity.HasIndex(e => e.Login).IsUnique(); | ||
| entity.Property(e => e.Login).IsRequired(); | ||
| entity.Property(e => e.Password).IsRequired(); | ||
| entity.Property(e => e.Avatar).IsRequired(); | ||
| entity.Property(e => e.AvailableProducts) | ||
| .HasConversion( | ||
| v => string.Join(',', v), | ||
| v => v.Split(',', StringSplitOptions.RemoveEmptyEntries).ToList() | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or 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,20 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using AuthService.Domain; | ||
|
|
||
| namespace AuthService.DataAccess.EF; | ||
|
|
||
| public static class EFInstaller | ||
| { | ||
| public static IServiceCollection AddEFConfiguration(this IServiceCollection services, IConfiguration configuration) | ||
| { | ||
|
Comment on lines
+8
to
+11
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. предпочтительно EfInstaller и AddEfConfiguration |
||
| services.AddDbContext<AuthDbContext>(options => | ||
| { | ||
| options.UseInMemoryDatabase("InsuranceAgents"); | ||
| }); | ||
|
|
||
| services.AddScoped<IInsuranceAgents, InsuranceAgentRepository>(); | ||
| return services; | ||
| } | ||
| } | ||
This file contains hidden or 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,27 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using AuthService.Domain; | ||
|
|
||
| namespace AuthService.DataAccess.EF; | ||
|
|
||
| public class InsuranceAgentRepository : IInsuranceAgents | ||
| { | ||
| private readonly AuthDbContext dbContext; | ||
|
|
||
| public InsuranceAgentRepository(AuthDbContext dbContext) | ||
| { | ||
| this.dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); | ||
| } | ||
|
|
||
| public void Add(InsuranceAgent agent) | ||
| { | ||
| dbContext.InsuranceAgents.Add(agent); | ||
| } | ||
|
Comment on lines
+17
to
+20
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. не хватает сохранения контекста |
||
|
|
||
| public InsuranceAgent FindByLogin(string login) | ||
| { | ||
| return dbContext.InsuranceAgents | ||
| .FirstOrDefault(a => a.Login == login); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,16 @@ | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace AuthService.Init; | ||
|
|
||
| public static class ApplicationBuilderExtensions | ||
| { | ||
| public static void UseInitializer(this IApplicationBuilder app) | ||
| { | ||
| using (var scope = app.ApplicationServices.CreateScope()) | ||
| { | ||
| var initializer = scope.ServiceProvider.GetService<DataLoader>(); | ||
| initializer.Seed(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,31 @@ | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using AuthService.DataAccess.EF; | ||
| using AuthService.Domain; | ||
|
|
||
| namespace AuthService.Init; | ||
|
|
||
| public class DataLoader | ||
| { | ||
| private readonly AuthDbContext dbContext; | ||
|
|
||
| public DataLoader(AuthDbContext context) | ||
| { | ||
| dbContext = context; | ||
| } | ||
|
|
||
| public void Seed() | ||
| { | ||
| dbContext.Database.EnsureCreated(); | ||
| if (dbContext.InsuranceAgents.Any()) return; | ||
|
|
||
| dbContext.InsuranceAgents.Add(new InsuranceAgent("jimmy.solid", "secret", "static/avatars/jimmy_solid.png", | ||
| new List<string> { "TRI", "HSI", "FAI", "CAR" })); | ||
| dbContext.InsuranceAgents.Add(new InsuranceAgent("danny.solid", "secret", "static/avatars/danny.solid.png", | ||
| new List<string> { "TRI", "HSI", "FAI", "CAR" })); | ||
| dbContext.InsuranceAgents.Add(new InsuranceAgent("admin", "admin", "static/avatars/admin.png", | ||
| new List<string> { "TRI", "HSI", "FAI", "CAR" })); | ||
|
|
||
| dbContext.SaveChanges(); | ||
| } | ||
| } |
This file contains hidden or 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,12 @@ | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace AuthService.Init; | ||
|
|
||
| public static class DataLoaderInstaller | ||
| { | ||
| public static IServiceCollection AddAuthDemoInitializer(this IServiceCollection services) | ||
| { | ||
| services.AddScoped<DataLoader>(); | ||
| return services; | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
используй primary конструктор