Repository navigation
Expand file tree
/
Copy pathBoard.cs
More file actions
229 lines (197 loc) · 6.94 KB
/
Copy pathBoard.cs
File metadata and controls
229 lines (197 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//------------------------------------------------------------------------------
// <copyright company="LeanKit Inc.">
// Copyright (c) LeanKit Inc. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using LeanKit.API.Client.Library.Exceptions;
using Newtonsoft.Json;
namespace LeanKit.API.Client.Library.TransferObjects
{
[JsonObject(MemberSerialization = MemberSerialization.OptOut)]
public class Board
{
public virtual long Id { get; set; }
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual long Version { get; set; }
public virtual bool Active { get; set; }
public virtual long OrganizationId { get; set; }
public virtual IList<Activity> OrganizationActivities { get; set; }
public virtual IList<Lane> Lanes { get; set; }
public virtual IEnumerable<User> BoardUsers { get; set; }
public virtual IList<Lane> Backlog { get; set; }
public virtual IList<Lane> Archive { get; set; }
public virtual IList<ClassOfService> ClassesOfService { get; set; }
public virtual IList<CardType> CardTypes { get; set; }
public virtual bool ClassOfServiceEnabled { get; set; }
public virtual string CardColorField { get; set; }
public virtual bool IsCardIdEnabled { get; set; }
public virtual bool IsHeaderEnabled { get; set; }
public virtual bool IsPrefixEnabled { get; set; }
public virtual bool IsPrefixIncludedInHyperlink { get; set; }
public virtual string Prefix { get; set; }
public virtual bool IsHyperlinkEnabled { get; set; }
public virtual string Format { get; set; }
public virtual bool AllowMultiUserAssignments { get; set; }
public virtual bool BaseWipOnCardSize { get; set; }
public virtual bool ExcludeFromOrgAnalytics { get; set; }
public virtual bool ExcludeCompletedAndArchiveViolations { get; set; }
public virtual IList<long?> TopLevelLaneIds { get; set; }
public virtual long? BacklogTopLevelLaneId { get; set; }
public virtual long? ArchiveTopLevelLaneId { get; set; }
public IList<CardContext> CardContexts { get; set; }
public IList<ParentCard> ParentCards { get; set; }
public IEnumerable<Lane> AllLanes()
{
return new List<Lane>().Concat(Backlog).Concat(Lanes).Concat(Archive);
}
public CardView GetCardViewById(long cardId)
{
CardView cardView = Lanes.Where(x => x.Cards != null).SelectMany(l => l.Cards).FirstOrDefault(c => c.Id == cardId);
if (cardView == null)
{
cardView = Backlog.Where(x => x.Cards != null).SelectMany(lane => lane.Cards).FirstOrDefault(c => c.Id == cardId);
}
if (cardView == null)
{
cardView = Archive.Where(x => x.Cards != null).SelectMany(lane => lane.Cards).FirstOrDefault(c => c.Id == cardId);
}
return cardView;
}
public Card GetCardById(long cardId)
{
CardView cardView = GetCardViewById(cardId);
return cardView == null ? null : cardView.ToCard();
}
public Card GetCardByExternalId(string externalId)
{
if (IsPrefixEnabled && externalId.StartsWith(Prefix))
{
externalId = externalId.TrimStart(Prefix.ToCharArray());
}
CardView cardView = Lanes.SelectMany(lane => lane.Cards).FirstOrDefault(c => c.ExternalCardID == externalId);
if (cardView == null)
{
cardView = Backlog.SelectMany(lane => lane.Cards).FirstOrDefault(c => c.ExternalCardID == externalId);
}
if (cardView == null)
{
cardView = Archive.SelectMany(lane => lane.Cards).FirstOrDefault(c => c.ExternalCardID == externalId);
}
return cardView == null ? null : cardView.ToCard();
}
public Lane GetLaneById(long laneId)
{
Lane lane = Lanes.FirstOrDefault(x => x.Id == laneId);
if (lane == null)
{
lane = Backlog.FirstOrDefault(x => x.Id == laneId);
}
if (lane == null)
{
lane = Archive.FirstOrDefault(x => x.Id == laneId);
}
return lane;
}
public void UpdateLane(Lane laneToUpdateWith)
{
//Try getting the lane from the Lanes
Lane regularLaneToReplace = Lanes.FirstOrDefault(lane => lane.Id == laneToUpdateWith.Id);
if (regularLaneToReplace != null)
{
int laneIndex = Lanes.IndexOf(regularLaneToReplace);
Lanes.RemoveAt(laneIndex);
Lanes.Insert(laneIndex, laneToUpdateWith);
return;
}
//If none, try getting from Backlog
Lane backLogLaneToReplace = Backlog.FirstOrDefault(lane => lane.Id == laneToUpdateWith.Id);
if (backLogLaneToReplace != null)
{
int laneIndex = Backlog.IndexOf(backLogLaneToReplace);
Backlog.RemoveAt(laneIndex);
Backlog.Insert(laneIndex, laneToUpdateWith);
return;
}
//Lastly, get it from Archive
Lane archiveLaneToReplace = Archive.FirstOrDefault(lane => lane.Id == laneToUpdateWith.Id);
if (archiveLaneToReplace != null)
{
int laneIndex = Archive.IndexOf(archiveLaneToReplace);
Archive.RemoveAt(laneIndex);
Archive.Insert(laneIndex, laneToUpdateWith);
return;
}
throw new ItemNotFoundException("Could not find the Lane to replace with the updated Lane.");
}
public void ApplyCardMove(long cardId, long toLaneId, int position)
{
//get the card
CardView currentCard = GetCardViewById(cardId);
//remove from the lane it was in
Lane lane = GetLaneById(currentCard.LaneId);
int index = lane.Cards.IndexOf(currentCard);
foreach (CardView cardView in lane.Cards.OrderBy(card => card.Index))
{
if (cardView.Index > currentCard.Index)
{
cardView.Index--;
}
}
lane.Cards.RemoveAt(index);
//move the card into the new lane
bool inserted = false;
int indexToInsert = 0;
Lane destinationLane = GetLaneById(toLaneId);
if (destinationLane.Cards == null) destinationLane.Cards = new List<CardView>();
foreach (CardView cardView in destinationLane.Cards.OrderBy(card => card.Index))
{
if (cardView.Index >= position)
{
if (!inserted)
{
indexToInsert = destinationLane.Cards.IndexOf(cardView);
inserted = true;
}
else
{
cardView.Index++;
}
}
}
currentCard.Index = position;
destinationLane.Cards.Insert(indexToInsert, currentCard);
if (destinationLane.Id != null) currentCard.LaneId = (long) destinationLane.Id;
}
public void ApplyCardDelete(long cardId)
{
CardView cardToDelete = GetCardViewById(cardId);
IList<CardView> laneCards = GetLaneById(cardToDelete.Id).Cards;
for (int i = 0; i < laneCards.Count(); i++)
{
CardView currentCard = laneCards[i];
if (currentCard.Index > cardToDelete.Index)
{
currentCard.Index--;
}
}
laneCards.Remove(cardToDelete);
}
public string GetLaneTitle(long laneId)
{
long parentLaneId = 0;
Lane parentLane = Lanes.FirstOrDefault(x => x.ChildLaneIds != null && x.ChildLaneIds.Contains(laneId));
if (parentLane != null)
{
parentLaneId = parentLane.Id.GetValueOrDefault(0);
}
if (parentLaneId != 0)
{
return GetLaneTitle(parentLaneId) + ":" + GetLaneById(laneId).Title;
}
return GetLaneById(laneId).Title;
}
}
}