data access for agent

master
Tudor Stanciu 2020-12-23 01:18:25 +02:00
parent 584525cf87
commit 3264ef6a24
11 changed files with 145 additions and 17 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using NetworkResurrector.Agent.Domain.Entities;
namespace NetworkResurrector.Agent.Domain.Data.EntityTypeConfiguration
{
class MachineConfiguration : IEntityTypeConfiguration<Machine>
{
public void Configure(EntityTypeBuilder<Machine> builder)
{
builder.ToTable("Machine").HasKey(key => key.MachineId);
builder.Property(z => z.MachineId).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="..\NetworkResurrector.Agent.Domain\NetworkResurrector.Agent.Domain.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore;
using NetworkResurrector.Agent.Domain.Data.DbContexts;
using NetworkResurrector.Agent.Domain.Entities;
using NetworkResurrector.Agent.Domain.Repositories;
using System.Threading.Tasks;
namespace NetworkResurrector.Agent.Domain.Data.Repositories
{
class AgentRepository : IAgentRepository
{
private readonly AgentDbContext _dbContext;
public AgentRepository(AgentDbContext dbContext)
{
_dbContext = dbContext;
}
public Task<Machine[]> GetMachines()
{
return _dbContext.Machines.ToArrayAsync();
}
}
}

View File

@ -0,0 +1,21 @@
if not exists (select top 1 1 from sys.objects where name = 'Machine' and type = 'U')
begin
create table Machine
(
MachineId int identity(0, 1) constraint PK_Machine primary key,
MachineName varchar(50) not null,
FullMachineName varchar(100) not null,
MACAddress varchar(20) not null,
IPv4Address varchar(20),
[Description] varchar(max)
)
end
go
if not exists (select top 1 1 from Machine)
begin
insert into Machine(MachineName, FullMachineName, MACAddress, IPv4Address, [Description])
select '***REMOVED***', '***REMOVED***', '***REMOVED***', '***REMOVED***', 'Personal main development station' union
select '***REMOVED***', '***REMOVED***', '***REMOVED***', null, 'Main file server'
end
go

View File

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

View File

@ -0,0 +1,12 @@
namespace NetworkResurrector.Agent.Domain.Entities
{
public class Machine
{
public int MachineId { get; set; }
public string MachineName { get; set; }
public string FullMachineName { get; set; }
public string MACAddress { get; set; }
public string IPv4Address { get; set; }
public string Description { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using NetworkResurrector.Agent.Domain.Entities;
using System.Threading.Tasks;
namespace NetworkResurrector.Agent.Domain.Repositories
{
public interface IAgentRepository
{
Task<Machine[]> GetMachines();
}
}

View File

@ -1,7 +1,11 @@
{ {
"urls": "http://*:5064",
"ConnectionStrings": {
"DatabaseConnection": "***REMOVED***"
},
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Debug",
"Microsoft": "Warning", "Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }