/// <summary>
        /// request all suggestions in a forum
        /// </summary>
        /// <param name="subdomain">the site's subdomain</param>
        /// <param name="forumId">the forumId</param>
        /// <param name="consumerKey">your conusmer key</param>
        /// <param name="consumerSecret">your consumer secret</param>
        /// <param name="page">the result's page</param>
        /// <param name="perPage">number of entries per page</param>
        /// <param name="sort">the status used to order results</param>
        /// <returns>the response for the request of all suggestions in a forum</returns>
        private async Task <IRestResponse> GetAllSuggestionsResponse(string subdomain, string consumerKey, string consumerSecret, int forumId, int page = 1, int perPage = 10, SuggestionSort sort = SuggestionSort.newest, SuggestionFilter filter = SuggestionFilter.all, SuggestionStatusesResult.Status status = null)
        {
            _client = new RestClient(string.Format("https://{0}.uservoice.com/api/v1/forums/{1}/", subdomain, forumId));
            _client.Authenticator = OAuth1Authenticator.ForProtectedResource(consumerKey, consumerSecret, null, null);

            RestRequest request = new RestRequest("suggestions.json", HttpMethod.Get);

            request.AddHeader("If-Modified-Since", DateTime.Now.ToUniversalTime().ToString("R"));


            request.AddParameter("sort", sort);
            request.AddParameter("filter", filter);
            if (status != null && status.id != 0)
            {
                request.AddParameter("status_id", status.id);
            }
            request.AddParameter("page", page);
            request.AddParameter("per_page", perPage);


            return(await _client.Execute(request));
        }
        /// <summary>
        /// requests all suggestions in a forum
        /// </summary>
        /// <param name="subdomain">the site's subdomain</param>
        /// <param name="forumId">the forumId</param>
        /// <param name="consumerKey">your conusmer key</param>
        /// <param name="consumerSecret">your consumer secret</param>
        /// <param name="page">the result's page</param>
        /// <param name="perPage">number of entries per page</param>
        /// <param name="sort">the status used to order results</param>
        /// <returns>all suggestions in a forum</returns>
        public async Task <SuggestionsResult> GetAllSuggesstions(string subdomain, string consumerKey, string consumerSecret, int forumId, int page = 1, int perPage = 10, SuggestionSort sort = SuggestionSort.newest, SuggestionFilter filter = SuggestionFilter.all, SuggestionStatusesResult.Status status = null)
        {
            SuggestionsResult result = new SuggestionsResult();

            IRestResponse response = await GetAllSuggestionsResponse(subdomain, consumerKey, consumerSecret, forumId, page, perPage, sort, filter, status);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                string jsonContent = Encoding.UTF8.GetString(response.RawBytes, 0, response.RawBytes.Length);

                result = JsonConvert.DeserializeObject <SuggestionsResult>(jsonContent);
            }

            return(result);
        }
Beispiel #3
0
        public HttpResponseMessageWrapper <List <Suggestion> > QuerySuggestions(HttpRequestMessage req)
        {
            Operation      operation = null;
            HttpStatusCode code      = AuthenticateUser(req);

            if (code != HttpStatusCode.OK)
            {   // user not authenticated
                return(ReturnResult <List <Suggestion> >(req, operation, code));
            }

            // get the filter from message body
            SuggestionFilter filter = null;

            code = ProcessRequestBody <SuggestionFilter>(req, out filter, out operation, true);
            if (code != HttpStatusCode.OK)  // error encountered processing body
            {
                return(ReturnResult <List <Suggestion> >(req, operation, code));
            }

            if (!ValidateEntityOwnership(filter.EntityID, filter.EntityType))
            {   // entity being queried does not belong to authenticated user, return 403 Forbidden
                TraceLog.TraceError("Queried entity does not belong to current user)");
                return(ReturnResult <List <Suggestion> >(req, operation, HttpStatusCode.Forbidden));
            }

            try
            {
                List <Suggestion> suggestions;
                if (filter.FieldName == null)
                {
                    if (filter.EntityType == EntityTypes.Folder)
                    {   // include suggestions for BOTH Folder and User
                        suggestions = this.SuggestionsStorageContext.Suggestions.
                                      Where(s => (s.EntityID == filter.EntityID || s.EntityID == this.CurrentUser.ID) && (s.ReasonSelected == null || s.ReasonSelected == Reasons.Like)).
                                      OrderBy(s => s.WorkflowInstanceID).OrderBy(s => s.GroupDisplayName).OrderBy(s => s.SortOrder).
                                      ToList <Suggestion>();
                    }
                    else
                    {
                        suggestions = this.SuggestionsStorageContext.Suggestions.
                                      Where(s => s.EntityID == filter.EntityID && (s.ReasonSelected == null || s.ReasonSelected == Reasons.Like)).
                                      OrderBy(s => s.WorkflowInstanceID).OrderBy(s => s.GroupDisplayName).OrderBy(s => s.SortOrder).
                                      ToList <Suggestion>();
                    }
                }
                else
                {
                    suggestions = this.SuggestionsStorageContext.Suggestions.
                                  Where(s => s.EntityID == filter.EntityID && s.SuggestionType == filter.FieldName && (s.ReasonSelected == null || s.ReasonSelected == Reasons.Like)).
                                  OrderBy(s => s.WorkflowInstanceID).OrderBy(s => s.GroupDisplayName).OrderBy(s => s.SortOrder).
                                  ToList <Suggestion>();
                }
                var response = ReturnResult <List <Suggestion> >(req, operation, suggestions, HttpStatusCode.OK);
                response.Headers.CacheControl = new CacheControlHeaderValue()
                {
                    NoCache = true
                };
                return(response);
            }
            catch (Exception ex)
            {   // suggestions not found - return 404 Not Found
                TraceLog.TraceException("Internal Server Error", ex);
                return(ReturnResult <List <Suggestion> >(req, operation, HttpStatusCode.InternalServerError));
            }
        }