Commits

lfoust  committed 3c39545

Added some Badge methods

  • Participants
  • Parent commits 2f3a147
  • Branches V2.0

Comments (0)

Files changed (9)

File trunk/source/Stacky.Net35/Stacky.Net35.csproj

     </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
+    <Reference Include="System.Runtime.Serialization" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />
     <Reference Include="System.Data" />

File trunk/source/Stacky.Silverlight/Stacky.Silverlight.csproj

     <Reference Include="Newtonsoft.Json">
       <HintPath>..\packages\Newtonsoft.Json.4.0.4\lib\sl4\Newtonsoft.Json.dll</HintPath>
     </Reference>
+    <Reference Include="System.Runtime.Serialization" />
     <Reference Include="System.Windows" />
     <Reference Include="system" />
     <Reference Include="System.Core" />

File trunk/source/Stacky/Entities/Badge.cs

 using Newtonsoft.Json;
+using Newtonsoft.Json.Converters;
 
 namespace Stacky
 {
             set { awardCount = value; NotifyOfPropertyChange(() => AwardCount); }
         }
 
-        [JsonProperty("badge_type")]
+        [JsonProperty("badge_type"), JsonConverter(typeof(StringEnumConverter))]
         public BadgeType Type
         {
             get { return type; }

File trunk/source/Stacky/Enums/BadgeClass.cs

-namespace Stacky
+using System.Runtime.Serialization;
+namespace Stacky
 {
     /// <summary>
     /// Specifies the badge type.
     public enum BadgeType
     {
         Named,
+		[EnumMember(Value = "tag_based")]
         TagBased
     }
 }

File trunk/source/Stacky/Helpers/EnumHelper.cs

 
             return new string(result.ToArray());
         }
-    }
+
+		public static bool IsEnum(Type type)
+		{
+			Type t = (EnumHelper.IsNullableType(type))
+			? Nullable.GetUnderlyingType(type)
+			: type;
+
+			return t.IsEnum;
+		}
+
+		public static bool IsNullable(Type type)
+		{
+			if (type == null)
+				throw new ArgumentNullException("type");
+
+			if (type.IsValueType)
+				return IsNullableType(type);
+
+			return true;
+		}
+
+		public static bool IsNullableType(Type type)
+		{
+			if (type == null)
+				throw new ArgumentNullException("type");
+
+			return (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>));
+		}
+
+		// TODO: Consider removing this method and it's associated tests as it is currently not being used
+		public static string ParseQueryStringValue(string value)
+		{
+			List<char> result = new List<char>();
+			for (int i = 0; i < value.Length; i++)
+			{
+				char c = value[i];
+				if (i == 0)
+				{
+					result.Add(Char.ToUpper(c));
+				}
+				else if (c == '_')
+				{
+					// skip the underscore and capitalize the next letter
+					result.Add(Char.ToUpper(value[++i]));
+				}
+				else
+				{
+					result.Add(c);
+				}
+			}
+			return new string(result.ToArray());
+		}
+	}
 }

File trunk/source/Stacky/Stacky.csproj

     <Reference Include="System" />
     <Reference Include="System.configuration" />
     <Reference Include="System.Core" />
+    <Reference Include="System.Runtime.Serialization" />
     <Reference Include="System.Web" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />

File trunk/source/Stacky/StackyClient/BadgeMethods.cs

 {
     public partial class StackyClient
     {
-        public virtual IPagedList<Badge> GetBadges(BadgeSort? sortBy = null, SortDirection? sortDirection = null, int? page = null, int? pageSize = null, DateTime? fromdate = null, DateTime? toDate = null, BadgeMinMax? min = null, BadgeMinMax? max = null)
+        public virtual IPagedList<Badge> GetBadges(BadgeSort? sortBy = null, SortDirection? sortDirection = null, int? page = null, int? pageSize = null, DateTime? fromdate = null, DateTime? toDate = null, BadgeMinMax? min = null, BadgeMinMax? max = null, string filter = null)
         {
             var response = MakeRequest<Badge>("badges", null, new
             {
+				site = this.SiteUrlName,
                 sort = GetEnumValue(sortBy),
                 order = GetSortDirection(sortDirection),
                 page = page ?? null,
                 fromDate = GetDateValue(fromdate),
                 toDate = GetDateValue(toDate),
                 min = GetEnumValue(min),
-                max = GetEnumValue(max)
+                max = GetEnumValue(max),
+				filter = filter
             });
             return new PagedList<Badge>(response);
         }
             return new PagedList<User>(response);
         }
 
-        public virtual IEnumerable<Badge> GetTagBasedBadges()
-        {
-            return GetBadges("badges", new string[] { "tags" });
-        }
+		public virtual IPagedList<Badge> GetTagBasedBadges(BadgeSort? sortBy = null, SortDirection? sortDirection = null, int? page = null, int? pageSize = null, DateTime? fromdate = null, DateTime? toDate = null, BadgeMinMax? min = null, BadgeMinMax? max = null, string filter = null)
+		{
+			var response = MakeRequest<Badge>("badges", new string[] { "tags" }, new
+			{
+				site = this.SiteUrlName,
+				sort = GetEnumValue(sortBy),
+				order = GetSortDirection(sortDirection),
+				page = page ?? null,
+				pagesize = pageSize ?? null,
+				fromDate = GetDateValue(fromdate),
+				toDate = GetDateValue(toDate),
+				min = GetEnumValue(min),
+				max = GetEnumValue(max),
+				filter = filter
+			});
+			return new PagedList<Badge>(response);
+		}
 
         public virtual IEnumerable<Badge> GetUserBadges(int userId)
         {

File trunk/source/Tests/Stacky.IntegrationTests/BadgeTests.cs

         {
             var badges = Client.GetTagBasedBadges();
             Assert.IsNotNull(badges);
+			foreach (var badge in badges)
+				Assert.AreEqual(BadgeType.TagBased, badge.Type);
         }
 
         [TestMethod]

File trunk/source/Tests/Stacky.UnitTests/EnumTests.cs

     public class EnumTests
     {
         [TestMethod]
-        public void GetEnumValue_DetectsNull()
+		public void GetQueryStringValue_DetectsNull()
         {
             string result = EnumHelper.GetQueryStringValue(null);
             Assert.IsNull(result);
         }
 
         [TestMethod]
-        public void GetEnumValue_ConvertsToLowerCase()
+		public void GetQueryStringValue_ConvertsToLowerCase()
         {
             string result = BadgeSort.Rank.GetQueryStringValue();
             Assert.AreEqual("rank", result);
         }
 
         [TestMethod]
-        public void GetEnumValue_UnderscoresForCamelCasing()
+		public void GetQueryStringValue_UnderscoresForCamelCasing()
         {
             string result = BadgeMinMax.TagBased.GetQueryStringValue();
             Assert.AreEqual("tag_based", result);
         }
+
+		[TestMethod]
+		public void ParseQueryStringValue_CapitalizedFirstLetter()
+		{
+			string result = EnumHelper.ParseQueryStringValue("go");
+			Assert.AreEqual("Go", result);
+		}
+
+		[TestMethod]
+		public void ParseQueryStringValue_UnderscoreRemovedAndCamelCased()
+		{
+			string result = EnumHelper.ParseQueryStringValue("remember_the_milk");
+			Assert.AreEqual("RememberTheMilk", result);
+		}
     }
 }