@@ -25,24 +25,21 @@ public class WorkItem : Entity
2525
2626 public ChangeLogItem ChangeLogItem { get ; private set ; }
2727
28- private readonly List < WorkItemChange > _workItemsChanges ;
28+ private readonly List < WorkItemChange > _workItemsChanges = new List < WorkItemChange > ( ) ;
2929 public IReadOnlyCollection < WorkItemChange > WorkItemsChanges => _workItemsChanges ;
3030
31- private readonly List < TimeByState > _timeByState ;
31+ private readonly List < TimeByState > _timeByState = new List < TimeByState > ( ) ;
3232 public IReadOnlyCollection < TimeByState > TimeByStates => _timeByState ;
33+
34+ private readonly List < WorkItemCustomField > _workItemCustomFields = new List < WorkItemCustomField > ( ) ;
35+ public IReadOnlyCollection < WorkItemCustomField > CustomFields => _workItemCustomFields ;
3336 public string CurrentStatus => _workItemsChanges ? . OrderBy ( x => x . CreatedAt ) ? . LastOrDefault ( ) ? . NewState ;
3437 public string LastStatus => _workItemsChanges ? . OrderBy ( x => x . CreatedAt ) ? . ToList ( ) ? . Skip ( 1 ) ? . LastOrDefault ( ) ? . OldState ;
3538
36- private WorkItem ( )
37- {
38- _workItemsChanges = new List < WorkItemChange > ( ) ;
39- _timeByState = new List < TimeByState > ( ) ;
40- }
39+ private WorkItem ( ) { }
4140
4241 public WorkItem ( string workItemId ) : base ( workItemId )
4342 {
44- _workItemsChanges = new List < WorkItemChange > ( ) ;
45- _timeByState = new List < TimeByState > ( ) ;
4643 Validate ( ) ;
4744 }
4845
@@ -87,34 +84,75 @@ public void Delete()
8784 public void Validate ( )
8885 {
8986 if ( Id . IsNullOrEmpty ( ) )
90- throw new Exception ( "WorkItemId is required" ) ;
87+ throw new ArgumentException ( "WorkItemId is required" ) ;
9188 }
9289
9390 public void AddWorkItemChange ( WorkItemChange workItemChange )
9491 {
95- if ( workItemChange == null )
96- throw new Exception ( "WorkItemChange is null" ) ;
92+ if ( workItemChange is null )
93+ throw new ArgumentException ( "WorkItemChange is null" ) ;
9794
9895 _workItemsChanges . Add ( workItemChange ) ;
9996 }
10097
10198 public void AddTimeByState ( TimeByState timeByState )
10299 {
103- if ( timeByState == null )
104- throw new Exception ( "TimeByState is null" ) ;
100+ if ( timeByState is null )
101+ throw new ArgumentException ( "TimeByState is null" ) ;
105102
106103 _timeByState . Add ( timeByState ) ;
107104 }
108105
109106 public void AddTimesByState ( IEnumerable < TimeByState > timesByState )
110107 {
111- if ( ! timesByState . Any ( ) )
108+ if ( timesByState is not null && ! timesByState . Any ( ) )
112109 return ;
113110
114111 foreach ( var timeByState in timesByState )
115112 AddTimeByState ( timeByState ) ;
116113 }
117114
115+ public void AddCustomField ( WorkItemCustomField customField )
116+ {
117+ if ( customField is null )
118+ throw new ArgumentException ( "CustomField is null" ) ;
119+
120+ if ( customField . WorkItemId . IsNullOrEmpty ( ) )
121+ customField . LinkWorkItem ( Id ) ;
122+
123+ _workItemCustomFields . Add ( customField ) ;
124+ }
125+
126+ public void AddCustomFields ( IEnumerable < WorkItemCustomField > customFields )
127+ {
128+ if ( customFields is not null && ! customFields . Any ( ) )
129+ return ;
130+
131+ foreach ( var customField in customFields )
132+ AddCustomField ( customField ) ;
133+ }
134+
135+ public void UpdateCustomFields ( IEnumerable < WorkItemCustomField > newCustomFields )
136+ {
137+ if ( newCustomFields is not null && ! newCustomFields . Any ( ) )
138+ return ;
139+
140+ var customFieldsToAdd = newCustomFields . Where ( x => ! _workItemCustomFields . Select ( x => x . Key ) . Contains ( x . Key ) ) ;
141+ AddCustomFields ( customFieldsToAdd ) ;
142+
143+ var customFieldsToUpdate = newCustomFields . Where ( x => _workItemCustomFields . Select ( x => x . Key ) . Contains ( x . Key ) ) ;
144+ foreach ( var newCustomField in customFieldsToUpdate )
145+ {
146+ var customField = _workItemCustomFields . FirstOrDefault ( x => x . Key == newCustomField . Key ) ;
147+ if ( customField is null ) continue ;
148+
149+ if ( customField . WorkItemId . IsNullOrEmpty ( ) )
150+ customField . LinkWorkItem ( Id ) ;
151+
152+ customField . Update ( customField . Value ) ;
153+ }
154+ }
155+
118156 public void ClearTimesByState ( )
119157 {
120158 _timeByState . Clear ( ) ;
@@ -127,8 +165,8 @@ public void RemoveChangeLogItem()
127165
128166 public void VinculateChangeLogItem ( ChangeLogItem changeLogItem )
129167 {
130- if ( changeLogItem == null )
131- throw new Exception ( "ChangeLogItem is null" ) ;
168+ if ( changeLogItem is null )
169+ throw new ArgumentException ( "ChangeLogItem is null" ) ;
132170
133171 ChangeLogItem = changeLogItem ;
134172 }
@@ -139,7 +177,7 @@ public IEnumerable<TimeByState> CalculateTotalTimeByState()
139177 if ( ! _workItemsChanges . Any ( ) )
140178 return timesByStateList ;
141179
142- foreach ( var workItemChange in _workItemsChanges . OrderBy ( x => x . CreatedAt ) . GroupBy ( x => x . OldState ) . Where ( x => x . Key != null ) )
180+ foreach ( var workItemChange in _workItemsChanges . OrderBy ( x => x . CreatedAt ) . GroupBy ( x => x . OldState ) . Where ( x => x . Key is not null ) )
143181 {
144182 var totalTime = TimeSpan . Zero ;
145183 var totalWorkedTime = TimeSpan . Zero ;
0 commit comments