Commits

lfoust committed b0b6756

Added GetCommentsByPost method

Comments (0)

Files changed (9)

trunk/source/Stacky.Net35/Options/CommentsByPostOptions.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Stacky
+{
+    public class CommentsByPostOptions
+    {
+        public CommentSort SortBy = CommentSort.Creation;
+        public SortDirection SortDirection = SortDirection.Descending;
+        public int? Page = null;
+        public int? PageSize = null;
+        public DateTime? FromDate = null;
+        public DateTime? ToDate = null;
+    }
+}

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

     <Compile Include="Options\AnswerOptions.cs" />
     <Compile Include="Options\BadgeByUserOptions.cs" />
     <Compile Include="Options\CommentOptions.cs" />
+    <Compile Include="Options\CommentsByPostOptions.cs" />
     <Compile Include="Options\FavoriteQuestionOptions.cs" />
     <Compile Include="Options\QuestionByUserOptions.cs" />
     <Compile Include="Options\QuestionSearchOptions.cs" />

trunk/source/Stacky.Net35/StackyClient/CommentMethods.cs

         {
             return GetComments(fromUserId.ToArray(), options);
         }
+
+        public virtual IPagedList<Comment> GetCommentsByPost(int postId)
+        {
+            return GetCommentsByPost(postId, new CommentsByPostOptions());
+        }
+
+        public virtual IPagedList<Comment> GetCommentsByPost(int postId, CommentsByPostOptions options)
+        {
+            return GetCommentsByPost(postId.ToArray(), options);
+        }
+
+        public virtual IPagedList<Comment> GetCommentsByPost(IEnumerable<int> postIds)
+        {
+            return GetCommentsByPost(postIds, new CommentsByPostOptions());
+        }
+
+        public virtual IPagedList<Comment> GetCommentsByPost(IEnumerable<int> postIds, CommentsByPostOptions options)
+        {
+            var response = MakeRequest<CommentResponse>("posts", new string[] { postIds.Vectorize(), "comments" }, new
+            {
+                key = apiKey,
+                page = options.Page ?? null,
+                pagesize = options.PageSize ?? null,
+                fromdate = options.FromDate.HasValue ? (long?)options.FromDate.Value.ToUnixTime() : null,
+                todate = options.ToDate.HasValue ? (long?)options.ToDate.Value.ToUnixTime() : null,
+                sort = options.SortBy.ToString().ToLower(),
+                order = GetSortDirection(options.SortDirection),
+            });
+            return new PagedList<Comment>(response.Comments, response);
+        } 
     }
 }

trunk/source/Stacky.Net35/StackyClientAsync/CommentMethods.cs

         {
             GetComments(fromUserId.ToArray(), onSuccess, onError, options);
         }
+
+        public void GetCommentsByPost(int postId, Action<IPagedList<Comment>> onSuccess, Action<ApiException> onError)
+        {
+            GetCommentsByPost(postId, onSuccess, onError, new CommentsByPostOptions());
+        }
+
+        public void GetCommentsByPost(int postId, Action<IPagedList<Comment>> onSuccess, Action<ApiException> onError, CommentsByPostOptions options)
+        {
+            GetCommentsByPost(postId.ToArray(), onSuccess, onError, options);
+        }
+
+        public void GetCommentsByPost(IEnumerable<int> postIds, Action<IPagedList<Comment>> onSuccess, Action<ApiException> onError)
+        {
+            GetCommentsByPost(postIds, onSuccess, onError, new CommentsByPostOptions());
+        }
+
+        public void GetCommentsByPost(IEnumerable<int> postIds, Action<IPagedList<Comment>> onSuccess, Action<ApiException> onError, CommentsByPostOptions options)
+        {
+            MakeRequest<CommentResponse>("posts", new string[] { postIds.Vectorize(), "comments" }, new
+            {
+                key = apiKey,
+                page = options.Page ?? null,
+                pagesize = options.PageSize ?? null,
+                fromdate = options.FromDate.HasValue ? (long?)options.FromDate.Value.ToUnixTime() : null,
+                todate = options.ToDate.HasValue ? (long?)options.ToDate.Value.ToUnixTime() : null,
+                sort = options.SortBy.ToString().ToLower(),
+                order = GetSortDirection(options.SortDirection),
+            }, (items) => onSuccess(new PagedList<Comment>(items.Comments, items)), onError);
+        } 
     }
 }

trunk/source/Stacky/StackyClient/CommentMethods.cs

         {
             return GetComments(fromUserId.ToArray(), sortBy, sortDirection, toUserId, page, pageSize, fromDate, toDate);
         }
+
+        public virtual IPagedList<Comment> GetCommentsByPost(int postId, CommentSort sortBy = CommentSort.Creation, SortDirection sortDirection = SortDirection.Descending, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null)
+        {
+            return GetCommentsByPost(postId.ToArray(), sortBy, sortDirection, page, pageSize, fromDate, toDate);
+        }
+
+        public virtual IPagedList<Comment> GetCommentsByPost(IEnumerable<int> postIds, CommentSort sortBy = CommentSort.Creation, SortDirection sortDirection = SortDirection.Descending, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null)
+        {
+            var response = MakeRequest<CommentResponse>("posts", new string[] { postIds.Vectorize(), "comments" }, new
+            {
+                key = apiKey,
+                page = page ?? null,
+                pagesize = pageSize ?? null,
+                fromdate = fromDate.HasValue ? (long?)fromDate.Value.ToUnixTime() : null,
+                todate = toDate.HasValue ? (long?)toDate.Value.ToUnixTime() : null,
+                sort = sortBy.ToString().ToLower(),
+                order = GetSortDirection(sortDirection),
+            });
+            return new PagedList<Comment>(response.Comments, response);
+        } 
     }
 }

