diff --git a/Controllers/UsersController.cs b/Controllers/UsersController.cs index e03ca46..b38a4de 100644 --- a/Controllers/UsersController.cs +++ b/Controllers/UsersController.cs @@ -10,35 +10,36 @@ using ProjectGrid.Data; namespace ProjectGrid.Controllers { - [ApiController] - public class UsersController : ControllerBase + [ApiController] + public class UsersController : ControllerBase + { + + private readonly ILogger _logger; + + //static readonly Models.IUserRepository repository = new Models.UserRepository(); + private readonly Models.IUserRepository repository; + + public UsersController(ILogger logger, DataAccessContext context) { - - private readonly ILogger _logger; - - //static readonly Models.IUserRepository repository = new Models.UserRepository(); - static readonly Models.IUserRepository repository = DataAccessContext.GetInstance(); - - public UsersController(ILogger logger) - { - _logger = logger; - } - - [HttpGet] - [Route("api/users")] - public IEnumerable GetAllUsers() - { - return repository.GetAll(); - } - - [HttpPost] - [Route("api/user")] - [Consumes("application/json")] - public Models.UserModel PostUser(Models.UserModel item) - { - return repository.Add(item); - } - - + _logger = logger; + repository = context; } + + [HttpGet] + [Route("api/users")] + public IEnumerable GetAllUsers() + { + return repository.GetAll(); + } + + [HttpPost] + [Route("api/user")] + [Consumes("application/json")] + public Models.UserModel PostUser(Models.UserModel item) + { + return repository.Add(item); + } + + + } } diff --git a/Data/DataAccessContext.cs b/Data/DataAccessContext.cs index bbc3da8..17e3b7b 100644 --- a/Data/DataAccessContext.cs +++ b/Data/DataAccessContext.cs @@ -7,55 +7,49 @@ using ProjectGrid.Models; namespace ProjectGrid.Data { - public class DataAccessContext : DbContext, IUserRepository + public class DataAccessContext : DbContext, IUserRepository + { + public DataAccessContext(DbContextOptions options) : base(options) { } + + public DbSet Users { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) { - private static DataAccessContext _instance; - public static DataAccessContext GetInstance() => _instance; - - public DataAccessContext(DbContextOptions options) : base(options) - { - _instance = this; - } - - public DbSet Users { get; set; } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - 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; - } + modelBuilder.Entity().ToTable("USER"); } - public class UserData + public IEnumerable GetAll() { - 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}; - } + 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/Migrations/20210605214637_ProjectGrid.Designer.cs b/Migrations/20210605214637_ProjectGrid.Designer.cs new file mode 100644 index 0000000..082119c --- /dev/null +++ b/Migrations/20210605214637_ProjectGrid.Designer.cs @@ -0,0 +1,47 @@ +// +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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Email") + .HasColumnType("nvarchar(max)"); + + b.Property("Name1") + .HasColumnType("nvarchar(max)"); + + b.Property("Name2") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("USER"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20210605214637_ProjectGrid.cs b/Migrations/20210605214637_ProjectGrid.cs new file mode 100644 index 0000000..9e56452 --- /dev/null +++ b/Migrations/20210605214637_ProjectGrid.cs @@ -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(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name1 = table.Column(type: "nvarchar(max)", nullable: true), + Name2 = table.Column(type: "nvarchar(max)", nullable: true), + Email = table.Column(type: "nvarchar(max)", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_USER", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "USER"); + } + } +} diff --git a/Migrations/DataAccessContextModelSnapshot.cs b/Migrations/DataAccessContextModelSnapshot.cs new file mode 100644 index 0000000..1bf6b22 --- /dev/null +++ b/Migrations/DataAccessContextModelSnapshot.cs @@ -0,0 +1,45 @@ +// +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("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("Email") + .HasColumnType("nvarchar(max)"); + + b.Property("Name1") + .HasColumnType("nvarchar(max)"); + + b.Property("Name2") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("USER"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ProjectGrid.csproj b/ProjectGrid.csproj index c3c6e97..fde86ca 100644 --- a/ProjectGrid.csproj +++ b/ProjectGrid.csproj @@ -10,6 +10,10 @@ + + runtime; build; native; contentfiles; analyzers; buildtransitive + all +