コード例 #1
0
        public override void Execute()
        {
            int answerId = int.Parse(this.Data[1]);
            var question = this.Forum.CurrentQuestion;

            if (!this.Forum.IsLogged)
            {
                throw new CommandException(Messages.NotLogged);
            }
            if (this.Forum.CurrentQuestion == null)
            {
                throw new CommandException(Messages.NoQuestionOpened);
            }
            if (!question.Answers.Any(a => a.Id == answerId))
            {
                throw new CommandException(Messages.NoAnswer);
            }
            if (question.Author != this.Forum.CurrentUser && !(this.Forum.CurrentUser is IAdministrator))
            {
                throw new CommandException(Messages.NoPermission);
            }
            if (question.Answers.Any(a => a is BestAnswer))
            {
                int currentBestID = question.Answers.Where(a => a is BestAnswer).FirstOrDefault().Id;
                Answer oldBest = new Answer(currentBestID, question.Answers[currentBestID - 1].Body, question.Answers[currentBestID - 1].Author);
                question.Answers[currentBestID - 1] = oldBest;
            }
            IAnswer answer = question.Answers.FirstOrDefault(a => a.Id == answerId);
            BestAnswer bestAnswer = new BestAnswer(answer.Id, answer.Body, answer.Author);
            question.Answers[answerId - 1] = bestAnswer;
            this.Forum.Output.AppendLine(string.Format(Messages.BestAnswerSuccess, answerId));
        }
コード例 #2
0
 public override void Execute()
 {
     ValidateLoginUser();
     ValidateQuestion();
     string answerBody = this.Data[1];
     int id = this.Forum.Answers.Count + 1;
     IAnswer anwser = new Answer(id, answerBody, this.Forum.CurrentUser);
     this.Forum.CurrentQuestion.Answers.Add(anwser);
     this.Forum.Answers.Add(anwser);
     this.Forum.Output.AppendLine(String.Format(Messages.PostAnswerSuccess, anwser.Id));
 }
コード例 #3
0
ファイル: PostAnswerCommand.cs プロジェクト: simooo93/Exams
 public override void Execute()
 {
     if (!this.Forum.IsLogged)
     {
         throw new CommandException(Messages.NotLogged);
     }
     if (this.Forum.CurrentQuestion == null)
     {
         throw new CommandException(Messages.NoQuestionOpened);
     }
     IAnswer answer = new Answer(this.Forum.Answers.Count + 1, this.Forum.CurrentUser, this.Data[1]);
     this.Forum.CurrentQuestion.Answers.Add(answer);
     this.Forum.Answers.Add(answer);
     this.Forum.Output.AppendLine(string.Format(Messages.PostAnswerSuccess, answer.Id));
 }
コード例 #4
0
        public override void Execute()
        {
            if (!this.Forum.IsLogged)
            {
                throw new CommandException(Messages.NotLogged);
            }
            if (this.Forum.CurrentQuestion == null)
            {
                throw new CommandException(Messages.NoQuestionOpened);
            }
            int id = ++Answer.AnswerCount;
            string body = this.Data[1];

            Answer answer = new Answer(id, body,this.Forum.CurrentUser);
            this.Forum.CurrentQuestion.Answers.Add(answer);
            this.Forum.Output.AppendFormat(Messages.PostAnswerSuccess, id).AppendLine();
        }
コード例 #5
0
        public override void Execute()
        {
            var id = int.Parse(this.Data[1]);

            if (!this.Forum.IsLogged)
            {
                throw new CommandException(Messages.NotLogged);
            }

            if (this.Forum.CurrentQuestion == null)
            {
                throw new CommandException(Messages.NoQuestionOpened);
            }

            if (this.Forum.CurrentUser != this.Forum.CurrentQuestion.Author)
            {
                if (!(this.Forum.CurrentUser is IAdministrator))
                {
                    throw new CommandException(Messages.NoPermission);
                }
            }

            var availBestAnswer = this.Forum.CurrentQuestion.Answers.FirstOrDefault(a => a.Id == id);
            if (availBestAnswer == null)
            {
                throw new CommandException(Messages.NoAnswer);
            }

            var curentBestAnswer = this.Forum.CurrentQuestion.Answers.FirstOrDefault(a => a is BestAnswer) as BestAnswer;
            if (curentBestAnswer != null)
            {
                if (curentBestAnswer.Id != id)
                {
                    var answer = new Answer(curentBestAnswer.Id, curentBestAnswer.Author, curentBestAnswer.Body);
                    this.ExchangeAnswers(curentBestAnswer, answer);
                }
            }
            else
            {
                this.Forum.Output.AppendLine(string.Format(Messages.BestAnswerSuccess, id));
                var bestAnswer = new BestAnswer(availBestAnswer.Id, availBestAnswer.Author, availBestAnswer.Body);
                this.ExchangeAnswers(availBestAnswer, bestAnswer);
            }
        }
コード例 #6
0
ファイル: PostAnswerCommand.cs プロジェクト: didimitrov/Algo
        public override void Execute()
        {
            var body = Data[1];
            var question = this.Forum.CurrentQuestion;

            if (!Forum.IsLogged)
            {
                throw new CommandException(Messages.NotLogged);
            }
            if (question==null)
            {
                throw new CommandException(Messages.NoQuestion);
            }

            var answer = new Answer((Forum.Answers.Count + 1), body, Forum.CurrentUser);
            Forum.Answers.Add(answer);
            question.Answers.Add(answer);
            this.Forum.Output.AppendLine(string.Format(Messages.PostAnswerSuccess, answer.Id));
        }
