Compare commits
No commits in common. "0733b933da2aace466d6fa447201e3bea0a7b357" and "c359776a221414a07f4124c9dc2e13a99030db65" have entirely different histories.
0733b933da
...
c359776a22
|
|
@ -6,9 +6,8 @@ using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using ProjectGrid.Data;
|
|
||||||
|
|
||||||
namespace ProjectGrid.Controllers
|
namespace vue_dotnet_example.Controllers
|
||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class UsersController : ControllerBase
|
public class UsersController : ControllerBase
|
||||||
|
|
@ -16,8 +15,7 @@ namespace ProjectGrid.Controllers
|
||||||
|
|
||||||
private readonly ILogger<UsersController> _logger;
|
private readonly ILogger<UsersController> _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<UsersController> logger)
|
public UsersController(ILogger<UsersController> logger)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using ProjectGrid.Models;
|
|
||||||
|
|
||||||
|
|
||||||
namespace ProjectGrid.Data
|
|
||||||
{
|
|
||||||
public class DataAccessContext : DbContext, IUserRepository
|
|
||||||
{
|
|
||||||
private static DataAccessContext _instance;
|
|
||||||
public static DataAccessContext GetInstance() => _instance;
|
|
||||||
|
|
||||||
public DataAccessContext(DbContextOptions<DataAccessContext> options) : base(options)
|
|
||||||
{
|
|
||||||
_instance = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DbSet<UserData> Users { get; set; }
|
|
||||||
|
|
||||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
||||||
{
|
|
||||||
modelBuilder.Entity<UserData>().ToTable("USER");
|
|
||||||
}
|
|
||||||
|
|
||||||
public IEnumerable<UserModel> GetAll()
|
|
||||||
{
|
|
||||||
return Users.Select<UserData, UserModel>( 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};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,39 +0,0 @@
|
||||||
using ProjectGrid.Models;
|
|
||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace ProjectGrid.Data
|
|
||||||
{
|
|
||||||
public static class DbInitializer
|
|
||||||
{
|
|
||||||
|
|
||||||
public static void Initialize(DataAccessContext context)
|
|
||||||
{
|
|
||||||
context.Database.EnsureCreated();
|
|
||||||
|
|
||||||
// Look for any UserDatas.
|
|
||||||
if (context.Users.Any())
|
|
||||||
{
|
|
||||||
return; // DB has been seeded
|
|
||||||
}
|
|
||||||
|
|
||||||
var users = new UserData[]
|
|
||||||
{
|
|
||||||
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 (UserData s in users)
|
|
||||||
{
|
|
||||||
context.Users.Add(s);
|
|
||||||
}
|
|
||||||
context.SaveChanges();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace ProjectGrid.Models
|
namespace vue_dotnet_example.Models
|
||||||
{
|
{
|
||||||
public interface IUserRepository
|
public interface IUserRepository
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
namespace ProjectGrid.Models
|
namespace vue_dotnet_example.Models
|
||||||
{
|
{
|
||||||
public class UserModel
|
public class UserModel
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace ProjectGrid.Models
|
namespace vue_dotnet_example.Models
|
||||||
{
|
{
|
||||||
public class UserRepository: IUserRepository
|
public class UserRepository: IUserRepository
|
||||||
{
|
{
|
||||||
|
|
|
||||||
31
Program.cs
31
Program.cs
|
|
@ -1,5 +1,3 @@
|
||||||
using ProjectGrid.Data;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
@ -9,42 +7,17 @@ using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace ProjectGrid
|
namespace vue_dotnet_example
|
||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
|
|
||||||
private static ILogger _logger;
|
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var host = CreateHostBuilder(args).Build();
|
CreateHostBuilder(args).Build().Run();
|
||||||
|
|
||||||
using (var scope = host.Services.CreateScope())
|
|
||||||
{
|
|
||||||
var services = scope.ServiceProvider;
|
|
||||||
_logger = services.GetRequiredService<ILogger<Program>>();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var context = services.GetRequiredService<DataAccessContext>();
|
|
||||||
DbInitializer.Initialize(context);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
|
|
||||||
_logger.LogError(ex, "An error occurred while seeding the database.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
host.Run();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||||
Host.CreateDefaultBuilder(args)
|
Host.CreateDefaultBuilder(args)
|
||||||
.ConfigureLogging(loggingBuilder =>
|
|
||||||
{
|
|
||||||
loggingBuilder.ClearProviders();
|
|
||||||
loggingBuilder.AddDebug().AddConsole();
|
|
||||||
})
|
|
||||||
.ConfigureWebHostDefaults(webBuilder =>
|
.ConfigureWebHostDefaults(webBuilder =>
|
||||||
{
|
{
|
||||||
webBuilder.UseStartup<Startup>();
|
webBuilder.UseStartup<Startup>();
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,6 @@
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.6" />
|
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.17.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
|
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"ProjectGrid": {
|
"vue_dotnet_example": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"dotnetRunMessages": "true",
|
"dotnetRunMessages": "true",
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
|
|
|
||||||
39
Startup.cs
39
Startup.cs
|
|
@ -1,60 +1,47 @@
|
||||||
using Microsoft.ApplicationInsights.Channel;
|
using System;
|
||||||
using Microsoft.ApplicationInsights.Extensibility;
|
using System.Collections.Generic;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using System.Linq;
|
||||||
using Microsoft.Extensions.Logging;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.Logging.ApplicationInsights;
|
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.HttpsPolicy;
|
using Microsoft.AspNetCore.HttpsPolicy;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using ProjectGrid.Data;
|
|
||||||
|
|
||||||
namespace ProjectGrid
|
namespace vue_dotnet_example
|
||||||
{
|
{
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
ILogger _logger;
|
public Startup(IConfiguration configuration)
|
||||||
|
|
||||||
public Startup()
|
|
||||||
{
|
{
|
||||||
Configuration = new ConfigurationBuilder()
|
Configuration = configuration;
|
||||||
.AddJsonFile("appSettings.json") // Get Connectionstring from appsetting.json
|
|
||||||
.Build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public IConfiguration Configuration { get; }
|
public IConfiguration Configuration { get; }
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to add services to the container.
|
// This method gets called by the runtime. Use this method to add services to the container.
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
var connectionString = Configuration.GetConnectionString("DefaultConnection");
|
|
||||||
// Cant log here..
|
|
||||||
//_logger.LogInformation($"Try to connect to Database with {connectionString}");
|
|
||||||
services.AddDbContext<DataAccessContext>(options => options.UseSqlServer(connectionString));
|
|
||||||
//_logger.LogInformation($"DataAccessContext registered with {connectionString}");
|
|
||||||
// TODO: for Testing
|
|
||||||
//services.AddDatabaseDeveloperPageExceptionFilter();
|
|
||||||
services.AddControllers();
|
services.AddControllers();
|
||||||
services.AddSwaggerGen(c =>
|
services.AddSwaggerGen(c =>
|
||||||
{
|
{
|
||||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ProjectGrid", Version = "v1" });
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "vue_dotnet_example", Version = "v1" });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
|
||||||
if (env.IsDevelopment())
|
if (env.IsDevelopment())
|
||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ProjectGrid v1"));
|
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "vue_dotnet_example v1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseHttpsRedirection();
|
app.UseHttpsRedirection();
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,9 @@
|
||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
|
||||||
"DefaultConnection": "Server=hello-world.games;Database=ProjectGrid;Driver=SQL Server;uid=net;pwd=15123456"
|
|
||||||
},
|
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft": "Warning",
|
"Microsoft": "Warning",
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,4 @@
|
||||||
{
|
{
|
||||||
"ConnectionStrings": {
|
|
||||||
"DefaultConnection": "Server=hello-world.games;Database=ProjectGrid;uid=net;pwd=15123456"
|
|
||||||
},
|
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue