diff --git a/.gitignore b/.gitignore index 9a40f6f..9900991 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ obj/ _ReSharper*/ [Tt]est[Rr]esult* *.nupkg -packages/ \ No newline at end of file +packages/ +TrelloNet.sln.ide \ No newline at end of file diff --git a/TrelloNet.Tests/BoardTests.cs b/TrelloNet.Tests/BoardTests.cs index bfdbf93..9d5526f 100644 --- a/TrelloNet.Tests/BoardTests.cs +++ b/TrelloNet.Tests/BoardTests.cs @@ -372,7 +372,7 @@ private static ExpectedObject CreateExpectedWelcomeBoard() Name = "Welcome Board", Desc = "A test description", IdOrganization = Constants.TestOrganizationId, - Pinned = true, + Pinned = false, Url = "https://trello.com/b/J9JUdoYV/welcome-board", Id = Constants.WelcomeBoardId, Prefs = new BoardPreferences @@ -395,6 +395,10 @@ private static Dictionary CreateExpectedWelcomeBoardLabels() { Color.Orange, "" }, { Color.Green, "label name" }, { Color.Blue, "" }, + { Color.Pink, "" }, + { Color.Sky, "" }, + { Color.Lime, "" }, + { Color.Black, "" }, }; } } diff --git a/TrelloNet.Tests/SearchTests.cs b/TrelloNet.Tests/SearchTests.cs index bc566cc..d01698e 100644 --- a/TrelloNet.Tests/SearchTests.cs +++ b/TrelloNet.Tests/SearchTests.cs @@ -50,7 +50,7 @@ public void Search_WithTestQuery_ReturnsCorrectBoard() Name = "Welcome Board", Desc = "A test description", IdOrganization = Constants.TestOrganizationId, - Pinned = true, + Pinned = false, Url = "https://trello.com/b/J9JUdoYV/welcome-board", Id = Constants.WelcomeBoardId, Prefs = new BoardPreferences @@ -62,12 +62,16 @@ public void Search_WithTestQuery_ReturnsCorrectBoard() }, LabelNames = new Dictionary { + { Color.Green, "label name" }, { Color.Yellow, "" }, + { Color.Orange, "" }, { Color.Red, "" }, { Color.Purple, "" }, - { Color.Orange, "" }, - { Color.Green, "label name" }, { Color.Blue, "" }, + { Color.Pink, "" }, + { Color.Sky, "" }, + { Color.Lime, "" }, + { Color.Black, "" }, } }.ToExpectedObject(); diff --git a/TrelloNet/Boards/Internal/BoardsChangeLabelNameRequest.cs b/TrelloNet/Boards/Internal/BoardsChangeLabelNameRequest.cs index 56c85bd..37df939 100644 --- a/TrelloNet/Boards/Internal/BoardsChangeLabelNameRequest.cs +++ b/TrelloNet/Boards/Internal/BoardsChangeLabelNameRequest.cs @@ -7,7 +7,9 @@ internal class BoardsChangeLabelNameRequest : BoardsRequest public BoardsChangeLabelNameRequest(IBoardId board, Color color, string name) : base(board, "labelNames/{color}", Method.PUT) { - AddParameter("color", color.ToTrelloString(), ParameterType.UrlSegment); + Guard.NotNull(color, "color"); + + AddParameter("color", color.ColorName, ParameterType.UrlSegment); this.AddValue(name ?? ""); } } diff --git a/TrelloNet/Boards/Internal/ColorConverter.cs b/TrelloNet/Boards/Internal/ColorConverter.cs new file mode 100644 index 0000000..82bfc21 --- /dev/null +++ b/TrelloNet/Boards/Internal/ColorConverter.cs @@ -0,0 +1,33 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace TrelloNet.Internal +{ + internal class ColorConverter : TypeConverter + { + public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) + { + return sourceType == typeof (string); + } + + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) + { + // color can be null in uncolored label attached to a card. + if (value == null) return null; + + var colorName = value as string; + if (colorName != null) + { + return new Color(colorName); + } + else + { + throw new NotSupportedException( + string.Format( + "Conversion from {0} to Color is not supported", + value.GetType().Name)); + } + } + } +} \ No newline at end of file diff --git a/TrelloNet/Cards/Card.cs b/TrelloNet/Cards/Card.cs index e4cc279..ef06ae9 100644 --- a/TrelloNet/Cards/Card.cs +++ b/TrelloNet/Cards/Card.cs @@ -25,7 +25,7 @@ public class Card : ICardId, IUpdatableCard public double Pos { get; set; } public DateTime DateLastActivity { get; set; } public List IdMembers { get; set; } - public IEnumerable LabelColors { get { return Labels == null ? Enumerable.Empty() : Labels.Select(l => l.Color); } } + public IEnumerable LabelColors { get { return Labels == null ? Enumerable.Empty() : Labels.Where(l => l.Color != null).Select(l => l.Color); } } public string GetCardId() { diff --git a/TrelloNet/Cards/Color.cs b/TrelloNet/Cards/Color.cs index c6a312c..8f8af5c 100644 --- a/TrelloNet/Cards/Color.cs +++ b/TrelloNet/Cards/Color.cs @@ -1,12 +1,64 @@ +using System; +using System.ComponentModel; +using TrelloNet.Internal; + namespace TrelloNet { - public enum Color + [TypeConverter(typeof(ColorConverter))] + public class Color { - Green, - Yellow, - Orange, - Red, - Purple, - Blue + // Known colors: + public static readonly Color Green = new Color("green"); + public static readonly Color Yellow = new Color("yellow"); + public static readonly Color Orange = new Color("orange"); + public static readonly Color Red = new Color("red"); + public static readonly Color Purple = new Color("purple"); + public static readonly Color Blue = new Color("blue"); + public static readonly Color Pink = new Color("pink"); + public static readonly Color Sky = new Color("sky"); + public static readonly Color Lime = new Color("lime"); + public static readonly Color Black = new Color("black"); + + public string ColorName { get; private set; } + + public Color(string colorName) + { + if (colorName == null) throw new ArgumentNullException("colorName"); + + ColorName = colorName.ToLower(); + } + + public override string ToString() + { + return ColorName; + } + + protected bool Equals(Color other) + { + return string.Equals(ColorName, other.ColorName); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != GetType()) return false; + return Equals((Color)obj); + } + + public override int GetHashCode() + { + return (ColorName != null ? ColorName.GetHashCode() : 0); + } + + public static bool operator ==(Color left, Color right) + { + return Equals(left, right); + } + + public static bool operator !=(Color left, Color right) + { + return !Equals(left, right); + } } } \ No newline at end of file diff --git a/TrelloNet/Cards/Internal/CardsAddLabelRequest.cs b/TrelloNet/Cards/Internal/CardsAddLabelRequest.cs index 486933a..4ab6c66 100644 --- a/TrelloNet/Cards/Internal/CardsAddLabelRequest.cs +++ b/TrelloNet/Cards/Internal/CardsAddLabelRequest.cs @@ -7,7 +7,9 @@ internal class CardsAddLabelRequest : CardsRequest public CardsAddLabelRequest(ICardId card, Color color) : base(card, "labels", Method.POST) { - this.AddValue(color.ToTrelloString()); + Guard.NotNull(color, "color"); + + this.AddValue(color.ColorName); } } } \ No newline at end of file diff --git a/TrelloNet/Cards/Internal/CardsRemoveLabelRequest.cs b/TrelloNet/Cards/Internal/CardsRemoveLabelRequest.cs index 270cc18..af27ba4 100644 --- a/TrelloNet/Cards/Internal/CardsRemoveLabelRequest.cs +++ b/TrelloNet/Cards/Internal/CardsRemoveLabelRequest.cs @@ -7,7 +7,9 @@ internal class CardsRemoveLabelRequest : CardsRequest public CardsRemoveLabelRequest(ICardId card, Color color) : base(card, "labels/{color}", Method.DELETE) { - AddParameter("color", color.ToTrelloString(), ParameterType.UrlSegment); + Guard.NotNull(color, "color"); + + AddParameter("color", color.ColorName, ParameterType.UrlSegment); } } } \ No newline at end of file diff --git a/TrelloNet/Cards/Internal/CardsUpdateRequest.cs b/TrelloNet/Cards/Internal/CardsUpdateRequest.cs index d1f20f4..b06859f 100644 --- a/TrelloNet/Cards/Internal/CardsUpdateRequest.cs +++ b/TrelloNet/Cards/Internal/CardsUpdateRequest.cs @@ -19,7 +19,7 @@ public CardsUpdateRequest(IUpdatableCard card) AddParameter("closed", card.Closed.ToTrelloString()); AddParameter("idList", card.IdList); AddParameter("due", card.Due == null ? null : new DateTimeOffset(card.Due.Value).ToString(CultureInfo.InvariantCulture)); - AddParameter("labels", string.Join(",", card.LabelColors.Select(c => c.ToTrelloString()))); + AddParameter("labels", string.Join(",", card.LabelColors.Select(c => c.ColorName))); } } } \ No newline at end of file diff --git a/TrelloNet/TrelloNet.csproj b/TrelloNet/TrelloNet.csproj index 9f99ff8..5f3921a 100644 --- a/TrelloNet/TrelloNet.csproj +++ b/TrelloNet/TrelloNet.csproj @@ -123,6 +123,7 @@ +