Compare commits

..

No commits in common. "0733b933da2aace466d6fa447201e3bea0a7b357" and "c359776a221414a07f4124c9dc2e13a99030db65" have entirely different histories.

12 changed files with 24 additions and 177 deletions

View File

@ -6,9 +6,8 @@ using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ProjectGrid.Data;
namespace ProjectGrid.Controllers
namespace vue_dotnet_example.Controllers
{
[ApiController]
public class UsersController : ControllerBase
@ -16,8 +15,7 @@ namespace ProjectGrid.Controllers
private readonly ILogger<UsersController> _logger;
//static readonly Models.IUserRepository repository = new Models.UserRepository();
static readonly Models.IUserRepository repository = DataAccessContext.GetInstance();
static readonly Models.IUserRepository repository = new Models.UserRepository();
public UsersController(ILogger<UsersController> logger)
{

View File

@ -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};
}
}
}

View File

@ -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();
}
}
}

View File

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
namespace ProjectGrid.Models
namespace vue_dotnet_example.Models
{
public interface IUserRepository
{

View File

@ -1,4 +1,4 @@
namespace ProjectGrid.Models
namespace vue_dotnet_example.Models
{
public class UserModel
{

View File

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
namespace ProjectGrid.Models
namespace vue_dotnet_example.Models
{
public class UserRepository: IUserRepository
{

View File

@ -1,5 +1,3 @@
using ProjectGrid.Data;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
@ -9,42 +7,17 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ProjectGrid
namespace vue_dotnet_example
{
public class Program
{
private static ILogger _logger;
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
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();
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddDebug().AddConsole();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();

View File

@ -8,11 +8,6 @@
</PropertyGroup>
<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" />
</ItemGroup>

View File

@ -17,7 +17,7 @@
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ProjectGrid": {
"vue_dotnet_example": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,

View File

@ -1,60 +1,47 @@
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.ApplicationInsights;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Microsoft.EntityFrameworkCore;
using ProjectGrid.Data;
namespace ProjectGrid
namespace vue_dotnet_example
{
public class Startup
{
ILogger _logger;
public Startup()
public Startup(IConfiguration configuration)
{
Configuration = new ConfigurationBuilder()
.AddJsonFile("appSettings.json") // Get Connectionstring from appsetting.json
.Build();
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
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.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.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
_logger = logger;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
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();

View File

@ -1,7 +1,4 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=hello-world.games;Database=ProjectGrid;Driver=SQL Server;uid=net;pwd=15123456"
},
"Logging": {
"LogLevel": {
"Default": "Information",

View File

@ -1,7 +1,4 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=hello-world.games;Database=ProjectGrid;uid=net;pwd=15123456"
},
"Logging": {
"LogLevel": {
"Default": "Information",