88using AzureDevopsTracker . Helpers ;
99using AzureDevopsTracker . Interfaces ;
1010using AzureDevopsTracker . Interfaces . Internals ;
11+ using Microsoft . AspNetCore . Http ;
12+ using Microsoft . Extensions . DependencyInjection ;
1113using Newtonsoft . Json ;
1214using System ;
1315using System . Linq ;
@@ -20,15 +22,18 @@ public class AzureDevopsTrackerService : IAzureDevopsTrackerService
2022 public readonly IWorkItemRepository _workItemRepository ;
2123 public readonly IWorkItemAdapter _workItemAdapter ;
2224 public readonly IChangeLogItemRepository _changeLogItemRepository ;
25+ private readonly IServiceScopeFactory _serviceScopeFactory ;
2326
2427 public AzureDevopsTrackerService (
2528 IWorkItemAdapter workItemAdapter ,
2629 IWorkItemRepository workItemRepository ,
27- IChangeLogItemRepository changeLogItemRepository )
30+ IChangeLogItemRepository changeLogItemRepository ,
31+ IServiceScopeFactory serviceScopeFactory )
2832 {
2933 _workItemAdapter = workItemAdapter ;
3034 _workItemRepository = workItemRepository ;
3135 _changeLogItemRepository = changeLogItemRepository ;
36+ _serviceScopeFactory = serviceScopeFactory ;
3237 }
3338
3439 public async Task Create ( CreateWorkItemDTO create , bool addWorkItemChange = true )
@@ -55,6 +60,8 @@ public async Task Create(CreateWorkItemDTO create, bool addWorkItemChange = true
5560
5661 CheckWorkItemAvailableToChangeLog ( workItem , create . Resource . Fields ) ;
5762
63+ ManipulateCustomFields ( workItem ) ;
64+
5865 await _workItemRepository . Add ( workItem ) ;
5966 await _workItemRepository . SaveChangesAsync ( ) ;
6067 }
@@ -101,6 +108,8 @@ public async Task Update(UpdatedWorkItemDTO update)
101108
102109 CheckWorkItemAvailableToChangeLog ( workItem , update . Resource . Revision . Fields ) ;
103110
111+ ManipulateCustomFields ( workItem ) ;
112+
104113 _workItemRepository . Update ( workItem ) ;
105114 await _workItemRepository . SaveChangesAsync ( ) ;
106115 }
@@ -131,6 +140,8 @@ public async Task Delete(DeleteWorkItemDTO delete)
131140 delete . Resource . Fields . Activity ,
132141 delete . Resource . Fields . Lancado ) ;
133142
143+ ManipulateCustomFields ( workItem ) ;
144+
134145 _workItemRepository . Update ( workItem ) ;
135146 await _workItemRepository . SaveChangesAsync ( ) ;
136147 }
@@ -161,6 +172,8 @@ public async Task Restore(RestoreWorkItemDTO restore)
161172 restore . Resource . Fields . Activity ,
162173 restore . Resource . Fields . Lancado ) ;
163174
175+ ManipulateCustomFields ( workItem ) ;
176+
164177 _workItemRepository . Update ( workItem ) ;
165178 await _workItemRepository . SaveChangesAsync ( ) ;
166179 }
@@ -175,6 +188,31 @@ public async Task<WorkItemDTO> GetByWorkItemId(string workItemId)
175188 }
176189
177190 #region Support Methods
191+ public void ManipulateCustomFields ( WorkItem workItem )
192+ {
193+ try
194+ {
195+ var json = GetRequestBody ( ) ;
196+ if ( json . IsNullOrEmpty ( ) )
197+ return ;
198+
199+ var customFields = ReadJsonHelper . ReadJson ( workItem . Id , json ) ;
200+ if ( customFields is null || ! customFields . Any ( ) )
201+ return ;
202+
203+ workItem . UpdateCustomFields ( customFields ) ;
204+ }
205+ catch
206+ { }
207+ }
208+
209+ public string GetRequestBody ( )
210+ {
211+ using var scope = _serviceScopeFactory . CreateScope ( ) ;
212+ var httpContext = scope . ServiceProvider . GetService < IHttpContextAccessor > ( ) ;
213+ return httpContext ? . HttpContext ? . Request ? . Body ? . ToString ( ) ;
214+ }
215+
178216 public WorkItemChange ToWorkItemChange (
179217 string workItemId , string changedBy ,
180218 string iterationPath , DateTime newDate , string newState ,
@@ -274,58 +312,6 @@ public void RemoveChangeLogItem(WorkItem workItem)
274312 workItem . RemoveChangeLogItem ( ) ;
275313 }
276314 }
277-
278- /*
279- * Still missing:
280- * - Migration
281- */
282- public async Task Create ( string jsonText , bool addWorkItemChange = true )
283- {
284- try
285- {
286- var workItemDTO = JsonConvert . DeserializeObject < CreateWorkItemDTO > ( jsonText ) ;
287- await Create ( workItemDTO ) ;
288-
289- var workItem = await _workItemRepository . GetByWorkItemId ( workItemDTO . Resource . Id ) ;
290- if ( workItem is null )
291- return ;
292-
293- var customFields = ReadJsonHelper . ReadJson ( workItem . Id , jsonText ) ;
294- if ( customFields is null || ! customFields . Any ( ) )
295- return ;
296-
297- workItem . AddCustomFields ( customFields ) ;
298-
299- _workItemRepository . Update ( workItem ) ;
300- await _workItemRepository . SaveChangesAsync ( ) ;
301- }
302- catch
303- { }
304- }
305-
306- public async Task Update ( string jsonText )
307- {
308- try
309- {
310- var workItemDTO = JsonConvert . DeserializeObject < UpdatedWorkItemDTO > ( jsonText ) ;
311- await Update ( workItemDTO ) ;
312-
313- var workItem = await _workItemRepository . GetByWorkItemId ( workItemDTO . Resource . WorkItemId ) ;
314- if ( workItem is null )
315- return ;
316-
317- var customFields = ReadJsonHelper . ReadJson ( workItem . Id , jsonText ) ;
318- if ( customFields is null || ! customFields . Any ( ) )
319- return ;
320-
321- workItem . UpdateCustomFields ( customFields ) ;
322-
323- _workItemRepository . Update ( workItem ) ;
324- await _workItemRepository . SaveChangesAsync ( ) ;
325- }
326- catch
327- { }
328- }
329315 #endregion
330316 }
331317}
0 commit comments