trunk/source/Stacky/StackyClientAsync/CommentMethods.cs

         {
             GetComments(fromUserId.ToArray(), onSuccess, onError, sortBy, sortDirection, toUserId, page, pageSize, fromDate, toDate);
         }
+
+        public virtual void GetCommentsByPost(int postId, Action<IPagedList<Comment>> onSuccess, Action<ApiException> onError = null, CommentSort sortBy = CommentSort.Creation, SortDirection sortDirection = SortDirection.Descending, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null)
+        {
+            GetCommentsByPost(postId.ToArray(), onSuccess, onError, sortBy, sortDirection, page, pageSize, fromDate, toDate);
+        }
+
+        public virtual void GetCommentsByPost(IEnumerable<int> postIds, Action<IPagedList<Comment>> onSuccess, Action<ApiException> onError = null, CommentSort sortBy = CommentSort.Creation, SortDirection sortDirection = SortDirection.Descending, int? page = null, int? pageSize = null, DateTime? fromDate = null, DateTime? toDate = null)
+        {
+            MakeRequest<CommentResponse>("posts", new string[] { postIds.Vectorize(), "comments" }, new
+            {
+                key = apiKey,
+                page = page ?? null,
+                pagesize = pageSize ?? null,
+                fromdate = fromDate.HasValue ? (long?)fromDate.Value.ToUnixTime() : null,
+                todate = toDate.HasValue ? (long?)toDate.Value.ToUnixTime() : null,
+                sort = sortBy.ToString().ToLower(),
+                order = GetSortDirection(sortDirection),
+            }, (items) => onSuccess(new PagedList<Comment>(items.Comments, items)), onError);
+        }
     }
 }

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

             Assert.IsNotNull(comment.Owner);
             Assert.IsNotNull(comment.Owner.DisplayName);
         }
+
+        [TestMethod]
+        public void GetCommentsByPost_ByQuestionId()
+        {
+            var comments = Client.GetCommentsByPost(9033);
+            Assert.IsNotNull(comments);
+            Assert.AreEqual(PostType.Question, comments.FirstOrDefault().PostType);
+        }
+
+        [TestMethod]
+        public void GetCommentsByPost_ByAnswerId()
+        {
+            var comments = Client.GetCommentsByPost(11738);
+            Assert.IsNotNull(comments);
+            Assert.AreEqual(PostType.Answer, comments.FirstOrDefault().PostType);
+        }
     }
 }

trunk/source/Tests/Stacky.Net35.IntegrationTests/CommentTests.cs

             Assert.IsTrue(comments.CurrentPage > 0);
             Assert.IsTrue(comments.TotalItems > 0);
         }
+
+        [TestMethod]
+        public void GetCommentsByPost_ByQuestionId()
+        {
+            var comments = Client.GetCommentsByPost(9033);
+            Assert.IsNotNull(comments);
+            Assert.AreEqual(PostType.Question, comments.FirstOrDefault().PostType);
+        }
+
+        [TestMethod]
+        public void GetCommentsByPost_ByAnswerId()
+        {
+            var comments = Client.GetCommentsByPost(11738);
+            Assert.IsNotNull(comments);
+            Assert.AreEqual(PostType.Answer, comments.FirstOrDefault().PostType);
+        }
     }
 }

trunk/source/Tests/Stacky.Silverlight.IntegrationTests/CommentTests.cs

                 EnqueueCallback(() => Assert.IsTrue(received.TotalItems > 0));
             }
         }
+
+        [TestMethod, Asynchronous]
+        public void GetCommentsByPost_ByQuestionId()
+        {
+            using (var context = new AsynchronusTestContext(this))
+            {
+                IPagedList<Comment> received = null;
+                ApiException exception = null;
+
+                bool completed = false;
+                EnqueueCallback(() =>
+                {
+                    Client.GetCommentsByPost(9033, results =>
+                    {
+                        received = results;
+                        completed = true;
+                    },
+                    error =>
+                    {
+                        exception = error;
+                        completed = true;
+                    });
+                });
+                EnqueueConditional(() => completed);
+                EnqueueCallback(() => Assert.IsNull(exception));
+                EnqueueCallback(() => Assert.IsNotNull(received));
+                EnqueueCallback(() => Assert.AreEqual(PostType.Question, received.FirstOrDefault().PostType));
+            }
+        }
+
+        [TestMethod, Asynchronous]
+        public void GetCommentsByPost_ByAnswerId()
+        {
+            using (var context = new AsynchronusTestContext(this))
+            {
+                IPagedList<Comment> received = null;
+                ApiException exception = null;
+
+                bool completed = false;
+                EnqueueCallback(() =>
+                {
+                    Client.GetCommentsByPost(11738, results =>
+                    {
+                        received = results;
+                        completed = true;
+                    },
+                    error =>
+                    {
+                        exception = error;
+                        completed = true;
+                    });
+                });
+                EnqueueConditional(() => completed);
+                EnqueueCallback(() => Assert.IsNull(exception));
+                EnqueueCallback(() => Assert.IsNotNull(received));
+                EnqueueCallback(() => Assert.AreEqual(PostType.Answer, received.FirstOrDefault().PostType));
+            }
+        }
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.