Commits

lfoust committed ba39825

Added Badge methods. No Integration tests yet.

Comments (0)

Files changed (3)

trunk/source/Stacky/StackyClient/BadgeMethods.cs

-using System.Collections.Generic;
-using System;
+using System;
+using System.Collections.Generic;
+using System.Linq;
 
 namespace Stacky
 {
     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, string filter = 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,
-                pagesize = pageSize ?? null,
-                fromDate = GetDateValue(fromdate),
-                toDate = GetDateValue(toDate),
-                min = GetEnumValue(min),
-                max = GetEnumValue(max),
-				filter = filter
-            });
-            return new PagedList<Badge>(response);
+			return Execute<Badge, BadgeMinMax>("badges", null,
+				sortBy, sortDirection, page, pageSize, fromDate, toDate, min, max, filter);
         }
 
-        private IEnumerable<Badge> GetBadges(string method, string[] urlArguments)
-        {
-            return MakeRequest<Badge>(method, urlArguments, new
-            {
-                site = this.SiteUrlName,
-            }).Items;
-        }
-
-        public virtual IEnumerable<User> GetUsersByBadge(int badgeId, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null)
-        {
-            return GetUsersByBadge(badgeId.ToArray(), page, pageSize, fromDate, toDate);
-        }
-
-        public virtual IPagedList<User> GetUsersByBadge(IEnumerable<int> badgeIds, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null)
-        {
-            var response = MakeRequest<User>("badges", new string[] { badgeIds.Vectorize() }, new
-            {
-                site = this.SiteUrlName,
-                page = page ?? null,
-                pagesize = pageSize ?? null,
-                fromdate = fromDate.HasValue ? (long?)fromDate.Value.ToUnixTime() : null,
-                todate = toDate.HasValue ? (long?)toDate.Value.ToUnixTime() : null
-            });
-            return new PagedList<User>(response);
-        }
-
-		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)
+		public virtual Badge GetBadge(int id, 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);
+			return GetBadges(id.ToArray(), filter: filter).FirstOrDefault();
 		}
 
-        public virtual IEnumerable<Badge> GetUserBadges(int userId)
-        {
-            return GetUserBadges(userId.ToArray());
-        }
+		public virtual IPagedList<Badge> GetBadges(IEnumerable<int> ids, BadgeSort? sortBy = null, SortDirection? sortDirection = null, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null, DateTime? min = null, DateTime? max = null, string filter = null)
+		{
+			ValidateVectorizedParameters(ids);
+			return Execute<Badge>("badges", new string[] { ids.Vectorize() },
+				sortBy, sortDirection, page, pageSize, fromDate, toDate, min, max, filter);
+		}
 
-        public virtual IEnumerable<Badge> GetUserBadges(IEnumerable<int> userIds)
-        {
-            return MakeRequest<Badge>("users", new string[] { userIds.Vectorize(), "badges" }, new
-            {
-                site = this.SiteUrlName
-            }).Items;
-        }
+		public virtual IPagedList<Badge> GetNamedBadges(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)
+		{
+			return Execute<Badge, BadgeMinMax>("badges", new string[] { "name" },
+				sortBy, sortDirection, page, pageSize, fromDate, toDate, min, max, filter);
+		}
+
+		public virtual IPagedList<Badge> GetRecentlyAwardedBadges(int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null, string filter = null)
+		{
+			return Execute<Badge, BadgeMinMax>("badges", new string[] { "recipients" },
+				null, null, page, pageSize, fromDate, toDate, null, null, filter);
+		}
+
+		public virtual IPagedList<Badge> GetRecentlyAwardedBadges(int id, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null, string filter = null)
+		{
+			return GetRecentlyAwardedBadges(id.ToArray(), page, pageSize, fromDate, toDate, filter);
+		}
+
+		public virtual IPagedList<Badge> GetRecentlyAwardedBadges(IEnumerable<int> ids, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null, string filter = null)
+		{
+			ValidateVectorizedParameters(ids);
+			return Execute<Badge, BadgeMinMax>("badges", new string[] { ids.Vectorize(), "recipients" },
+				null, null, page, pageSize, fromDate, toDate, null, null, filter);
+		}
+
+		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)
+		{
+			return Execute<Badge, BadgeMinMax>("badges", new string[] { "tags" },
+				sortBy, sortDirection, page, pageSize, fromDate, toDate, min, max, filter);
+		}
+        
     }
 }

trunk/source/Stacky/StackyClient/StackyClient.cs

             return WebClient.MakeRequest(url);
         }
 
-        protected IPagedList<TEntity> Execute<TEntity, TSort, TMinMax>(string methodName, string[] urlArguments, TSort? sortBy = null, SortDirection? sortDirection = null, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null, TMinMax? min = null, TMinMax? max = null, string filter = null)
+        protected IPagedList<TEntity> Execute<TEntity, TMinMax>(string methodName, string[] urlArguments, object sortBy = null, SortDirection? sortDirection = null, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null, TMinMax? min = null, TMinMax? max = null, string filter = null)
             where TEntity : Entity, new()
-            where TSort : struct
             where TMinMax : struct
         {
             var response = MakeRequest<TEntity>(methodName, urlArguments, new
             return new PagedList<TEntity>(response);
         }
 
+		protected void ValidateVectorizedParameters(IEnumerable<int> items)
+		{
+			if (items.Count() > 100)
+				throw new ArgumentOutOfRangeException("Paramaterized arguments cannot have more than 100 items in them");
+		}
+
         #endregion
 
         #region Properties

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

             Assert.IsNotNull(badges);
         }
 
-        [TestMethod]
-        public void Badge_GetUsersByBadge()
-        {
-            var users = Client.GetUsersByBadge(204);
-            Assert.IsNotNull(users);
-        }
+		//[TestMethod]
+		//public void Badge_GetUsersByBadge()
+		//{
+		//    var users = Client.GetUsersByBadge(204);
+		//    Assert.IsNotNull(users);
+		//}
 
         [TestMethod]
         public void Badge_GetTagBasedBadges()
 				Assert.AreEqual(BadgeType.TagBased, badge.Type);
         }
 
-        [TestMethod]
-        public void Bug_2_GetUserBadges()
-        {
-            var badges = Client.GetUserBadges(1);
-            Assert.IsNotNull(badges);
-        }
+		//[TestMethod]
+		//public void Bug_2_GetUserBadges()
+		//{
+		//    var badges = Client.GetUserBadges(1);
+		//    Assert.IsNotNull(badges);
+		//}
     }
 }