@@ -24,21 +24,17 @@ namespace MESS.Services.CRUD.WorkInstructions;
2424public class WorkInstructionUpdater : IWorkInstructionUpdater
2525{
2626 private readonly IProductResolver _productResolver ;
27- private readonly IPartNodeResolver _partNodeResolver ;
2827 private readonly IPartDefinitionResolver _partDefinitionResolver ;
2928
3029 /// <summary>
3130 /// Initializes a new instance of the <see cref="WorkInstructionUpdater"/> class.
3231 /// </summary>
3332 /// <param name="productResolver">The service used for resolving products from product names.</param>
3433 /// <param name="partDefinitionResolver">The service used for resolving the part definition that a work instruction produces.</param>
35- /// <param name="partNodeResolver">The service used for resolving part nodes in a work instruction.</param>
36- public WorkInstructionUpdater ( IProductResolver productResolver , IPartDefinitionResolver partDefinitionResolver ,
37- IPartNodeResolver partNodeResolver )
34+ public WorkInstructionUpdater ( IProductResolver productResolver , IPartDefinitionResolver partDefinitionResolver )
3835 {
3936 _productResolver = productResolver ;
4037 _partDefinitionResolver = partDefinitionResolver ;
41- _partNodeResolver = partNodeResolver ;
4238 }
4339
4440 /// <summary>
@@ -91,9 +87,7 @@ public async Task ApplyAsync(
9187 await SyncPartProducedAsync ( dto , entity , context ) ;
9288 await SyncProductsAsync ( dto , entity , context ) ;
9389
94- SyncNodes ( dto . Nodes , entity , context ) ;
95-
96- await _partNodeResolver . ResolvePendingNodesAsync ( context , entity . Nodes ) ;
90+ await SyncNodes ( dto . Nodes , entity , context ) ;
9791 }
9892
9993 private void ApplyScalars ( WorkInstructionFormDTO dto , WorkInstruction entity )
@@ -113,7 +107,7 @@ private async Task SyncProductsAsync(
113107 entity . Products = await _productResolver . ResolveProductsAsync ( context , dto . ProductNames ) ;
114108 }
115109
116- private void SyncNodes (
110+ private async Task SyncNodes (
117111 List < WorkInstructionNodeFormDTO > formNodes ,
118112 WorkInstruction entity ,
119113 ApplicationContext context )
@@ -138,11 +132,11 @@ private void SyncNodes(
138132 {
139133 if ( dto . Id != 0 && existingById . TryGetValue ( dto . Id , out var existing ) )
140134 {
141- ApplyToExisting ( dto , existing ) ;
135+ await ApplyToExistingAsync ( dto , existing , context ) ;
142136 }
143137 else
144138 {
145- var newNode = CreateNew ( dto ) ;
139+ var newNode = await CreateNewAsync ( dto , context ) ;
146140 entity . Nodes . Add ( newNode ) ;
147141 }
148142 }
@@ -179,9 +173,10 @@ private async Task SyncPartProducedAsync(
179173 null ) ;
180174 }
181175
182- private void ApplyToExisting (
176+ private async Task ApplyToExistingAsync (
183177 WorkInstructionNodeFormDTO dto ,
184- WorkInstructionNode entity )
178+ WorkInstructionNode entity ,
179+ ApplicationContext context )
185180 {
186181 entity . Position = dto . Position ;
187182
@@ -192,15 +187,17 @@ private void ApplyToExisting(
192187 break ;
193188
194189 case PartNodeFormDTO partDto when entity is PartNode part :
195- ApplyPartNode ( partDto , part ) ;
190+ await ApplyPartNodeAsync ( partDto , part , context ) ;
196191 break ;
197192
198193 default :
199194 throw new InvalidOperationException ( "Node type mismatch." ) ;
200195 }
201196 }
202197
203- private WorkInstructionNode CreateNew ( WorkInstructionNodeFormDTO dto )
198+ private async Task < WorkInstructionNode > CreateNewAsync (
199+ WorkInstructionNodeFormDTO dto ,
200+ ApplicationContext context )
204201 {
205202 return dto switch
206203 {
@@ -215,7 +212,7 @@ private WorkInstructionNode CreateNew(WorkInstructionNodeFormDTO dto)
215212 SecondaryMedia = stepDto . SecondaryMedia . ToList ( )
216213 } ,
217214
218- PartNodeFormDTO partDto => CreatePartNode ( partDto ) ,
215+ PartNodeFormDTO partDto => await CreatePartNodeAsync ( partDto , context ) ,
219216
220217 _ => throw new NotSupportedException ( )
221218 } ;
@@ -231,34 +228,42 @@ private void ApplyStep(StepNodeFormDTO dto, Step entity)
231228 entity . SecondaryMedia = dto . SecondaryMedia . ToList ( ) ;
232229 }
233230
234- private void ApplyPartNode ( PartNodeFormDTO dto , PartNode entity )
231+ private async Task ApplyPartNodeAsync (
232+ PartNodeFormDTO dto ,
233+ PartNode entity ,
234+ ApplicationContext context )
235235 {
236236 entity . InputType = dto . InputType ;
237237
238- // Store part info in a "pending resolution" way
239- entity . PartDefinitionId = 0 ; // Leave FK unset
240- entity . PartDefinition = new PartDefinition
241- {
242- Name = dto . Name ,
243- Number = dto . Number
244- } ;
238+ var part = await _partDefinitionResolver . ResolveAsync (
239+ context ,
240+ dto . Name ,
241+ dto . Number )
242+ ?? throw new InvalidOperationException (
243+ $ "Failed to resolve PartDefinition '{ dto . Name } ' '{ dto . Number } '.") ;
244+
245+ entity . PartDefinitionId = part . Id ;
246+ entity . PartDefinition = part ;
245247 }
246248
247- private PartNode CreatePartNode ( PartNodeFormDTO partDto )
249+ private async Task < PartNode > CreatePartNodeAsync (
250+ PartNodeFormDTO dto ,
251+ ApplicationContext context )
248252 {
253+ var part = await _partDefinitionResolver . ResolveAsync (
254+ context ,
255+ dto . Name ,
256+ dto . Number )
257+ ?? throw new InvalidOperationException (
258+ $ "Failed to resolve PartDefinition '{ dto . Name } ' '{ dto . Number } '.") ;
259+
249260 return new PartNode
250261 {
251262 NodeType = WorkInstructionNodeType . Part ,
252- Position = partDto . Position ,
253- InputType = partDto . InputType ,
254-
255- // Store part info without FK; will be resolved later
256- PartDefinitionId = 0 ,
257- PartDefinition = new PartDefinition
258- {
259- Name = partDto . Name ,
260- Number = partDto . Number
261- }
263+ Position = dto . Position ,
264+ InputType = dto . InputType ,
265+ PartDefinitionId = part . Id ,
266+ PartDefinition = part
262267 } ;
263268 }
264269}
0 commit comments