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" }