Skip to content

Commit 851a601

Browse files
committed
added invalid navigation entity error handling
1 parent 3d2ed60 commit 851a601

File tree

8 files changed

+79
-1
lines changed

8 files changed

+79
-1
lines changed

NorthwindCRUD/Controllers/OrdersController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ public ActionResult<OrderDto> Create(OrderDto model)
211211

212212
return BadRequest(ModelState);
213213
}
214+
catch (InvalidOperationException exception)
215+
{
216+
return StatusCode(400, exception.Message);
217+
}
214218
catch (Exception error)
215219
{
216220
logger.LogError(error.Message);
@@ -238,6 +242,10 @@ public ActionResult<OrderDto> Update(OrderDto model)
238242

239243
return BadRequest(ModelState);
240244
}
245+
catch (InvalidOperationException exception)
246+
{
247+
return StatusCode(400, exception.Message);
248+
}
241249
catch (Exception error)
242250
{
243251
logger.LogError(error.Message);

NorthwindCRUD/Controllers/ProductsController.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ public ActionResult<ProductDto> Update(ProductDto model)
187187

188188
return BadRequest(ModelState);
189189
}
190+
catch (InvalidOperationException exception)
191+
{
192+
return StatusCode(400, exception.Message);
193+
}
190194
catch (Exception error)
191195
{
192196
logger.LogError(error.Message);

NorthwindCRUD/Controllers/TerritoriesController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public ActionResult<TerritoryDto> Create(TerritoryDto model)
123123

124124
return BadRequest(ModelState);
125125
}
126+
catch (InvalidOperationException exception)
127+
{
128+
return StatusCode(400, exception.Message);
129+
}
126130
catch (Exception error)
127131
{
128132
logger.LogError(error.Message);
@@ -151,6 +155,10 @@ public ActionResult<TerritoryDto> Update(TerritoryDto model)
151155

152156
return BadRequest(ModelState);
153157
}
158+
catch (InvalidOperationException exception)
159+
{
160+
return StatusCode(400, exception.Message);
161+
}
154162
catch (Exception error)
155163
{
156164
logger.LogError(error.Message);

NorthwindCRUD/Models/Contracts/ITerritory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ public interface ITerritory
55
string TerritoryId { get; set; }
66

77
string TerritoryDescription { get; set; }
8+
9+
public int? RegionId { get; set; }
810
}
911
}

NorthwindCRUD/Models/Dtos/TerritoryDto.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ public class TerritoryDto : ITerritory
77
public string TerritoryId { get; set; }
88

99
public string TerritoryDescription { get; set; }
10+
11+
public int? RegionId { get; set; }
1012
}
1113
}

NorthwindCRUD/Services/OrderService.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using Microsoft.CodeAnalysis;
44
using Microsoft.EntityFrameworkCore;
5+
using NorthwindCRUD.Constants;
56
using NorthwindCRUD.Helpers;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.Dtos;
@@ -73,6 +74,22 @@ public OrderDetailDb[] GetOrderDetailsByProductId(int id)
7374

7475
public OrderDb Create(OrderDb model)
7576
{
77+
78+
if (this.dataContext.Customers.FirstOrDefault(c => c.CustomerId == model.CustomerId) == null)
79+
{
80+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Customer), model.CustomerId.ToString()));
81+
}
82+
83+
if (this.dataContext.Employees.FirstOrDefault(e => e.EmployeeId == model.EmployeeId) == null)
84+
{
85+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Employee), model.EmployeeId.ToString()));
86+
}
87+
88+
if (this.dataContext.Shippers.FirstOrDefault(s => s.ShipperId == model.ShipperId) == null)
89+
{
90+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Shipper), model.ShipperId.ToString()));
91+
}
92+
7693
var id = IdGenerator.CreateDigitsId();
7794
var existWithId = this.GetById(id);
7895
while (existWithId != null)
@@ -99,6 +116,21 @@ public OrderDb Create(OrderDb model)
99116

