Repository navigation
Expand file tree
/
Copy pathCollectionExtensions.cs
More file actions
88 lines (72 loc) · 2.5 KB
/
Copy pathCollectionExtensions.cs
File metadata and controls
88 lines (72 loc) · 2.5 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
//------------------------------------------------------------------------------
// <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 LeanKit.API.Client.Library.TransferObjects;
namespace LeanKit.API.Client.Library.Extensions
{
public static class CollectionExtensions
{
public static Card FindContainedCard(this IEnumerable<Lane> lanes, long laneId, long cardId)
{
var list = lanes as IList<Lane> ?? lanes.ToList();
if (list.ContainsLane(laneId))
{
var candidateLane = list.FindLane(laneId);
//Get the card from the lane
var candidateCard = candidateLane.Cards.FirstOrDefault(card => card.Id == cardId);
if (candidateCard != null) return candidateCard.ToCard();
}
//the card may have been moved so look in all the affected lanes
var candidateCard2 = list.SelectMany(x => x.Cards).FirstOrDefault(card => card.Id == cardId);
if (candidateCard2 == null)
{
throw new ItemNotFoundException(string.Format("Unable to find the Card [{0}] in the associated Lane [{1}].", cardId,
laneId));
}
return candidateCard2.ToCard();
}
public static Lane FindLane(this IEnumerable<Lane> lanes, long laneId)
{
var candidateLane = lanes.FirstOrDefault(lane => lane.Id == laneId);
if (candidateLane == null)
{
throw new ItemNotFoundException(string.Format("Unable to find Lane [{0}].", laneId));
}
return candidateLane;
}
public static bool ContainsLane(this IEnumerable<Lane> lanes, long laneId)
{
return lanes.Any(lane => lane.Id == laneId);
}
public static bool ContainsCard(this IEnumerable<Lane> lanes, long cardId)
{
return lanes.SelectMany(lane => lane.Cards).Any(card => card.Id == cardId);
}
public static User FindUser(this IEnumerable<User> boardUsers, long userId)
{
var candidateUser = boardUsers.FirstOrDefault(user => user.Id == userId);
if (candidateUser == null)
{
throw new ItemNotFoundException(string.Format("Unable to find User [{0}].", userId));
}
return candidateUser;
}
public static IEnumerable<Lane> GetFlatLanes(this IEnumerable<HierarchicalLane> lanes)
{
foreach (var c in lanes)
{
yield return c.Lane;
if (c.ChildLanes.Count <= 0) continue;
foreach (var lane in GetFlatLanes(c.ChildLanes))
{
yield return lane;
}
}
}
}
}