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 ) ;
0 commit comments