diff --git a/Controllers/UsersController.cs b/Controllers/UsersController.cs index 947a22f..e03ca46 100644 --- a/Controllers/UsersController.cs +++ b/Controllers/UsersController.cs @@ -6,6 +6,7 @@ using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using ProjectGrid.Data; namespace ProjectGrid.Controllers { @@ -15,7 +16,8 @@ namespace ProjectGrid.Controllers private readonly ILogger _logger; - static readonly Models.IUserRepository repository = new Models.UserRepository(); + //static readonly Models.IUserRepository repository = new Models.UserRepository(); + static readonly Models.IUserRepository repository = DataAccessContext.GetInstance(); public UsersController(ILogger logger) { diff --git a/Data/DataAccessContext.cs b/Data/DataAccessContext.cs index 56bef78..bbc3da8 100644 --- a/Data/DataAccessContext.cs +++ b/Data/DataAccessContext.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using Microsoft.EntityFrameworkCore; using ProjectGrid.Models; @@ -6,16 +7,55 @@ using ProjectGrid.Models; namespace ProjectGrid.Data { - public class DataAccessContext : DbContext + public class DataAccessContext : DbContext, IUserRepository { + private static DataAccessContext _instance; + public static DataAccessContext GetInstance() => _instance; - public DataAccessContext(DbContextOptions options) { } + public DataAccessContext(DbContextOptions options) : base(options) + { + _instance = this; + } - public DbSet Users; + public DbSet Users { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity().ToTable("[USER]"); + modelBuilder.Entity().ToTable("USER"); + } + + public IEnumerable GetAll() + { + return Users.Select( data => data.ToModel() ); + } + + public UserModel Add(UserModel user) + { + // TODO: check if exist + Users.Add(new UserData(user)); + SaveChanges(); + return user; + } + } + + public class UserData + { + public int? Id { get; set; } + public string Name1 { get; set; } + public string Name2 { get; set; } + public string Email { get; set; } + + public UserData() { } + public UserData(UserModel model) + { + Name1 = model.firstName; + Name2 = model.lastName; + Email = model.email; + } + + public UserModel ToModel() + { + return new UserModel {firstName = Name1, lastName = Name2, email = Email}; } } } diff --git a/Data/DemoData.cs b/Data/DemoData.cs index 473a72d..2e287a8 100644 --- a/Data/DemoData.cs +++ b/Data/DemoData.cs @@ -11,24 +11,24 @@ namespace ProjectGrid.Data { context.Database.EnsureCreated(); - // Look for any UserModels. + // Look for any UserDatas. if (context.Users.Any()) { return; // DB has been seeded } - var users = new UserModel[] + var users = new UserData[] { - new UserModel{firstName="Carson",lastName="Alexander",email="test@abc.com"}, - new UserModel{firstName="Meredith",lastName="Alonso",email="test@abc.com"}, - new UserModel{firstName="Arturo",lastName="Anand",email="test@abc.com"}, - new UserModel{firstName="Gytis",lastName="Barzdukas",email="test@abc.com"}, - new UserModel{firstName="Yan",lastName="Li",email="test@abc.com"}, - new UserModel{firstName="Peggy",lastName="Justice",email="test@abc.com"}, - new UserModel{firstName="Laura",lastName="Norman",email="test@abc.com"}, - new UserModel{firstName="Nino",lastName="Olivetto",email="test@abc.com"} + new UserData{Name1="Carson",Name2="Alexander",Email="test@abc.com"}, + new UserData{Name1="Meredith",Name2="Alonso",Email="test@abc.com"}, + new UserData{Name1="Arturo",Name2="Anand",Email="test@abc.com"}, + new UserData{Name1="Gytis",Name2="Barzdukas",Email="test@abc.com"}, + new UserData{Name1="Yan",Name2="Li",Email="test@abc.com"}, + new UserData{Name1="Peggy",Name2="Justice",Email="test@abc.com"}, + new UserData{Name1="Laura",Name2="Norman",Email="test@abc.com"}, + new UserData{Name1="Nino",Name2="Olivetto",Email="test@abc.com"} }; - foreach (UserModel s in users) + foreach (UserData s in users) { context.Users.Add(s); } diff --git a/Program.cs b/Program.cs index 792af80..6d97594 100644 --- a/Program.cs +++ b/Program.cs @@ -43,7 +43,7 @@ namespace ProjectGrid .ConfigureLogging(loggingBuilder => { loggingBuilder.ClearProviders(); - loggingBuilder.AddDebug().AddEventLog(); + loggingBuilder.AddDebug().AddConsole(); }) .ConfigureWebHostDefaults(webBuilder => { diff --git a/Startup.cs b/Startup.cs index 5428efb..cc7aaf9 100644 --- a/Startup.cs +++ b/Startup.cs @@ -19,7 +19,12 @@ namespace ProjectGrid { ILogger _logger; - public Startup() { } + public Startup() + { + Configuration = new ConfigurationBuilder() + .AddJsonFile("appSettings.json") // Get Connectionstring from appsetting.json + .Build(); + } public IConfiguration Configuration { get; } @@ -28,6 +33,8 @@ namespace ProjectGrid public void ConfigureServices(IServiceCollection services) { var connectionString = Configuration.GetConnectionString("DefaultConnection"); + // Cant log here.. + //_logger.LogInformation($"Try to connect to Database with {connectionString}"); services.AddDbContext(options => options.UseSqlServer(connectionString)); //_logger.LogInformation($"DataAccessContext registered with {connectionString}"); // TODO: for Testing diff --git a/appsettings.Development.json b/appsettings.Development.json index dba68eb..6d43e21 100644 --- a/appsettings.Development.json +++ b/appsettings.Development.json @@ -1,9 +1,12 @@ { + "ConnectionStrings": { + "DefaultConnection": "Server=hello-world.games;Database=ProjectGrid;Driver=SQL Server;uid=net;pwd=15123456" + }, "Logging": { "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" } } } diff --git a/appsettings.json b/appsettings.json index 37bd267..08fc6ba 100644 --- a/appsettings.json +++ b/appsettings.json @@ -1,6 +1,6 @@ { - "ConnectionStrings": { - "DefaultConnection": "Server=hello-world.games;Database=ProjectGrid;Driver=SQL Server;uid=net;pwd=15123456" + "ConnectionStrings": { + "DefaultConnection": "Server=hello-world.games;Database=ProjectGrid;uid=net;pwd=15123456" }, "Logging": { "LogLevel": {