From d86717fb969b6d27a7210e7a9baf70b04cdb34c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B3th=20Tibor?= Date: Fri, 1 Apr 2022 09:44:48 +0200 Subject: [PATCH 1/3] migrate Bll and Dal to .NET 6 --- WebApiLab.BLL/DTOs/DTO.cs | 21 ---- WebApiLab.BLL/Dtos/Dtos.cs | 19 ++++ .../Exceptions/EntityNotFoundException.cs | 23 +++-- WebApiLab.BLL/Interfaces/IProductService.cs | 20 ++-- WebApiLab.BLL/WebApiLab.BLL.csproj | 6 +- WebApiLab.DAL/AppDbContext.cs | 89 +++++++++++++++++ WebApiLab.DAL/Entities/Category.cs | 19 ++-- WebApiLab.DAL/Entities/Order.cs | 21 ++-- WebApiLab.DAL/Entities/OrderItem.cs | 23 ++--- WebApiLab.DAL/Entities/Product.cs | 36 +++---- WebApiLab.DAL/Entities/ShipmentRegion.cs | 17 ++-- WebApiLab.DAL/NorthwindContext.cs | 95 ------------------- WebApiLab.DAL/WebApiLab.DAL.csproj | 6 +- WebApiLab.sln | 8 +- 14 files changed, 182 insertions(+), 221 deletions(-) delete mode 100644 WebApiLab.BLL/DTOs/DTO.cs create mode 100644 WebApiLab.BLL/Dtos/Dtos.cs create mode 100644 WebApiLab.DAL/AppDbContext.cs delete mode 100644 WebApiLab.DAL/NorthwindContext.cs diff --git a/WebApiLab.BLL/DTOs/DTO.cs b/WebApiLab.BLL/DTOs/DTO.cs deleted file mode 100644 index d5f0868..0000000 --- a/WebApiLab.BLL/DTOs/DTO.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using WebApiLab.DAL.Entities; - -namespace WebApiLab.BLL.DTO -{ - public record Category(int Id, string Name); - - public record Order(int Id, DateTime OrderDate); - - public record Product - { - public int Id { get; init; } - public string Name { get; init; } - public int UnitPrice { get; init; } - public ShipmentRegion ShipmentRegion { get; init; } - public int CategoryId { get; init; } - public Category Category { get; init; } - public List Orders { get; init; } - } -} \ No newline at end of file diff --git a/WebApiLab.BLL/Dtos/Dtos.cs b/WebApiLab.BLL/Dtos/Dtos.cs new file mode 100644 index 0000000..f36a0ab --- /dev/null +++ b/WebApiLab.BLL/Dtos/Dtos.cs @@ -0,0 +1,19 @@ +using WebApiLab.Dal.Entities; + +namespace WebApiLab.Bll.Dtos +{ + public record Category(int Id, string Name); + + public record Order(int Id, DateTime OrderDate); + + public record Product + { + public int Id { get; init; } + public string Name { get; init; } = null!; + public int UnitPrice { get; init; } + public ShipmentRegion ShipmentRegion { get; init; } + public int CategoryId { get; init; } + public Category Category { get; init; } = null!; + public List Orders { get; init; } = null!; + } +} \ No newline at end of file diff --git a/WebApiLab.BLL/Exceptions/EntityNotFoundException.cs b/WebApiLab.BLL/Exceptions/EntityNotFoundException.cs index 24f1394..2f0d11c 100644 --- a/WebApiLab.BLL/Exceptions/EntityNotFoundException.cs +++ b/WebApiLab.BLL/Exceptions/EntityNotFoundException.cs @@ -1,19 +1,18 @@ -using System; +namespace WebApiLab.Bll.Exceptions; -namespace WebApiLab.BLL +public class EntityNotFoundException : Exception { - public class EntityNotFoundException : Exception + public EntityNotFoundException() { - public EntityNotFoundException() - { - } + } - public EntityNotFoundException(string message) : base(message) - { - } + public EntityNotFoundException(string message) + : base(message) + { + } - public EntityNotFoundException(string message, Exception innerException) : base(message, innerException) - { - } + public EntityNotFoundException(string message, Exception innerException) + : base(message, innerException) + { } } diff --git a/WebApiLab.BLL/Interfaces/IProductService.cs b/WebApiLab.BLL/Interfaces/IProductService.cs index b3ffa7a..beaec51 100644 --- a/WebApiLab.BLL/Interfaces/IProductService.cs +++ b/WebApiLab.BLL/Interfaces/IProductService.cs @@ -1,14 +1,12 @@ -using System.Collections.Generic; -using WebApiLab.DAL.Entities; +using WebApiLab.Dal.Entities; -namespace WebApiLab.BLL +namespace WebApiLab.Bll.Interfaces; + +public interface IProductService { - public interface IProductService - { - Product GetProduct(int productId); - IEnumerable GetProducts(); - Product InsertProduct(Product newProduct); - void UpdateProduct(int productId, Product updatedProduct); - void DeleteProduct(int productId); - } + public Product GetProduct(int productId); + public IEnumerable GetProducts(); + public Product InsertProduct(Product newProduct); + public void UpdateProduct(int productId, Product updatedProduct); + public void DeleteProduct(int productId); } diff --git a/WebApiLab.BLL/WebApiLab.BLL.csproj b/WebApiLab.BLL/WebApiLab.BLL.csproj index 452ec9e..b2522ff 100644 --- a/WebApiLab.BLL/WebApiLab.BLL.csproj +++ b/WebApiLab.BLL/WebApiLab.BLL.csproj @@ -1,11 +1,13 @@ - net5.0 + net6.0 + enable + enable - + diff --git a/WebApiLab.DAL/AppDbContext.cs b/WebApiLab.DAL/AppDbContext.cs new file mode 100644 index 0000000..e9a9636 --- /dev/null +++ b/WebApiLab.DAL/AppDbContext.cs @@ -0,0 +1,89 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Microsoft.Extensions.Logging; + +using WebApiLab.Dal.Entities; + +namespace WebApiLab.Dal; + +public class AppDbContext : DbContext +{ + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=NEPTUN;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False") + .LogTo(Console.WriteLine, LogLevel.Debug); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity() + .Property(c => c.Name) + .HasMaxLength(15) + .IsRequired(); + + modelBuilder.Entity().HasData( + new Category { Id = 1, Name = "Ital" } + ); + + modelBuilder.Entity().HasData( + new Product { Id = 1, Name = "Sör", UnitPrice = 50, CategoryId = 1, ShipmentRegion = ShipmentRegion.Asia }, + new Product { Id = 2, Name = "Bor", UnitPrice = 550, CategoryId = 1 }, + new Product { Id = 3, Name = "Tej", UnitPrice = 260, CategoryId = 1 }, + new Product + { + Id = 4, + Name = "Whiskey", + UnitPrice = 960, + CategoryId = 1, + ShipmentRegion = ShipmentRegion.Australia + }, + new Product + { + Id = 5, + Name = "Rum", + UnitPrice = 960, + CategoryId = 1, + ShipmentRegion = ShipmentRegion.Eu | ShipmentRegion.NorthAmerica + } + ); + + modelBuilder.Entity().HasData( + new Order { Id = 1, OrderDate = new DateTime(2019, 02, 01) } + ); + + modelBuilder.Entity().HasData( + new OrderItem { Id = 1, OrderId = 1, ProductId = 1 }, + new OrderItem { Id = 2, OrderId = 1, ProductId = 2 } + ); + + modelBuilder.Entity() + .HasMany(p => p.Orders) + .WithMany(o => o.Products) + .UsingEntity( + j => j + .HasOne(oi => oi.Order) + .WithMany(o => o.OrderItems) + .HasForeignKey(oi => oi.OrderId) + .OnDelete(DeleteBehavior.Restrict), + j => j + .HasOne(oi => oi.Product) + .WithMany(p => p.ProductOrders) + .HasForeignKey(oi => oi.ProductId), + j => + { + j.HasKey(oi => oi.Id); + }); + + modelBuilder + .Entity() + .Property(e => e.ShipmentRegion) + .HasConversion(new EnumToStringConverter()); + } + + + public DbSet Products => Set(); + public DbSet Categories => Set(); + public DbSet Orders => Set(); +} diff --git a/WebApiLab.DAL/Entities/Category.cs b/WebApiLab.DAL/Entities/Category.cs index 39aba95..6acd438 100644 --- a/WebApiLab.DAL/Entities/Category.cs +++ b/WebApiLab.DAL/Entities/Category.cs @@ -1,17 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace WebApiLab.Dal.Entities; -namespace WebApiLab.DAL.Entities +public class Category { - public class Category - { - public int Id { get; set; } - public string Name { get; set; } + public int Id { get; set; } - public ICollection Products { get; } - = new List(); - } + public string Name { get; set; } = null!; + + public ICollection Products { get; } = new List(); } diff --git a/WebApiLab.DAL/Entities/Order.cs b/WebApiLab.DAL/Entities/Order.cs index 6eeed49..eebdbab 100644 --- a/WebApiLab.DAL/Entities/Order.cs +++ b/WebApiLab.DAL/Entities/Order.cs @@ -1,21 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace WebApiLab.Dal.Entities; -namespace WebApiLab.DAL.Entities +public class Order { - public class Order - { - public int Id { get; set; } + public int Id { get; set; } - public DateTime OrderDate { get; set; } + public DateTime OrderDate { get; set; } - public ICollection OrderItems { get; } - = new List(); + public ICollection OrderItems { get; } = new List(); - public ICollection Products { get; } - = new List(); - } + public ICollection Products { get; } = new List(); } diff --git a/WebApiLab.DAL/Entities/OrderItem.cs b/WebApiLab.DAL/Entities/OrderItem.cs index 7e7aeb7..949ab82 100644 --- a/WebApiLab.DAL/Entities/OrderItem.cs +++ b/WebApiLab.DAL/Entities/OrderItem.cs @@ -1,24 +1,17 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace WebApiLab.Dal.Entities; -namespace WebApiLab.DAL.Entities +public class OrderItem { - public class OrderItem - { - public int Id { get; set; } + public int Id { get; set; } - public int ProductId { get; set; } + public int ProductId { get; set; } - public Product Product { get; set; } + public Product? Product { get; set; } - public int OrderId { get; set; } + public int OrderId { get; set; } - public Order Order { get; set; } + public Order? Order { get; set; } - public int Quantity { get; set; } + public int Quantity { get; set; } - } } diff --git a/WebApiLab.DAL/Entities/Product.cs b/WebApiLab.DAL/Entities/Product.cs index cc4ad9c..7ed7e02 100644 --- a/WebApiLab.DAL/Entities/Product.cs +++ b/WebApiLab.DAL/Entities/Product.cs @@ -1,29 +1,23 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace WebApiLab.DAL.Entities +using System.ComponentModel.DataAnnotations.Schema; + +namespace WebApiLab.Dal.Entities; + +public class Product { - public class Product - { - public int Id { get; set; } + public int Id { get; set; } - [Column("ProductName")] - public string Name { get; set; } - public int UnitPrice { get; set; } + [Column("ProductName")] + public string Name { get; set; } = null!; - public int CategoryId { get; set; } - public Category Category { get; set; } + public int UnitPrice { get; set; } - public ICollection ProductOrders { get; } - = new List(); + public int CategoryId { get; set; } + public Category? Category { get; set; } - public ICollection Orders { get; } = new List(); + public ICollection ProductOrders { get; } = new List(); - public ShipmentRegion ShipmentRegion { get; set; } - } + public ICollection Orders { get; } = new List(); + public ShipmentRegion ShipmentRegion { get; set; } } + diff --git a/WebApiLab.DAL/Entities/ShipmentRegion.cs b/WebApiLab.DAL/Entities/ShipmentRegion.cs index 0646513..3c8580a 100644 --- a/WebApiLab.DAL/Entities/ShipmentRegion.cs +++ b/WebApiLab.DAL/Entities/ShipmentRegion.cs @@ -1,13 +1,10 @@ -using System; +namespace WebApiLab.Dal.Entities; -namespace WebApiLab.DAL.Entities +[Flags] +public enum ShipmentRegion { - [Flags] - public enum ShipmentRegion - { - EU = 1, - NorthAmerica = 2, - Asia = 4, - Australia = 8 - } + Eu = 1, + NorthAmerica = 2, + Asia = 4, + Australia = 8, } diff --git a/WebApiLab.DAL/NorthwindContext.cs b/WebApiLab.DAL/NorthwindContext.cs deleted file mode 100644 index 21e825f..0000000 --- a/WebApiLab.DAL/NorthwindContext.cs +++ /dev/null @@ -1,95 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using WebApiLab.DAL.Entities; - -namespace WebApiLab.DAL -{ - public class NorthwindContext : DbContext - { - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;Initial Catalog=xgef0q") - .LogTo(Console.WriteLine, LogLevel.Debug); - } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - base.OnModelCreating(modelBuilder); - - modelBuilder.Entity() - .Property(c => c.Name) - .HasMaxLength(15) - .IsRequired(); - - modelBuilder.Entity().HasData( - new Category { Id = 1, Name = "Ital" } - ); - - modelBuilder.Entity().HasData( - new Product { Id = 1, Name = "Sör", UnitPrice = 50, CategoryId = 1, ShipmentRegion = ShipmentRegion.Asia }, - new Product { Id = 2, Name = "Bor", UnitPrice = 550, CategoryId = 1 }, - new Product { Id = 3, Name = "Tej", UnitPrice = 260, CategoryId = 1 }, - new Product - { - Id = 4, - Name = "Whiskey", - UnitPrice = 960, - CategoryId = 1, - ShipmentRegion = ShipmentRegion.Australia - }, - new Product - { - Id = 5, - Name = "Rum", - UnitPrice = 960, - CategoryId = 1, - ShipmentRegion = ShipmentRegion.EU | ShipmentRegion.NorthAmerica - } - ); - - modelBuilder.Entity().HasData( - new Order { Id = 1, OrderDate = new DateTime(2019, 02, 01) } - ); - - modelBuilder.Entity().HasData( - new OrderItem { Id = 1, OrderId = 1, ProductId = 1 }, - new OrderItem { Id = 2, OrderId = 1, ProductId = 2 } - ); - - modelBuilder.Entity() - .HasMany(p => p.Orders) - .WithMany(o => o.Products) - .UsingEntity( - j => j - .HasOne(oi => oi.Order) - .WithMany(o => o.OrderItems) - .HasForeignKey(oi => oi.OrderId) - .OnDelete(DeleteBehavior.Restrict), - j => j - .HasOne(oi => oi.Product) - .WithMany(p => p.ProductOrders) - .HasForeignKey(oi => oi.ProductId), - j => - { - j.HasKey(oi => oi.Id); - }); - - var converter = new EnumToStringConverter(); - modelBuilder - .Entity() - .Property(e => e.ShipmentRegion) - .HasConversion(converter); - } - - - public DbSet Products { get; set; } - public DbSet Categories { get; set; } - public DbSet Orders { get; set; } - } -} diff --git a/WebApiLab.DAL/WebApiLab.DAL.csproj b/WebApiLab.DAL/WebApiLab.DAL.csproj index 63d98f8..21473a7 100644 --- a/WebApiLab.DAL/WebApiLab.DAL.csproj +++ b/WebApiLab.DAL/WebApiLab.DAL.csproj @@ -1,11 +1,13 @@ - net5.0 + net6.0 + enable + enable - + diff --git a/WebApiLab.sln b/WebApiLab.sln index c3a133a..ca0ae06 100644 --- a/WebApiLab.sln +++ b/WebApiLab.sln @@ -1,11 +1,11 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31005.135 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32210.238 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiLab.DAL", "WebApiLab.DAL\WebApiLab.DAL.csproj", "{AA817749-03BD-47DF-A847-84856B4B2B82}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApiLab.Dal", "WebApiLab.DAL\WebApiLab.Dal.csproj", "{AA817749-03BD-47DF-A847-84856B4B2B82}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApiLab.BLL", "WebApiLab.BLL\WebApiLab.BLL.csproj", "{8F376D98-9FF5-4241-8FCF-2F2B50CBFB66}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApiLab.Bll", "WebApiLab.BLL\WebApiLab.Bll.csproj", "{8F376D98-9FF5-4241-8FCF-2F2B50CBFB66}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution From e090b624bedb2cfbd2e3a3f20dbc7b3438804524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20G=C3=A1bor?= Date: Tue, 5 Apr 2022 19:54:07 +0200 Subject: [PATCH 2/3] model update --- WebApiLab.DAL/AppDbContext.cs | 14 ++++++-------- WebApiLab.DAL/Entities/Category.cs | 7 ++++++- WebApiLab.DAL/Entities/OrderItem.cs | 4 ++-- WebApiLab.DAL/Entities/Product.cs | 7 ++++++- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/WebApiLab.DAL/AppDbContext.cs b/WebApiLab.DAL/AppDbContext.cs index e9a9636..e19b687 100644 --- a/WebApiLab.DAL/AppDbContext.cs +++ b/WebApiLab.DAL/AppDbContext.cs @@ -24,25 +24,23 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .IsRequired(); modelBuilder.Entity().HasData( - new Category { Id = 1, Name = "Ital" } + new Category("Ital") { Id = 1 } ); modelBuilder.Entity().HasData( - new Product { Id = 1, Name = "Sör", UnitPrice = 50, CategoryId = 1, ShipmentRegion = ShipmentRegion.Asia }, - new Product { Id = 2, Name = "Bor", UnitPrice = 550, CategoryId = 1 }, - new Product { Id = 3, Name = "Tej", UnitPrice = 260, CategoryId = 1 }, - new Product + new Product("Sör") { Id = 1, UnitPrice = 50, CategoryId = 1, ShipmentRegion = ShipmentRegion.Asia }, + new Product("Bor") { Id = 2, UnitPrice = 550, CategoryId = 1 }, + new Product("Tej") { Id = 3, UnitPrice = 260, CategoryId = 1 }, + new Product("Whiskey") { Id = 4, - Name = "Whiskey", UnitPrice = 960, CategoryId = 1, ShipmentRegion = ShipmentRegion.Australia }, - new Product + new Product("Rum") { Id = 5, - Name = "Rum", UnitPrice = 960, CategoryId = 1, ShipmentRegion = ShipmentRegion.Eu | ShipmentRegion.NorthAmerica diff --git a/WebApiLab.DAL/Entities/Category.cs b/WebApiLab.DAL/Entities/Category.cs index 6acd438..a695ffa 100644 --- a/WebApiLab.DAL/Entities/Category.cs +++ b/WebApiLab.DAL/Entities/Category.cs @@ -4,7 +4,12 @@ public class Category { public int Id { get; set; } - public string Name { get; set; } = null!; + public string Name { get; set; } public ICollection Products { get; } = new List(); + + public Category(string name) + { + Name = name; + } } diff --git a/WebApiLab.DAL/Entities/OrderItem.cs b/WebApiLab.DAL/Entities/OrderItem.cs index 949ab82..e7bab75 100644 --- a/WebApiLab.DAL/Entities/OrderItem.cs +++ b/WebApiLab.DAL/Entities/OrderItem.cs @@ -6,11 +6,11 @@ public class OrderItem public int ProductId { get; set; } - public Product? Product { get; set; } + public Product? Product { get; set; } = null!; public int OrderId { get; set; } - public Order? Order { get; set; } + public Order? Order { get; set; } = null!; public int Quantity { get; set; } diff --git a/WebApiLab.DAL/Entities/Product.cs b/WebApiLab.DAL/Entities/Product.cs index 7ed7e02..1a082f7 100644 --- a/WebApiLab.DAL/Entities/Product.cs +++ b/WebApiLab.DAL/Entities/Product.cs @@ -7,7 +7,7 @@ public class Product public int Id { get; set; } [Column("ProductName")] - public string Name { get; set; } = null!; + public string Name { get; set; } public int UnitPrice { get; set; } @@ -19,5 +19,10 @@ public class Product public ICollection Orders { get; } = new List(); public ShipmentRegion ShipmentRegion { get; set; } + + public Product(string name) + { + Name = name; + } } From d532092b16f2e11bb2e6238434aa23b2aa041e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=B3th=20Tibor?= Date: Wed, 6 Apr 2022 10:21:08 +0200 Subject: [PATCH 3/3] =?UTF-8?q?nav=20propertyk=20nem=20nullozhat=C3=B3ak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WebApiLab.DAL/Entities/OrderItem.cs | 4 ++-- WebApiLab.DAL/Entities/Product.cs | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/WebApiLab.DAL/Entities/OrderItem.cs b/WebApiLab.DAL/Entities/OrderItem.cs index e7bab75..3ff0010 100644 --- a/WebApiLab.DAL/Entities/OrderItem.cs +++ b/WebApiLab.DAL/Entities/OrderItem.cs @@ -6,11 +6,11 @@ public class OrderItem public int ProductId { get; set; } - public Product? Product { get; set; } = null!; + public Product Product { get; set; } = null!; public int OrderId { get; set; } - public Order? Order { get; set; } = null!; + public Order Order { get; set; } = null!; public int Quantity { get; set; } diff --git a/WebApiLab.DAL/Entities/Product.cs b/WebApiLab.DAL/Entities/Product.cs index 1a082f7..4f778d4 100644 --- a/WebApiLab.DAL/Entities/Product.cs +++ b/WebApiLab.DAL/Entities/Product.cs @@ -12,7 +12,8 @@ public class Product public int UnitPrice { get; set; } public int CategoryId { get; set; } - public Category? Category { get; set; } + + public Category Category { get; set; } = null!; public ICollection ProductOrders { get; } = new List();