From 3264ef6a247934e95a856ef1f7387c98bd7794c0 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 23 Dec 2020 01:18:25 +0200 Subject: [PATCH] data access for agent --- .../Class1.cs | 8 ------ .../DbContexts/AgentDbContext.cs | 25 ++++++++++++++++++ .../DependencyInjectionExtensions.cs | 26 +++++++++++++++++++ .../MachineConfiguration.cs | 15 +++++++++++ ...etworkResurrector.Agent.Domain.Data.csproj | 8 ++++++ .../Repositories/AgentRepository.cs | 23 ++++++++++++++++ .../Scripts/001.Machine table.sql | 21 +++++++++++++++ NetworkResurrector.Agent.Domain/Class1.cs | 8 ------ .../Entities/Machine.cs | 12 +++++++++ .../Repositories/IAgentRepository.cs | 10 +++++++ NetworkResurrector.Agent/appsettings.json | 6 ++++- 11 files changed, 145 insertions(+), 17 deletions(-) delete mode 100644 NetworkResurrector.Agent.Domain.Data/Class1.cs create mode 100644 NetworkResurrector.Agent.Domain.Data/DbContexts/AgentDbContext.cs create mode 100644 NetworkResurrector.Agent.Domain.Data/DependencyInjectionExtensions.cs create mode 100644 NetworkResurrector.Agent.Domain.Data/EntityTypeConfiguration/MachineConfiguration.cs create mode 100644 NetworkResurrector.Agent.Domain.Data/Repositories/AgentRepository.cs create mode 100644 NetworkResurrector.Agent.Domain.Data/Scripts/001.Machine table.sql delete mode 100644 NetworkResurrector.Agent.Domain/Class1.cs create mode 100644 NetworkResurrector.Agent.Domain/Entities/Machine.cs create mode 100644 NetworkResurrector.Agent.Domain/Repositories/IAgentRepository.cs diff --git a/NetworkResurrector.Agent.Domain.Data/Class1.cs b/NetworkResurrector.Agent.Domain.Data/Class1.cs deleted file mode 100644 index 47148de..0000000 --- a/NetworkResurrector.Agent.Domain.Data/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace NetworkResurrector.Agent.Domain.Data -{ - public class Class1 - { - } -} diff --git a/NetworkResurrector.Agent.Domain.Data/DbContexts/AgentDbContext.cs b/NetworkResurrector.Agent.Domain.Data/DbContexts/AgentDbContext.cs new file mode 100644 index 0000000..8acca05 --- /dev/null +++ b/NetworkResurrector.Agent.Domain.Data/DbContexts/AgentDbContext.cs @@ -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 Machines { get; set; } + + public AgentDbContext(DbContextOptions 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()); + } + } +} diff --git a/NetworkResurrector.Agent.Domain.Data/DependencyInjectionExtensions.cs b/NetworkResurrector.Agent.Domain.Data/DependencyInjectionExtensions.cs new file mode 100644 index 0000000..f98a0e8 --- /dev/null +++ b/NetworkResurrector.Agent.Domain.Data/DependencyInjectionExtensions.cs @@ -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(); + + services + .AddDbContextPool( + (serviceProvider, options) => + { + var configuration = serviceProvider.GetService(); + var connectionString = configuration.GetConnectionString("DatabaseConnection"); + options.UseSqlServer(connectionString); + }); + } + } +} diff --git a/NetworkResurrector.Agent.Domain.Data/EntityTypeConfiguration/MachineConfiguration.cs b/NetworkResurrector.Agent.Domain.Data/EntityTypeConfiguration/MachineConfiguration.cs new file mode 100644 index 0000000..a0d9264 --- /dev/null +++ b/NetworkResurrector.Agent.Domain.Data/EntityTypeConfiguration/MachineConfiguration.cs @@ -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 + { + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("Machine").HasKey(key => key.MachineId); + builder.Property(z => z.MachineId).ValueGeneratedOnAdd(); + } + } +} diff --git a/NetworkResurrector.Agent.Domain.Data/NetworkResurrector.Agent.Domain.Data.csproj b/NetworkResurrector.Agent.Domain.Data/NetworkResurrector.Agent.Domain.Data.csproj index 9f5c4f4..fec7df8 100644 --- a/NetworkResurrector.Agent.Domain.Data/NetworkResurrector.Agent.Domain.Data.csproj +++ b/NetworkResurrector.Agent.Domain.Data/NetworkResurrector.Agent.Domain.Data.csproj @@ -4,4 +4,12 @@ netstandard2.0 + + + + + + + + diff --git a/NetworkResurrector.Agent.Domain.Data/Repositories/AgentRepository.cs b/NetworkResurrector.Agent.Domain.Data/Repositories/AgentRepository.cs new file mode 100644 index 0000000..f54ebd9 --- /dev/null +++ b/NetworkResurrector.Agent.Domain.Data/Repositories/AgentRepository.cs @@ -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 GetMachines() + { + return _dbContext.Machines.ToArrayAsync(); + } + } +} diff --git a/NetworkResurrector.Agent.Domain.Data/Scripts/001.Machine table.sql b/NetworkResurrector.Agent.Domain.Data/Scripts/001.Machine table.sql new file mode 100644 index 0000000..3f78b9c --- /dev/null +++ b/NetworkResurrector.Agent.Domain.Data/Scripts/001.Machine table.sql @@ -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 \ No newline at end of file diff --git a/NetworkResurrector.Agent.Domain/Class1.cs b/NetworkResurrector.Agent.Domain/Class1.cs deleted file mode 100644 index 0bcb93e..0000000 --- a/NetworkResurrector.Agent.Domain/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace NetworkResurrector.Agent.Domain -{ - public class Class1 - { - } -} diff --git a/NetworkResurrector.Agent.Domain/Entities/Machine.cs b/NetworkResurrector.Agent.Domain/Entities/Machine.cs new file mode 100644 index 0000000..a0e3afa --- /dev/null +++ b/NetworkResurrector.Agent.Domain/Entities/Machine.cs @@ -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; } + } +} diff --git a/NetworkResurrector.Agent.Domain/Repositories/IAgentRepository.cs b/NetworkResurrector.Agent.Domain/Repositories/IAgentRepository.cs new file mode 100644 index 0000000..804c4a7 --- /dev/null +++ b/NetworkResurrector.Agent.Domain/Repositories/IAgentRepository.cs @@ -0,0 +1,10 @@ +using NetworkResurrector.Agent.Domain.Entities; +using System.Threading.Tasks; + +namespace NetworkResurrector.Agent.Domain.Repositories +{ + public interface IAgentRepository + { + Task GetMachines(); + } +} diff --git a/NetworkResurrector.Agent/appsettings.json b/NetworkResurrector.Agent/appsettings.json index d9d9a9b..96ecf3e 100644 --- a/NetworkResurrector.Agent/appsettings.json +++ b/NetworkResurrector.Agent/appsettings.json @@ -1,7 +1,11 @@ { + "urls": "http://*:5064", + "ConnectionStrings": { + "DatabaseConnection": "***REMOVED***" + }, "Logging": { "LogLevel": { - "Default": "Information", + "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" }