100117
public OrderDb Update(OrderDb model)
101118
{
119+
if (this.dataContext.Customers.FirstOrDefault(c => c.CustomerId == model.CustomerId) == null)
120+
{
121+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Customer), model.CustomerId.ToString()));
122+
}
123+
124+
if (this.dataContext.Employees.FirstOrDefault(e => e.EmployeeId == model.EmployeeId) == null)
125+
{
126+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Employee), model.EmployeeId.ToString()));
127+
}
128+
129+
if (this.dataContext.Shippers.FirstOrDefault(s => s.ShipperId == model.ShipperId) == null)
130+
{
131+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Shipper), model.ShipperId.ToString()));
132+
}
133+
102134
var orderEntity = this.dataContext.Orders
103135
.Include(c => c.ShipAddress)
104136
.FirstOrDefault(e => e.OrderId == model.OrderId);

NorthwindCRUD/Services/ProductService.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ProductDb Create(ProductDb model)
5151
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Category), model.CategoryId.ToString()));
5252
}
5353

54-
if (this.dataContext.Suppliers.FirstOrDefault(c => c.SupplierId == model.SupplierId) == null)
54+
if (this.dataContext.Suppliers.FirstOrDefault(s => s.SupplierId == model.SupplierId) == null)
5555
{
5656
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Supplier), model.SupplierId.ToString()));
5757
}
@@ -75,6 +75,16 @@ public ProductDb Create(ProductDb model)
7575

7676
public ProductDb Update(ProductDb model)
7777
{
78+
if (this.dataContext.Categories.FirstOrDefault(c => c.CategoryId == model.CategoryId) == null)
79+
{
80+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Category), model.CategoryId.ToString()));
81+
}
82+
83+
if (this.dataContext.Suppliers.FirstOrDefault(s => s.SupplierId == model.SupplierId) == null)
84+
{
85+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Supplier), model.SupplierId.ToString()));
86+
}
87+
7888
var productEntity = this.dataContext.Products.FirstOrDefault(p => p.ProductId == model.ProductId);
7989
if (productEntity != null)
8090
{

NorthwindCRUD/Services/TerritoryService.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using AutoMapper;
44
using Microsoft.EntityFrameworkCore;
5+
using NorthwindCRUD.Constants;
56
using NorthwindCRUD.Helpers;
67
using NorthwindCRUD.Models.DbModels;
78
using NorthwindCRUD.Models.Dtos;
@@ -36,6 +37,11 @@ public TerritoryDb[] GetTerritoriesByRegionId(int id)
3637

3738
public TerritoryDb Create(TerritoryDb model)
3839
{
40+
if (this.dataContext.Regions.FirstOrDefault(r => r.RegionId == model.RegionId) == null)
41+
{
42+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Region), model.RegionId.ToString()));
43+
}
44+
3945
var id = IdGenerator.CreateDigitsId().ToString();
4046
var existWithId = this.GetById(id);
4147
while (existWithId != null)
@@ -55,10 +61,16 @@ public TerritoryDb Create(TerritoryDb model)
5561

5662
public TerritoryDb Update(TerritoryDb model)
5763
{
64+
if (this.dataContext.Regions.FirstOrDefault(r => r.RegionId == model.RegionId) == null)
65+
{
66+
throw new InvalidOperationException(string.Format(StringTemplates.InvalidEntityMessage, nameof(model.Region), model.RegionId.ToString()));
67+
}
68+
5869
var territoryEntity = this.dataContext.Territories.FirstOrDefault(p => p.TerritoryId == model.TerritoryId);
5970
if (territoryEntity != null)
6071
{
6172
territoryEntity.TerritoryDescription = model.TerritoryDescription != null ? model.TerritoryDescription : territoryEntity.TerritoryDescription;
73+
territoryEntity.RegionId = model.RegionId != null ? model.RegionId : territoryEntity.RegionId;
6274

6375
this.dataContext.SaveChanges();
6476
}

0 commit comments

Comments
 (0)