コード例 #7
0
        public override void Execute()
        {
            if (!this.Forum.IsLogged)
            {
                throw new CommandException(Messages.NotLogged);
            }

            if (this.Forum.CurrentQuestion == null)
            {
                throw new CommandException(Messages.NoQuestionOpened);
            }

            var author = this.Forum.CurrentUser;
            var question = this.Forum.CurrentQuestion;
            string answerBody = this.Data[1];
            var answer = new Answer(this.Forum.Answers.Count + 1, answerBody, author);
            this.Forum.Answers.Add(answer);
            question.Answers.Add(answer);
            this.Forum.Output.AppendFormat(Messages.PostAnswerSuccess, answer.Id).AppendLine();
        }
コード例 #8
0
        public override void Execute()
        {
            if (!this.Forum.IsLogged)
            {
                this.Forum.Output.AppendLine(string.Format(Messages.NotLogged));
                return;
            }

            if (this.Forum.CurrentQuestion == null)
            {
                this.Forum.Output.AppendLine(string.Format(Messages.NoQuestionOpened));
            }

            var body = this.Data[1];
            var answers = this.Forum.Answers;

            Answer answer = new Answer(answers.Count + 1, body, this.Forum.CurrentUser);
            answers.Add(answer);
            this.Forum.CurrentQuestion.Answers.Add(answer);

            this.Forum.Output.AppendLine(string.Format(Messages.PostAnswerSuccess, answer.Id));
        }
コード例 #9
0
        public override void Execute()
        {
            string answerBody = this.Data[1];
            var user = this.Forum.CurrentUser;
            var answerId = this.Forum.Answers.Count + 1;

            if (!this.Forum.IsLogged)
            {
                this.Forum.Output.AppendLine(Messages.NotLogged);
                return;
            }

            if (this.Forum.CurrentQuestion == null)
            {
                this.Forum.Output.AppendLine(Messages.NoQuestionOpened);
                return;
            }
            var answerForAdding = new Answer(answerId, answerBody, user);
            this.Forum.CurrentQuestion.Answers.Add(answerForAdding);
            this.Forum.Answers.Add(answerForAdding);
            this.Forum.Output.AppendFormat(Messages.PostAnswerSuccess, answerId).AppendLine();
        }
コード例 #10
0
        public override void Execute()
        {
            IUser author = this.Forum.CurrentUser;
            var question = this.Forum.CurrentQuestion as Question;
            if (author == null)
            {
                throw new CommandException(Messages.NotLogged);
            }

            if (question == null)
            {
                throw new CommandException(Messages.NoQuestionOpened);
            }

            int anwerId = this.Forum.Answers.Count + 1;
            string body = this.Data[1];

            var answer = new Answer(anwerId, body, author);

            question.AddAnswer(answer);
            this.Forum.Answers.Add(answer);
            this.Forum.Output.AppendLine(string.Format(Messages.PostAnswerSuccess, anwerId));
        }
コード例 #11
0
        public override void Execute()
        {
            if (!this.Forum.IsLogged)
            {
                throw new CommandException(Messages.NotLogged);
            }
            if (this.Forum.CurrentQuestion == null)
            {
                throw new CommandException(Messages.NoQuestionOpened);
            }
            int id = int.Parse(this.Data[1]);
            if (!this.Forum.CurrentQuestion.Answers.Any(question => question.Id == id))
            {
                throw new CommandException(Messages.NoAnswer);
            }
            if (this.Forum.CurrentQuestion.Author != this.Forum.CurrentUser)
            {
                throw new CommandException(Messages.NoPermission);
            }

            // taking the regular answer and make it best answer.
            IAnswer answerToBecomeBestAnswer = this.Forum.CurrentQuestion.Answers.First(answer => answer.Id == id);
            BestAnswer bestAnswer = new BestAnswer(answerToBecomeBestAnswer.Id, answerToBecomeBestAnswer.Author, answerToBecomeBestAnswer.Body);
            int indexOfAnswer = this.Forum.CurrentQuestion.Answers.IndexOf(answerToBecomeBestAnswer);
            if (this.Forum.CurrentQuestion.Answers.Any(answer => answer is BestAnswer))
            {
                // taking the answer which is currently the best answer and then making it regular answer.
                IAnswer bestAnswerToBecomeReguarAnswer =
                    this.Forum.CurrentQuestion.Answers.First(answer => answer is BestAnswer);
                IAnswer reguarAnswer = new Answer(bestAnswerToBecomeReguarAnswer.Id, bestAnswerToBecomeReguarAnswer.Author, bestAnswerToBecomeReguarAnswer.Body);
                int indexOfBestAnswerToBecomeRegularAnswer = this.Forum.CurrentQuestion.Answers.IndexOf(answerToBecomeBestAnswer);
                this.Forum.CurrentQuestion.Answers[indexOfBestAnswerToBecomeRegularAnswer] = reguarAnswer;

            }
            this.Forum.CurrentQuestion.Answers[indexOfAnswer] = bestAnswer;
            this.Forum.Output.AppendLine(string.Format(Messages.BestAnswerSuccess, bestAnswer.Id));
        }
コード例 #12
0
        public override void Execute()
        {
            var answerBody = this.Data[1];

            if (!this.Forum.IsLogged)
            {
                throw new CommandException(Messages.NotLogged);
            }

            if (this.Forum.CurrentQuestion == null)
            {
                throw new CommandException(Messages.NoQuestionOpened);
            }

            var answers = this.Forum.Answers;
            var newAnswerId = answers.Count + 1;
            var currentUser = this.Forum.CurrentUser;
            var answer = new Answer(newAnswerId, answerBody, currentUser);

            this.Forum.CurrentQuestion.Answers.Add(answer);

            this.Forum.Output.AppendLine(
                string.Format(Messages.PostAnswerSuccess, newAnswerId));
        }