Data access

master
Tudor Stanciu 2020-12-20 00:18:53 +02:00
parent 2de9b12a01
commit aa71156974
12 changed files with 142 additions and 16 deletions

View File

@ -1,4 +1,8 @@
{ {
"urls": "http://*:5062",
"ConnectionStrings": {
"DatabaseConnection": "Server=***REMOVED***;Database=IdentityServer_dev;User Id=sa;Password=***REMOVED***;MultipleActiveResultSets=true"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",

View File

@ -1,8 +0,0 @@
using System;
namespace IdentityServer.Domain.Data
{
public class Class1
{
}
}

View File

@ -0,0 +1,25 @@
using IdentityServer.Domain.Data.EntityTypeConfiguration;
using IdentityServer.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace IdentityServer.Domain.Data.DbContexts
{
public class IdentityDbContext : DbContext
{
public DbSet<AppUser> AppUsers { get; set; }
public IdentityDbContext(DbContextOptions<IdentityDbContext> options)
: base(options)
{
base.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll;
base.ChangeTracker.AutoDetectChangesEnabled = true;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new AppUserConfiguration());
}
}
}

View File

@ -0,0 +1,26 @@
using IdentityServer.Domain.Data.DbContexts;
using IdentityServer.Domain.Data.Repositories;
using IdentityServer.Domain.Repositories;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
namespace IdentityServer.Domain.Data
{
public static class DependencyInjectionExtensions
{
public static void AddDataAccess(this IServiceCollection services)
{
services.AddScoped<IIdentityRepository, IdentityRepository>();
services
.AddDbContextPool<IdentityDbContext>(
(serviceProvider, options) =>
{
var configuration = serviceProvider.GetService<IConfiguration>();
var connectionString = configuration.GetConnectionString("DatabaseConnection");
options.UseSqlServer(connectionString);
});
}
}
}

View File

@ -0,0 +1,15 @@
using IdentityServer.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace IdentityServer.Domain.Data.EntityTypeConfiguration
{
class AppUserConfiguration : IEntityTypeConfiguration<AppUser>
{
public void Configure(EntityTypeBuilder<AppUser> builder)
{
builder.ToTable("AppUser").HasKey(key => key.UserId);
builder.Property(z => z.UserId).ValueGeneratedOnAdd();
}
}
}

View File

@ -4,4 +4,12 @@
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(EntityFrameworkCorePackageVersion)" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IdentityServer.Domain\IdentityServer.Domain.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,23 @@
using IdentityServer.Domain.Data.DbContexts;
using IdentityServer.Domain.Entities;
using IdentityServer.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;
namespace IdentityServer.Domain.Data.Repositories
{
class IdentityRepository : IIdentityRepository
{
private readonly IdentityDbContext _dbContext;
public IdentityRepository(IdentityDbContext dbContext)
{
_dbContext = dbContext;
}
public Task<AppUser> GetAppUser(string userName, string password)
{
return _dbContext.AppUsers.FirstOrDefaultAsync(z => z.UserName == userName && z.Password == password);
}
}
}

View File

@ -0,0 +1,18 @@
if not exists (select top 1 1 from sys.objects where name = 'AppUser' and type = 'U')
begin
create table AppUser
(
UserId int identity(0, 1) constraint PK_AppUser primary key,
UserName varchar(100) not null,
[Password] varchar(100) not null,
CreationDate datetime constraint DF_AppUser_CreationDate default getdate()
)
end
go
if not exists (select top 1 1 from AppUser)
begin
insert into AppUser(UserName, [Password])
select '***REMOVED***', '***REMOVED***'
end
go

View File

@ -1,8 +0,0 @@
using System;
namespace IdentityServer.Domain
{
public class Class1
{
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace IdentityServer.Domain.Entities
{
public class AppUser
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public DateTime CreationDate { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace IdentityServer.Domain.Repositories
{
public interface IIdentityRepository
{
}
}

View File

@ -9,5 +9,6 @@
<AutoMapperExtensionsPackageVersion>7.0.0</AutoMapperExtensionsPackageVersion> <AutoMapperExtensionsPackageVersion>7.0.0</AutoMapperExtensionsPackageVersion>
<MediatRPackageVersion>6.0.0</MediatRPackageVersion> <MediatRPackageVersion>6.0.0</MediatRPackageVersion>
<SwashbucklePackageVersion>5.3.1</SwashbucklePackageVersion> <SwashbucklePackageVersion>5.3.1</SwashbucklePackageVersion>
<EntityFrameworkCorePackageVersion>3.1.3</EntityFrameworkCorePackageVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>