Source

stacky / trunk / source / Stacky.Net35 / StackyClientAsync / AnswerMethods.cs

using System;
using System.Collections.Generic;

namespace Stacky
{
#if SILVERLIGHT
    public partial class StackyClient
#else
    public partial class StackyClientAsync
#endif
    {
        public virtual void GetUsersAnswers(int userId, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError)
        {
            GetUsersAnswers(userId, onSuccess, onError, new AnswerOptions());
        }

        public virtual void GetUsersAnswers(int userId, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError, AnswerOptions options)
        {
            GetUsersAnswers(userId.ToArray(), onSuccess, onError, options);
        }

        public virtual void GetUsersAnswers(IEnumerable<int> userIds, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError)
        {
            GetUsersAnswers(userIds, onSuccess, onError, new AnswerOptions());
        }

        public virtual void GetUsersAnswers(IEnumerable<int> userIds, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError, AnswerOptions options)
        {
            GetAnswers("users", new string[] { userIds.Vectorize(), "answers" }, onSuccess, onError, options);
        }

        public virtual void GetQuestionAnswers(int questionId, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError)
        {
            GetQuestionAnswers(questionId, onSuccess, onError, new AnswerOptions());
        }

        public virtual void GetQuestionAnswers(int questionId, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError, AnswerOptions options)
        {
            GetUsersAnswers(questionId.ToArray(), onSuccess, onError, options);
        }

        public virtual void GetQuestionAnswers(IEnumerable<int> questionIds, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError)
        {
            GetQuestionAnswers(questionIds, onSuccess, onError, new AnswerOptions());
        }

        public virtual void GetQuestionAnswers(IEnumerable<int> questionIds, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError, AnswerOptions options)
        {
            GetAnswers("questions", new string[] { questionIds.Vectorize(), "answers" }, onSuccess, onError, options);
        }

        private void GetAnswers(string method, string[] urlParameters, Action<IPagedList<Answer>> onSuccess, Action<ApiException> onError, AnswerOptions options)
        {
            MakeRequest<Answer>(method, urlParameters, new
            {
                site = this.SiteUrlName,
                page = options.Page ?? null,
                pagesize = options.PageSize ?? null,
                body = options.IncludeBody ? (bool?)true : null,
                comments = options.IncludeComments ? (bool?)true : null,
                sort = options.SortBy.ToString().ToLower(),
                order = GetSortDirection(options.SortDirection),
                min = options.Min ?? null,
                max = options.Max ?? null,
                fromdate = GetDateValue(options.FromDate),
                todate = GetDateValue(options.ToDate),
            }, (items) => onSuccess(new PagedList<Answer>(items)), onError);
        }
    }
}