Skip to content

Commit 94aedf3

Browse files
committed
sql server validations and handling
1 parent 58db5b2 commit 94aedf3

File tree

13 files changed

+66
-40
lines changed

13 files changed

+66
-40
lines changed

NorthwindCRUD/Controllers/OrdersController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public ActionResult<CustomerDto> GetEmployeeByOrderId(int id)
125125
var order = this.orderService.GetById(id);
126126
if (order != null)
127127
{
128-
var employee = this.employeeService.GetById(order.EmployeeId);
128+
var employee = this.employeeService.GetById(order.EmployeeId ?? default);
129129
if (employee != null)
130130
{
131131
return Ok(this.mapper.Map<EmployeeDb, EmployeeDto>(employee));

NorthwindCRUD/Controllers/ProductsController.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ namespace NorthwindCRUD.Controllers
33
using AutoMapper;
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
6+
using NorthwindCRUD.Exceptions;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.Dtos;
8-
using NorthwindCRUD.Models.InputModels;
99
using NorthwindCRUD.Services;
1010

1111
[ApiController]
@@ -76,7 +76,7 @@ public ActionResult<CategoryDto> GetCategoryByProductId(int id)
7676
var product = this.productService.GetById(id);
7777
if (product != null)
7878
{
79-
var category = this.categoryService.GetById(product.CategoryId);
79+
var category = this.categoryService.GetById(product.CategoryId ?? default);
8080

8181
if (category != null)
8282
{
@@ -124,7 +124,7 @@ public ActionResult<SupplierDto> GetSupplierByProductId(int id)
124124
var product = this.productService.GetById(id);
125125
if (product != null)
126126
{
127-
var supplier = this.supplierService.GetById(product.SupplierId);
127+
var supplier = this.supplierService.GetById(product.SupplierId ?? default);
128128

129129
if (supplier != null)
130130
{
@@ -156,6 +156,10 @@ public ActionResult<ProductDto> Create(ProductDto model)
156156

157157
return BadRequest(ModelState);
158158
}
159+
catch (InvalidEntityIdException exception)
160+
{
161+
return StatusCode(400, exception.Message);
162+
}
159163
catch (Exception error)
160164
{
161165
logger.LogError(error.Message);

NorthwindCRUD/Controllers/TerritoriesController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public ActionResult<RegionDto[]> GetRegionByTerritory(string id)
9090
var territory = this.territoryService.GetById(id);
9191
if (territory != null)
9292
{
93-
var region = this.regionService.GetById(territory.RegionId);
93+
var region = this.regionService.GetById(territory.RegionId ?? default);
9494

9595
if (region != null)
9696
{

NorthwindCRUD/DataContext.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace NorthwindCRUD
22
{
33
using Microsoft.EntityFrameworkCore;
4-
using NorthwindCRUD.Models.Contracts;
54
using NorthwindCRUD.Models.DbModels;
65

76
public class DataContext : DbContext
@@ -35,35 +34,40 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3534

3635
modelBuilder.Entity<ProductDb>()
3736
.HasOne(p => p.Category)
38-
.WithMany(c => c.Products);
37+
.WithMany(c => c.Products)
38+
.OnDelete(DeleteBehavior.SetNull);
3939

4040
modelBuilder.Entity<ProductDb>()
4141
.HasOne(p => p.Supplier)
42-
.WithMany(c => c.Products);
42+
.WithMany(c => c.Products)
43+
.OnDelete(DeleteBehavior.SetNull);
4344

4445
modelBuilder.Entity<CustomerDb>()
4546
.HasOne(c => c.Address)
46-
.WithMany(a => a.Customers);
47+
.WithMany(a => a.Customers)
48+
.OnDelete(DeleteBehavior.NoAction);
4749

4850
modelBuilder.Entity<OrderDb>()
4951
.HasOne(o => o.ShipAddress)
50-
.WithMany(a => a.Orders);
52+
.WithMany(a => a.Orders)
53+
.OnDelete(DeleteBehavior.SetNull);
5154

5255
modelBuilder.Entity<OrderDb>()
5356
.HasOne(o => o.Customer)
5457
.WithMany(c => c.Orders)
5558
.HasForeignKey(o => o.CustomerId)
56-
.OnDelete(DeleteBehavior.Restrict);
59+
.OnDelete(DeleteBehavior.SetNull);
5760

5861
modelBuilder.Entity<OrderDb>()
5962
.HasOne(o => o.Employee)
6063
.WithMany(e => e.Orders)
6164
.HasForeignKey(o => o.EmployeeId)
62-
.OnDelete(DeleteBehavior.Restrict);
65+
.OnDelete(DeleteBehavior.SetNull);
6366

6467
modelBuilder.Entity<OrderDb>()
6568
.HasOne(o => o.Shipper)
66-
.WithMany(s => s.Orders);
69+
.WithMany(s => s.Orders)
70+
.OnDelete(DeleteBehavior.SetNull);
6771

6872
modelBuilder.Entity<OrderDetailDb>()
6973
.HasOne(od => od.Product)
@@ -75,11 +79,13 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
7579

7680
modelBuilder.Entity<EmployeeDb>()
7781
.HasOne(e => e.Address)
78-
.WithMany(a => a.Employees);
82+
.WithMany(a => a.Employees)
83+
.OnDelete(DeleteBehavior.NoAction);
7984

8085
modelBuilder.Entity<TerritoryDb>()
8186
.HasOne(t => t.Region)
82-
.WithMany(r => r.Territories);
87+
.WithMany(r => r.Territories)
88+
.OnDelete(DeleteBehavior.SetNull);
8389

8490
modelBuilder.Entity<EmployeeTerritoryDb>()
8591
.HasOne(et => et.Employee)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace NorthwindCRUD.Exceptions
2+
{
3+
public class InvalidEntityIdException : Exception
4+
{
5+
public InvalidEntityIdException(string name, string id) : base($"{name} with id {id} does not exist!")
6+
{
7+
}
8+
}
9+
}

NorthwindCRUD/Models/Contracts/IProduct.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ public interface IProduct
44
{
55
int ProductId { get; set; }
66

7-
int SupplierId { get; set; }
7+
int? SupplierId { get; set; }
88

9-
int CategoryId { get; set; }
9+
int? CategoryId { get; set; }
1010

1111
string QuantityPerUnit { get; set; }
1212

NorthwindCRUD/Models/DbModels/OrderDb.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ public OrderDb()
1515
[DatabaseGenerated(DatabaseGeneratedOption.None)]
1616
public int OrderId { get; set; }
1717

18-
public string CustomerId { get; set; }
18+
public string? CustomerId { get; set; }
1919

20-
public CustomerDb Customer { get; set;}
20+
public CustomerDb? Customer { get; set;}
2121

22-
public int EmployeeId { get; set; }
22+
public int? EmployeeId { get; set; }
2323

24-
public EmployeeDb Employee { get; set; }
24+
public EmployeeDb? Employee { get; set; }
2525

26-
public int ShipperId { get; set; }
26+
public int? ShipperId { get; set; }
2727

28-
public ShipperDb Shipper { get; set; }
28+
public ShipperDb? Shipper { get; set; }
2929

3030
public string OrderDate { get; set; }
3131

@@ -37,15 +37,15 @@ public OrderDb()
3737

3838
public string ShipName { get; set; }
3939

40-
public string ShipAddressId { get; set; }
41-
4240
public double UnitPrice { get; set; }
4341

4442
public int Quantity { get; set; }
4543

4644
public float Discount { get; set; }
4745

48-
public AddressDb ShipAddress { get; set; }
46+
public string? ShipAddressId { get; set; }
47+
48+
public AddressDb? ShipAddress { get; set; }
4949

5050
public ICollection<OrderDetailDb> Details { get; set; }
5151
}

NorthwindCRUD/Models/DbModels/OrderDetailDb.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System.ComponentModel.DataAnnotations;
2-
using System.ComponentModel.DataAnnotations.Schema;
3-
4-
namespace NorthwindCRUD.Models.DbModels
1+
namespace NorthwindCRUD.Models.DbModels
52
{
63
public class OrderDetailDb
74
{

NorthwindCRUD/Models/DbModels/ProductDb.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ public class ProductDb : IProduct
1010
[DatabaseGenerated(DatabaseGeneratedOption.None)]
1111
public int ProductId { get; set; }
1212

13-
public int SupplierId { get; set; }
13+
public int? SupplierId { get; set; }
1414

15-
public SupplierDb Supplier { get; set; }
15+
public SupplierDb? Supplier { get; set; }
1616

17-
public int CategoryId { get; set; }
17+
public int? CategoryId { get; set; }
1818

19-
public CategoryDb Category { get; set; }
19+
public CategoryDb? Category { get; set; }
2020

2121
public string QuantityPerUnit { get; set; }
2222

NorthwindCRUD/Models/DbModels/ShipperDb.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public class ShipperDb : IShipper
1414

1515
public string Phone { get; set; }
1616

17-
public ICollection<OrderDb> Orders { get; set; } = new List<OrderDb>();
17+
public ICollection<OrderDb> Orders { get; set; } = new List<OrderDb>();
1818
}
1919
}

0 commit comments

Comments
 (0)