@@ -24,24 +24,21 @@ public class WorkItem : Entity
2424
2525 public ChangeLogItem ChangeLogItem { get ; private set ; }
2626
27- private readonly List < WorkItemChange > _workItemsChanges ;
27+ private readonly List < WorkItemChange > _workItemsChanges = new List < WorkItemChange > ( ) ;
2828 public IReadOnlyCollection < WorkItemChange > WorkItemsChanges => _workItemsChanges ;
2929
30- private readonly List < TimeByState > _timeByState ;
30+ private readonly List < TimeByState > _timeByState = new List < TimeByState > ( ) ;
3131 public IReadOnlyCollection < TimeByState > TimeByStates => _timeByState ;
32+
33+ private readonly List < WorkItemCustomField > _workItemCustomFields = new List < WorkItemCustomField > ( ) ;
34+ public IReadOnlyCollection < WorkItemCustomField > CustomFields => _workItemCustomFields ;
3235 public string CurrentStatus => _workItemsChanges ? . OrderBy ( x => x . CreatedAt ) ? . LastOrDefault ( ) ? . NewState ;
3336 public string LastStatus => _workItemsChanges ? . OrderBy ( x => x . CreatedAt ) ? . ToList ( ) ? . Skip ( 1 ) ? . LastOrDefault ( ) ? . OldState ;
3437
35- private WorkItem ( )
36- {
37- _workItemsChanges = new List < WorkItemChange > ( ) ;
38- _timeByState = new List < TimeByState > ( ) ;
39- }
38+ private WorkItem ( ) { }
4039
4140 public WorkItem ( string workItemId ) : base ( workItemId )
4241 {
43- _workItemsChanges = new List < WorkItemChange > ( ) ;
44- _timeByState = new List < TimeByState > ( ) ;
4542 Validate ( ) ;
4643 }
4744
@@ -76,34 +73,75 @@ public void Update(string title,
7673 public void Validate ( )
7774 {
7875 if ( Id . IsNullOrEmpty ( ) )
79- throw new Exception ( "WorkItemId is required" ) ;
76+ throw new ArgumentException ( "WorkItemId is required" ) ;
8077 }
8178
8279 public void AddWorkItemChange ( WorkItemChange workItemChange )
8380 {
84- if ( workItemChange == null )
85- throw new Exception ( "WorkItemChange is null" ) ;
81+ if ( workItemChange is null )
82+ throw new ArgumentException ( "WorkItemChange is null" ) ;
8683
8784 _workItemsChanges . Add ( workItemChange ) ;
8885 }
8986
9087 public void AddTimeByState ( TimeByState timeByState )
9188 {
92- if ( timeByState == null )
93- throw new Exception ( "TimeByState is null" ) ;
89+ if ( timeByState is null )
90+ throw new ArgumentException ( "TimeByState is null" ) ;
9491
9592 _timeByState . Add ( timeByState ) ;
9693 }
9794
9895 public void AddTimesByState ( IEnumerable < TimeByState > timesByState )
9996 {
100- if ( ! timesByState . Any ( ) )
97+ if ( timesByState is not null && ! timesByState . Any ( ) )
10198 return ;
10299
103100 foreach ( var timeByState in timesByState )
104101 AddTimeByState ( timeByState ) ;
105102 }
106103
104+ public void AddCustomField ( WorkItemCustomField customField )
105+ {
106+ if ( customField is null )
107+ throw new ArgumentException ( "CustomField is null" ) ;
108+
109+ if ( customField . WorkItemId . IsNullOrEmpty ( ) )
110+ customField . LinkWorkItem ( Id ) ;
111+
112+ _workItemCustomFields . Add ( customField ) ;
113+ }
114+
115+ public void AddCustomFields ( IEnumerable < WorkItemCustomField > customFields )
116+ {
117+ if ( customFields is not null && ! customFields . Any ( ) )
118+ return ;
119+
120+ foreach ( var customField in customFields )
121+ AddCustomField ( customField ) ;
122+ }
123+
124+ public void UpdateCustomFields ( IEnumerable < WorkItemCustomField > newCustomFields )
125+ {
126+ if ( newCustomFields is not null && ! newCustomFields . Any ( ) )
127+ return ;
128+
129+ var customFieldsToAdd = newCustomFields . Where ( x => ! _workItemCustomFields . Select ( x => x . Key ) . Contains ( x . Key ) ) ;
130+ AddCustomFields ( customFieldsToAdd ) ;
131+
132+ var customFieldsToUpdate = newCustomFields . Where ( x => _workItemCustomFields . Select ( x => x . Key ) . Contains ( x . Key ) ) ;
133+ foreach ( var newCustomField in customFieldsToUpdate )
134+ {
135+ var customField = _workItemCustomFields . FirstOrDefault ( x => x . Key == newCustomField . Key ) ;
136+ if ( customField is null ) continue ;
137+
138+ if ( customField . WorkItemId . IsNullOrEmpty ( ) )
139+ customField . LinkWorkItem ( Id ) ;
140+
141+ customField . Update ( customField . Value ) ;
142+ }
143+ }
144+
107145 public void ClearTimesByState ( )
108146 {
109147 _timeByState . Clear ( ) ;
@@ -116,8 +154,8 @@ public void RemoveChangeLogItem()
116154
117155 public void VinculateChangeLogItem ( ChangeLogItem changeLogItem )
118156 {
119- if ( changeLogItem == null )
120- throw new Exception ( "ChangeLogItem is null" ) ;
157+ if ( changeLogItem is null )
158+ throw new ArgumentException ( "ChangeLogItem is null" ) ;
121159
122160 ChangeLogItem = changeLogItem ;
123161 }
@@ -128,7 +166,7 @@ public IEnumerable<TimeByState> CalculateTotalTimeByState()
128166 if ( ! _workItemsChanges . Any ( ) )
129167 return timesByStateList ;
130168
131- foreach ( var workItemChange in _workItemsChanges . OrderBy ( x => x . CreatedAt ) . GroupBy ( x => x . OldState ) . Where ( x => x . Key != null ) )
169+ foreach ( var workItemChange in _workItemsChanges . OrderBy ( x => x . CreatedAt ) . GroupBy ( x => x . OldState ) . Where ( x => x . Key is not null ) )
132170 {
133171 var totalTime = TimeSpan . Zero ;
134172 var totalWorkedTime = TimeSpan . Zero ;
0 commit comments