Exemple #1
0
        // update question content... answers can never be updated
        public PostedQuestion EditQuestion(QuestionUpdateRequestDTO q, int accountId)
        {
            //Todo - better way to do this?
            var question = _questionServices.GetPostedQuestion(q.QuestionId);

            // Validations
            if (accountId != question.AccountId)
            {
                throw new InvalidAccountException("User cannot edit another user's question");
            }
            if (question.IsClosed)
            {
                throw new QuestionIsClosedException("Question is closed and can no longer be updated");
            }
            if (question.Answers.Count > 0)
            {
                throw new QuestionUnavailableException("Question cannot be edited after an answer has been posted");
            }
            if (q.Text == null || q.Text.Length < _questionCharMin || q.Text.Length > _questionCharMax)
            {
                throw new InvalidQuestionLengthException("Question must be between " + _questionCharMin + " and " + _questionCharMax + " characters.");
            }

            // Update Question after passed in Question is validated
            question.Text = q.Text;
            question.ExpNeededToAnswer = q.ExpNeededToAnswer;

            // TODO - better way to do this? Does this work? Add method to services that returns PostedQuesiton on update
            return((PostedQuestion)_questionServices.UpdateQuestion(question));
        }
Exemple #2
0
        public IHttpActionResult UpdateQuestion([FromBody] QuestionUpdateRequestDTO questionDTO)
        {
            List <string> requiredClaims = new List <string>()
            {
            };

            authUserId = AuthorizeUser(requiredClaims);
            if (authUserId == 0)
            {
                return(Unauthorized());
            }

            using (var _db = new DatabaseContext())
            {
                Question question;
                try
                {
                    DiscussionForumManager discussionManager = new DiscussionForumManager(_db);
                    question = discussionManager.EditQuestion(questionDTO, questionDTO.AccountId);
                    _db.SaveChanges();
                    return(Content(HttpStatusCode.OK, "Question was updated succesfully."));
                }
                catch (QuestionIsClosedException ex)
                {
                    return(Content(HttpStatusCode.Forbidden, ex.Message));
                }
                catch (InvalidQuestionLengthException ex)
                {
                    return(Content(HttpStatusCode.BadRequest, ex.Message));
                }
                catch (InvalidAccountException ex)
                {
                    return(Content(HttpStatusCode.Unauthorized, ex.Message));
                }
                catch (QuestionUnavailableException ex)
                {
                    return(Content(HttpStatusCode.Forbidden, ex.Message));
                }
            }
        }