public HttpResponseMessage Delete(int id)
        {
            using (var transaction = this.ApplicationContext.DatabaseContext.Database.GetTransaction())
            {
                var responses = QuestionRepository.Current.GetResponses(id);
                var answers   = QuestionRepository.Current.GetAnswers(id);

                foreach (var response in responses)
                {
                    if (!ResponseRepository.Current.Delete(response.Id))
                    {
                        return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Can't delete question, Error removing the responses"));
                    }
                }

                foreach (var answer in answers)
                {
                    if (!AnswerRepository.Current.Delete(answer.Id))
                    {
                        return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Can't delete question, Error removing the answers"));
                    }
                }

                if (QuestionRepository.Current.Delete(id))
                {
                    transaction.Complete();

                    PollItCacheRefresher.ClearCache(id);

                    return(this.Request.CreateResponse(HttpStatusCode.OK));
                }
            }

            return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Can't delete question"));
        }
示例#2
0
        public Question Vote(int questionId, int answerId)
        {
            var question = QuestionRepository.Current.GetById(questionId);

            var canVote = true;

            if (question.StartDate != null)
            {
                canVote = DateTime.Now > question.StartDate;
            }

            if (canVote && question.EndDate != null)
            {
                canVote = DateTime.Now < question.EndDate;
            }

            if (canVote)
            {
                var result = QuestionRepository.Current.PostResponse(questionId, answerId);

                if (result != null)
                {
                    PollItCacheRefresher.ClearCache(questionId);
                }
            }

            return(this.GetQuestion(questionId));
        }
        public HttpResponseMessage Post(Answer answer)
        {
            var result = AnswerRepository.Current.Save(answer);

            if (result != null)
            {
                PollItCacheRefresher.ClearCache(answer.QuestionId);
                return(this.Request.CreateResponse(HttpStatusCode.OK, answer));
            }

            return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Can't save answer"));
        }
        public HttpResponseMessage PostAnswer(int id, Answer answer)
        {
            var result = QuestionRepository.Current.PostAnswer(id, answer);

            if (result != null)
            {
                PollItCacheRefresher.ClearCache(id);

                return(this.Request.CreateResponse(HttpStatusCode.OK, result));
            }

            return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Can't add answer to question"));
        }
        public HttpResponseMessage Delete(int id, int questionId)
        {
            using (var transaction = this.ApplicationContext.DatabaseContext.Database.GetTransaction())
            {
                if (ResponseRepository.Current.DeleteByAnswerId(id) && AnswerRepository.Current.Delete(id))
                {
                    transaction.Complete();
                    PollItCacheRefresher.ClearCache(questionId);

                    return(this.Request.CreateResponse(HttpStatusCode.OK));
                }
            }

            return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Can't delete answer"));
        }
        public HttpResponseMessage Post(Question question)
        {
            using (var transaction = this.ApplicationContext.DatabaseContext.Database.GetTransaction())
            {
                // add or update question
                question = QuestionRepository.Current.Save(question);

                if (question != null)
                {
                    // remove old answers, they don't appear in the result.answers array
                    var oldAnswers = QuestionRepository.Current.GetAnswers(question.Id).Where(a => !question.Answers.Any(r => r.Id.Equals(a.Id)));
                    foreach (var deletedAnswer in oldAnswers)
                    {
                        if (!ResponseRepository.Current.DeleteByAnswerId(deletedAnswer.Id) || !AnswerRepository.Current.Delete(deletedAnswer.Id))
                        {
                            return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Can't delete old answers, Error add of update of the quesion"));
                        }
                    }

                    // add or update answers
                    foreach (var answer in question.Answers)
                    {
                        var result = answer.Id != 0 ? AnswerRepository.Current.Save(answer) : QuestionRepository.Current.PostAnswer(question.Id, answer);

                        if (result == null)
                        {
                            return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Can't add answer to question"));
                        }
                    }

                    transaction.Complete();
                    PollItCacheRefresher.ClearCache(question.Id);

                    return(this.Request.CreateResponse(HttpStatusCode.OK, question));
                }

                return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Can't save question"));
            }
        }
示例#7
0
 private void CacheUpdated(PollItCacheRefresher sender, CacheRefresherEventArgs e)
 {
     // clear Poll-it cache, this will executed on all servers
     ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheByKeySearch($"{RuntimeCacheConstants.RuntimeCacheKeyPrefix}{e.MessageObject}");
 }