got Database working
This commit is contained in:
parent
0733b933da
commit
15ab089d1c
|
|
@ -10,35 +10,36 @@ using ProjectGrid.Data;
|
||||||
|
|
||||||
namespace ProjectGrid.Controllers
|
namespace ProjectGrid.Controllers
|
||||||
{
|
{
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class UsersController : ControllerBase
|
public class UsersController : ControllerBase
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly ILogger<UsersController> _logger;
|
||||||
|
|
||||||
|
//static readonly Models.IUserRepository repository = new Models.UserRepository();
|
||||||
|
private readonly Models.IUserRepository repository;
|
||||||
|
|
||||||
|
public UsersController(ILogger<UsersController> logger, DataAccessContext context)
|
||||||
{
|
{
|
||||||
|
_logger = logger;
|
||||||
private readonly ILogger<UsersController> _logger;
|
repository = context;
|
||||||
|
|
||||||
//static readonly Models.IUserRepository repository = new Models.UserRepository();
|
|
||||||
static readonly Models.IUserRepository repository = DataAccessContext.GetInstance();
|
|
||||||
|
|
||||||
public UsersController(ILogger<UsersController> logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet]
|
|
||||||
[Route("api/users")]
|
|
||||||
public IEnumerable<Models.UserModel> GetAllUsers()
|
|
||||||
{
|
|
||||||
return repository.GetAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
|
||||||
[Route("api/user")]
|
|
||||||
[Consumes("application/json")]
|
|
||||||
public Models.UserModel PostUser(Models.UserModel item)
|
|
||||||
{
|
|
||||||
return repository.Add(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[Route("api/users")]
|
||||||
|
public IEnumerable<Models.UserModel> GetAllUsers()
|
||||||
|
{
|
||||||
|
return repository.GetAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
[Route("api/user")]
|
||||||
|
[Consumes("application/json")]
|
||||||
|
public Models.UserModel PostUser(Models.UserModel item)
|
||||||
|
{
|
||||||
|
return repository.Add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,55 +7,49 @@ using ProjectGrid.Models;
|
||||||
|
|
||||||
namespace ProjectGrid.Data
|
namespace ProjectGrid.Data
|
||||||
{
|
{
|
||||||
public class DataAccessContext : DbContext, IUserRepository
|
public class DataAccessContext : DbContext, IUserRepository
|
||||||
|
{
|
||||||
|
public DataAccessContext(DbContextOptions<DataAccessContext> options) : base(options) { }
|
||||||
|
|
||||||
|
public DbSet<UserData> Users { get; set; }
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
{
|
{
|
||||||
private static DataAccessContext _instance;
|
modelBuilder.Entity<UserData>().ToTable("USER");
|
||||||
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 IEnumerable<UserModel> GetAll()
|
||||||
{
|
{
|
||||||
public int? Id { get; set; }
|
return Users.Select<UserData, UserModel>(data => data.ToModel());
|
||||||
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};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 };
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using ProjectGrid.Data;
|
||||||
|
|
||||||
|
namespace ProjectGrid.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(DataAccessContext))]
|
||||||
|
[Migration("20210605214637_ProjectGrid")]
|
||||||
|
partial class ProjectGrid
|
||||||
|
{
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128)
|
||||||
|
.HasAnnotation("ProductVersion", "5.0.6")
|
||||||
|
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
|
||||||
|
|
||||||
|
modelBuilder.Entity("ProjectGrid.Data.UserData", b =>
|
||||||
|
{
|
||||||
|
b.Property<int?>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int")
|
||||||
|
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("Name1")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("Name2")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("USER");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
namespace ProjectGrid.Migrations
|
||||||
|
{
|
||||||
|
public partial class ProjectGrid : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "USER",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "int", nullable: false)
|
||||||
|
.Annotation("SqlServer:Identity", "1, 1"),
|
||||||
|
Name1 = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
Name2 = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||||
|
Email = table.Column<string>(type: "nvarchar(max)", nullable: true)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_USER", x => x.Id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "USER");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using ProjectGrid.Data;
|
||||||
|
|
||||||
|
namespace ProjectGrid.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(DataAccessContext))]
|
||||||
|
partial class DataAccessContextModelSnapshot : ModelSnapshot
|
||||||
|
{
|
||||||
|
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 128)
|
||||||
|
.HasAnnotation("ProductVersion", "5.0.6")
|
||||||
|
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
|
||||||
|
|
||||||
|
modelBuilder.Entity("ProjectGrid.Data.UserData", b =>
|
||||||
|
{
|
||||||
|
b.Property<int?>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int")
|
||||||
|
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("Name1")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.Property<string>("Name2")
|
||||||
|
.HasColumnType("nvarchar(max)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("USER");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,6 +10,10 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.6" />
|
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.6" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.6" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.6">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.17.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.17.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue