public override void Execute()
        {
            int answerId = int.Parse(this.Data[1]);

            if (!this.Forum.IsLogged)
            {
                throw new CommandException(Messages.NotLogged);
            }
            else if (this.Forum.CurrentQuestion == null)
            {
                throw new CommandException(Messages.NoQuestionOpened);
            }
            else if (!this.Forum.Answers.Any(a => a.Id == answerId))
            {
                throw new CommandException(Messages.NoAnswer);
            }
            else if (this.Forum.CurrentUser != this.Forum.CurrentQuestion.Author && this.Forum.CurrentUser != this.Forum.Users[0])
            {
                throw new CommandException(Messages.NoPermission);
            }

            IAnswer oldAnswer     = this.Forum.Answers.First(a => a.Id == answerId);
            IAnswer newBestAnswer = new BestAnswer(oldAnswer.Id, oldAnswer.Body, oldAnswer.Author);

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

            this.Forum.Answers.Remove(oldAnswer);
            this.Forum.CurrentQuestion.Answers.Remove(oldAnswer);

            StringBuilder bestAnswerResult = new StringBuilder();

            bestAnswerResult.AppendFormat(Messages.BestAnswerSuccess, answerId);
            this.Forum.Output.AppendLine(bestAnswerResult.ToString());
        }
Example #2
0
        public override void Execute()
        {
            int     answerID = int.Parse(this.Data[1]);
            IAnswer answer;
            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;
            }
            answer = question.Answers.Where(a => a.Id == answerID).FirstOrDefault();
            BestAnswer bestAnswer = new BestAnswer(answer.Id, answer.Body, answer.Author);

            question.Answers[answerID - 1] = bestAnswer;
            this.Forum.Output.AppendLine(string.Format(Messages.BestAnswerSuccess, answerID));
        }
        private void MakeAnswerBestAnswer(IQuestion question, IAnswer answer)
        {
            this.RemoveCurrentAnswerFromDatabase(question, answer);

            var bestAnswer = new BestAnswer(answer.Id, answer.Body, answer.Author);

            this.AddBestAnswerToDatabase(question, bestAnswer);
        }
Example #4
0
        private void MakeAnswerBestAnswer(IQuestion question, IAnswer answer)
        {
            this.RemoveCurrentAnswerFromDatabase(question, answer);

            var bestAnswer = new BestAnswer(answer.Id, answer.Body, answer.Author);

            this.AddBestAnswerToDatabase(question, bestAnswer);
        }
Example #5
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(a => a.Id == id))
            {
                throw new CommandException(Messages.NoAnswer);
            }

            var answer = this.Forum.Answers.First(a => a.Id == id);

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

            if (this.Forum.CurrentQuestion.Answers.Any(a => a is BestAnswer))
            {
                var bestAnswer = this.Forum.CurrentQuestion.Answers.First(a => a is BestAnswer) as BestAnswer;

                if (bestAnswer.Id != id)
                {
                    var normalAnswer = new Answer(bestAnswer.Id, bestAnswer.Body, bestAnswer.Author);

                    this.Forum.Answers.Remove(bestAnswer);
                    this.Forum.CurrentQuestion.Answers.Remove(bestAnswer);

                    this.Forum.Answers.Add(normalAnswer);
                    this.Forum.CurrentQuestion.Answers.Add(normalAnswer);
                }
            }

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

            var newAnswer = new BestAnswer(answer.Id, ((Answer)answer).Body, answer.Author);

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

            this.Forum.Output.AppendLine(
                string.Format(Messages.BestAnswerSuccess, newAnswer.Id)
                );
        }
Example #6
0
        public void Solve()
        {
            int p = FindP(); //индекс текущего блока

            if (p > -1 && !CoveredRows[p] && Table.Blocks[p] != null && Table.Blocks.Count() != 0)
            {
                FindAnswer(p, 0);
            }
            if (BestAnswer != null)
            {
                BestAnswer.AddRange(MustBeInEveryAnswer);
            }
            else
            {
                throw new NoAnswerException("Не канает");
            }
        }
Example #7
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);
            }
        }
Example #8
0
        public override void Execute()
        {
            this.IsLoggedValidation();

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

            var currentQuestion = this.Forum.CurrentQuestion;
            int id = int.Parse(this.Data[1]);
            var answer = currentQuestion.Answers.FirstOrDefault(a => a.Id == id);

            if (answer == null)
            {
                throw new CommandException(Messages.NoAnswer);
            }

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

            var bestAnswer = new BestAnswer(answer.Id, answer.Body, answer.Author);

            var oldBestAnswer = this.Forum.Questions
                .First(q => q.Id == this.Forum.CurrentQuestion.Id)
                .Answers.FirstOrDefault(a => a is BestAnswer);

            if (oldBestAnswer != null)
            {
                var newAnswer = new Answer(oldBestAnswer.Id, oldBestAnswer.Body, oldBestAnswer.Author);
                this.Forum.Questions.First(q => q.Id == currentQuestion.Id).Answers.Remove(oldBestAnswer);
                this.Forum.Questions.First(q => q.Id == currentQuestion.Id).Answers.Add(newAnswer);
            }

            this.Forum.Questions.First(q => q.Id == currentQuestion.Id).Answers.Remove(answer);
            this.Forum.Questions.First(q => q.Id == currentQuestion.Id).Answers.Add(bestAnswer);
            this.Forum.Output.AppendLine(string.Format(Messages.BestAnswerSuccess, bestAnswer.Id));
        }
Example #9
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));
                return;
            }

            var     id          = int.Parse(this.Data[1]);
            var     answers     = this.Forum.CurrentQuestion.Answers;
            var     found       = false;
            IAnswer foundAnswer = null;

            foreach (var answer in answers)
            {
                if (answer.Id == id)
                {
                    foundAnswer = answer;
                    found       = true;
                }
            }

            if (!found)
            {
                this.Forum.Output.AppendLine(string.Format(Messages.NoAnswer));
                return;
            }

            var   questions      = this.Forum.Questions;
            IUser questionAuthor = null;

            foreach (var question in questions)
            {
                foreach (var answer in question.Answers)
                {
                    if (answer.Id == id)
                    {
                        questionAuthor = question.Author;
                    }
                }
            }

            if (questionAuthor == this.Forum.CurrentUser ||
                this.Forum.CurrentUser is Administrator)
            {
                answers.Remove(foundAnswer);
                var bestAnswer = new BestAnswer(id, foundAnswer.Body, foundAnswer.Author);
                answers.Add(bestAnswer);

                this.Forum.Output.AppendLine(string.Format(Messages.BestAnswerSuccess, id));
            }
            else
            {
                this.Forum.Output.AppendLine(string.Format(Messages.NoPermission));
            }
        }