Commits

lfoust committed f61c1ce

Started implementing badge methods

Comments (0)

Files changed (11)

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

     <Compile Include="..\Stacky\Enums\BadgeClass.cs">
       <Link>Enums\BadgeClass.cs</Link>
     </Compile>
+    <Compile Include="..\Stacky\Enums\BadgeMinMax.cs">
+      <Link>Enums\BadgeMinMax.cs</Link>
+    </Compile>
+    <Compile Include="..\Stacky\Enums\BadgeSort.cs">
+      <Link>Enums\BadgeSort.cs</Link>
+    </Compile>
     <Compile Include="..\Stacky\Enums\CommentSort.cs">
       <Link>Enums\CommentSort.cs</Link>
     </Compile>

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

     <Compile Include="..\Stacky\Enums\BadgeClass.cs">
       <Link>Enums\BadgeClass.cs</Link>
     </Compile>
+    <Compile Include="..\Stacky\Enums\BadgeMinMax.cs">
+      <Link>Enums\BadgeMinMax.cs</Link>
+    </Compile>
+    <Compile Include="..\Stacky\Enums\BadgeSort.cs">
+      <Link>Enums\BadgeSort.cs</Link>
+    </Compile>
     <Compile Include="..\Stacky\Enums\CommentSort.cs">
       <Link>Enums\CommentSort.cs</Link>
     </Compile>

trunk/source/Stacky.WindowsPhone/Stacky.WindowsPhone.csproj

     <Compile Include="..\Stacky\Enums\BadgeClass.cs">
       <Link>Enums\BadgeClass.cs</Link>
     </Compile>
+    <Compile Include="..\Stacky\Enums\BadgeMinMax.cs">
+      <Link>Enums\BadgeMinMax.cs</Link>
+    </Compile>
+    <Compile Include="..\Stacky\Enums\BadgeSort.cs">
+      <Link>Enums\BadgeSort.cs</Link>
+    </Compile>
     <Compile Include="..\Stacky\Enums\CommentSort.cs">
       <Link>Enums\CommentSort.cs</Link>
     </Compile>

trunk/source/Stacky/Enums/BadgeMinMax.cs

+namespace Stacky
+{
+    public enum BadgeMinMax
+    {
+        TagBased,
+        Named,
+        Bronze,
+        Silver,
+        Gold
+    }
+}

trunk/source/Stacky/Enums/BadgeSort.cs

+namespace Stacky
+{
+    public enum BadgeSort
+    {
+        Rank,
+        Name,
+        Type
+    }
+}

trunk/source/Stacky/Helpers/EnumHelper.cs

                 return null;
             return attributes.FirstOrDefault();
         }
+
+        public static string GetQueryStringValue(this Enum value)
+        {
+            if (value == null)
+                return null;
+            List<char> result = new List<char>();
+            string valueString = value.ToString();
+
+            for (int i = 0; i < valueString.Length; i++)
+            {
+                char item = valueString[i];
+                if (i != 0 && Char.IsUpper(item))
+                {
+                    result.Add('_');
+                }
+                result.Add(Char.ToLower(item));
+            }
+
+            return new string(result.ToArray());
+        }
     }
 }

trunk/source/Stacky/Stacky.csproj

     <Compile Include="Entities\UserEvent.cs" />
     <Compile Include="Enums\AnswerSort.cs" />
     <Compile Include="Enums\BadgeClass.cs" />
+    <Compile Include="Enums\BadgeMinMax.cs" />
+    <Compile Include="Enums\BadgeSort.cs" />
     <Compile Include="Enums\CommentSort.cs" />
     <Compile Include="Enums\ErrorCode.cs" />
     <Compile Include="Enums\FavoriteQuestionsSort.cs" />

trunk/source/Stacky/StackyClient/BadgeMethods.cs

 {
     public partial class StackyClient
     {
-        public virtual IEnumerable<Badge> GetBadges()
+        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)
         {
-            return GetBadges("badges", null);
+            var response = MakeRequest<Badge>("badges", null, new
+            {
+                sort = GetEnumValue(sortBy),
+                order = GetSortDirection(sortDirection),
+                page = page ?? null,
+                pagesize = pageSize ?? null,
+                fromDate = GetDateValue(fromdate),
+                toDate = GetDateValue(toDate),
+                min = GetEnumValue(min),
+                max = GetEnumValue(max)
+            });
+            return new PagedList<Badge>(response);
         }
 
         private IEnumerable<Badge> GetBadges(string method, string[] urlArguments)

trunk/source/Stacky/StackyClientBase.cs

 using System;
+using System.Collections.Generic;
 
 namespace Stacky
 {
             return null;
         }
 
-        protected string GetSortValue(object sort)
+        protected static string GetEnumValue(object sort)
         {
-            if (sort == null)
-                return null;
-            return sort.ToString().ToLower();
+            return EnumHelper.GetQueryStringValue((Enum)sort);
         }
 
         protected long? GetDateValue(DateTime? date)

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

+using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Stacky.UnitTests
+{
+    [TestClass]
+    public class EnumTests
+    {
+        [TestMethod]
+        public void GetEnumValue_DetectsNull()
+        {
+            string result = EnumHelper.GetQueryStringValue(null);
+            Assert.IsNull(result);
+        }
+
+        [TestMethod]
+        public void GetEnumValue_ConvertsToLowerCase()
+        {
+            string result = BadgeSort.Rank.GetQueryStringValue();
+            Assert.AreEqual("rank", result);
+        }
+
+        [TestMethod]
+        public void GetEnumValue_UnderscoresForCamelCasing()
+        {
+            string result = BadgeMinMax.TagBased.GetQueryStringValue();
+            Assert.AreEqual("tag_based", result);
+        }
+    }
+}

trunk/source/Tests/Stacky.UnitTests/Stacky.UnitTests.csproj

     </CodeAnalysisDependentAssemblyPaths>
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="EnumTests.cs" />
     <Compile Include="PagerTests.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="UrlHelperTests.cs" />