Skip to content

Commit 27678e7

Browse files
authored
Merge pull request #1 from bmeaut/net6-init
Migrate Bll and Dal init to .NET 6
2 parents af9858e + d532092 commit 27678e7

14 files changed

Lines changed: 190 additions & 220 deletions

File tree

WebApiLab.BLL/DTOs/DTO.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

WebApiLab.BLL/Dtos/Dtos.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using WebApiLab.Dal.Entities;
2+
3+
namespace WebApiLab.Bll.Dtos
4+
{
5+
public record Category(int Id, string Name);
6+
7+
public record Order(int Id, DateTime OrderDate);
8+
9+
public record Product
10+
{
11+
public int Id { get; init; }
12+
public string Name { get; init; } = null!;
13+
public int UnitPrice { get; init; }
14+
public ShipmentRegion ShipmentRegion { get; init; }
15+
public int CategoryId { get; init; }
16+
public Category Category { get; init; } = null!;
17+
public List<Order> Orders { get; init; } = null!;
18+
}
19+
}
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
using System;
1+
namespace WebApiLab.Bll.Exceptions;
22

3-
namespace WebApiLab.BLL
3+
public class EntityNotFoundException : Exception
44
{
5-
public class EntityNotFoundException : Exception
5+
public EntityNotFoundException()
66
{
7-
public EntityNotFoundException()
8-
{
9-
}
7+
}
108

11-
public EntityNotFoundException(string message) : base(message)
12-
{
13-
}
9+
public EntityNotFoundException(string message)
10+
: base(message)
11+
{
12+
}
1413

15-
public EntityNotFoundException(string message, Exception innerException) : base(message, innerException)
16-
{
17-
}
14+
public EntityNotFoundException(string message, Exception innerException)
15+
: base(message, innerException)
16+
{
1817
}
1918
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
using System.Collections.Generic;
2-
using WebApiLab.DAL.Entities;
1+
using WebApiLab.Dal.Entities;
32

4-
namespace WebApiLab.BLL
3+
namespace WebApiLab.Bll.Interfaces;
4+
5+
public interface IProductService
56
{
6-
public interface IProductService
7-
{
8-
Product GetProduct(int productId);
9-
IEnumerable<Product> GetProducts();
10-
Product InsertProduct(Product newProduct);
11-
void UpdateProduct(int productId, Product updatedProduct);
12-
void DeleteProduct(int productId);
13-
}
7+
public Product GetProduct(int productId);
8+
public IEnumerable<Product> GetProducts();
9+
public Product InsertProduct(Product newProduct);
10+
public void UpdateProduct(int productId, Product updatedProduct);
11+
public void DeleteProduct(int productId);
1412
}

WebApiLab.BLL/WebApiLab.BLL.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
57
</PropertyGroup>
68

79
<ItemGroup>
8-
<ProjectReference Include="..\WebApiLab.DAL\WebApiLab.DAL.csproj" />
10+
<ProjectReference Include="..\WebApiLab.DAL\WebApiLab.Dal.csproj" />
911
</ItemGroup>
1012

1113
</Project>

WebApiLab.DAL/AppDbContext.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
3+
using Microsoft.Extensions.Logging;
4+
5+
using WebApiLab.Dal.Entities;
6+
7+
namespace WebApiLab.Dal;
8+
9+
public class AppDbContext : DbContext
10+
{
11+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
12+
{
13+
optionsBuilder.UseSqlServer(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=NEPTUN;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False")
14+
.LogTo(Console.WriteLine, LogLevel.Debug);
15+
}
16+
17+
protected override void OnModelCreating(ModelBuilder modelBuilder)
18+
{
19+
base.OnModelCreating(modelBuilder);
20+
21+
modelBuilder.Entity<Category>()
22+
.Property(c => c.Name)
23+
.HasMaxLength(15)
24+
.IsRequired();
25+
26+
modelBuilder.Entity<Category>().HasData(
27+
new Category("Ital") { Id = 1 }
28+
);
29+
30+
modelBuilder.Entity<Product>().HasData(
31+
new Product("Sör") { Id = 1, UnitPrice = 50, CategoryId = 1, ShipmentRegion = ShipmentRegion.Asia },
32+
new Product("Bor") { Id = 2, UnitPrice = 550, CategoryId = 1 },
33+
new Product("Tej") { Id = 3, UnitPrice = 260, CategoryId = 1 },
34+
new Product("Whiskey")
35+
{
36+
Id = 4,
37+
UnitPrice = 960,
38+
CategoryId = 1,
39+
ShipmentRegion = ShipmentRegion.Australia
40+
},
41+
new Product("Rum")
42+
{
43+
Id = 5,
44+
UnitPrice = 960,
45+
CategoryId = 1,
46+
ShipmentRegion = ShipmentRegion.Eu | ShipmentRegion.NorthAmerica
47+
}
48+
);
49+
50+
modelBuilder.Entity<Order>().HasData(
51+
new Order { Id = 1, OrderDate = new DateTime(2019, 02, 01) }
52+
);
53+
54+
modelBuilder.Entity<OrderItem>().HasData(
55+
new OrderItem { Id = 1, OrderId = 1, ProductId = 1 },
56+
new OrderItem { Id = 2, OrderId = 1, ProductId = 2 }
57+
);
58+
59+
modelBuilder.Entity<Product>()
60+
.HasMany(p => p.Orders)
61+
.WithMany(o => o.Products)
62+
.UsingEntity<OrderItem>(
63+
j => j
64+
.HasOne(oi => oi.Order)
65+
.WithMany(o => o.OrderItems)
66+
.HasForeignKey(oi => oi.OrderId)
67+
.OnDelete(DeleteBehavior.Restrict),
68+
j => j
69+
.HasOne(oi => oi.Product)
70+
.WithMany(p => p.ProductOrders)
71+
.HasForeignKey(oi => oi.ProductId),
72+
j =>
73+
{
74+
j.HasKey(oi => oi.Id);
75+
});
76+
77+
modelBuilder
78+
.Entity<Product>()
79+
.Property(e => e.ShipmentRegion)
80+
.HasConversion(new EnumToStringConverter<ShipmentRegion>());
81+
}
82+
83+
84+
public DbSet<Product> Products => Set<Product>();
85+
public DbSet<Category> Categories => Set<Category>();
86+
public DbSet<Order> Orders => Set<Order>();
87+
}

WebApiLab.DAL/Entities/Category.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace WebApiLab.Dal.Entities;
62

7-
namespace WebApiLab.DAL.Entities
3+
public class Category
84
{
9-
public class Category
10-
{
11-
public int Id { get; set; }
12-
public string Name { get; set; }
5+
public int Id { get; set; }
6+
7+
public string Name { get; set; }
138

14-
public ICollection<Product> Products { get; }
15-
= new List<Product>();
9+
public ICollection<Product> Products { get; } = new List<Product>();
10+
11+
public Category(string name)
12+
{
13+
Name = name;
1614
}
1715
}

WebApiLab.DAL/Entities/Order.cs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace WebApiLab.Dal.Entities;
62

7-
namespace WebApiLab.DAL.Entities
3+
public class Order
84
{
9-
public class Order
10-
{
11-
public int Id { get; set; }
5+
public int Id { get; set; }
126

13-
public DateTime OrderDate { get; set; }
7+
public DateTime OrderDate { get; set; }
148

15-
public ICollection<OrderItem> OrderItems { get; }
16-
= new List<OrderItem>();
9+
public ICollection<OrderItem> OrderItems { get; } = new List<OrderItem>();
1710

18-
public ICollection<Product> Products { get; }
19-
= new List<Product>();
20-
}
11+
public ICollection<Product> Products { get; } = new List<Product>();
2112
}
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
1+
namespace WebApiLab.Dal.Entities;
62

7-
namespace WebApiLab.DAL.Entities
3+
public class OrderItem
84
{
9-
public class OrderItem
10-
{
11-
public int Id { get; set; }
5+
public int Id { get; set; }
126

13-
public int ProductId { get; set; }
7+
public int ProductId { get; set; }
148

15-
public Product Product { get; set; }
9+
public Product Product { get; set; } = null!;
1610

17-
public int OrderId { get; set; }
11+
public int OrderId { get; set; }
1812

19-
public Order Order { get; set; }
13+
public Order Order { get; set; } = null!;
2014

21-
public int Quantity { get; set; }
15+
public int Quantity { get; set; }
2216

23-
}
2417
}

WebApiLab.DAL/Entities/Product.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel.DataAnnotations.Schema;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
8-
namespace WebApiLab.DAL.Entities
1+
using System.ComponentModel.DataAnnotations.Schema;
2+
3+
namespace WebApiLab.Dal.Entities;
4+
5+
public class Product
96
{
10-
public class Product
11-
{
12-
public int Id { get; set; }
7+
public int Id { get; set; }
138

14-
[Column("ProductName")]
15-
public string Name { get; set; }
16-
public int UnitPrice { get; set; }
9+
[Column("ProductName")]
10+
public string Name { get; set; }
1711

18-
public int CategoryId { get; set; }
19-
public Category Category { get; set; }
12+
public int UnitPrice { get; set; }
2013

21-
public ICollection<OrderItem> ProductOrders { get; }
22-
= new List<OrderItem>();
14+
public int CategoryId { get; set; }
2315

24-
public ICollection<Order> Orders { get; } = new List<Order>();
16+
public Category Category { get; set; } = null!;
2517

26-
public ShipmentRegion ShipmentRegion { get; set; }
27-
}
18+
public ICollection<OrderItem> ProductOrders { get; } = new List<OrderItem>();
19+
20+
public ICollection<Order> Orders { get; } = new List<Order>();
2821

22+
public ShipmentRegion ShipmentRegion { get; set; }
23+
24+
public Product(string name)
25+
{
26+
Name = name;
27+
}
2928
}
29+

0 commit comments

Comments
 (0)