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 = Int32.Parse(base.Data[1]); var answer = this.Forum.CurrentQuestion.Answers.FirstOrDefault(a => a.Id == id); if (answer == null) throw new CommandException(Messages.NoAnswer); var currentUser = this.Forum.CurrentUser; bool isAdmin = currentUser.GetType() == typeof (Admin); bool isUserAuthor = currentUser.Id == this.Forum.CurrentQuestion.Author.Id; if (!isUserAuthor && !isAdmin) throw new CommandException(Messages.NoPermission); var bestAnswer = new BestAnswer(id, answer.Body, answer.Author); this.Forum.CurrentQuestion.Answers.Remove(answer); this.Forum.CurrentQuestion.Answers.Add(bestAnswer); this.Forum.Output.AppendLine(string.Format(Messages.BestAnswerSuccess, id)); }
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)); }
public override void Execute() { int answerId = int.Parse(this.Data[1]); var question = this.Forum.CurrentQuestion as Question; var currentUser = this.Forum.CurrentUser; if (currentUser == null) { throw new CommandException(Messages.NotLogged); } if (question == null) { throw new CommandException(Messages.NoQuestionOpened); } var answer = question.Answers.FirstOrDefault(a => a.Id == answerId); if (answer == null) { throw new CommandException(Messages.NoAnswer); } if (currentUser.Username == question.Author.Username || currentUser.Id == 1) { if (question.HasBestAnswer) { throw new CommandException(string.Format(Messages.BestAnswerDenied, question.Id)); } var bestAnswer = new BestAnswer(answerId, answer.Body, answer.Author); this.Forum.Answers.Remove(answer); this.Forum.Answers.Add(bestAnswer); question.Answers.Remove(answer); question.Answers.Add(bestAnswer); question.HasBestAnswer = true; this.Forum.Output.AppendLine(string.Format(Messages.BestAnswerSuccess, bestAnswer.Id)); } else { throw new CommandException(Messages.NoPermission); } }
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); } }
public override void Execute() { int answerId = int.Parse(this.Data[1]); var currentQuestion = this.Forum.CurrentQuestion; if (!this.Forum.IsLogged) { this.Forum.Output.AppendLine(Messages.NotLogged); return; } if (currentQuestion == null) { this.Forum.Output.AppendLine(Messages.NoQuestionOpened); return; } var answer = this.Forum.CurrentQuestion.Answers.FirstOrDefault(q => q.Id == answerId); if (answer == null) { this.Forum.Output.AppendLine(Messages.NoAnswer); return; } if (this.Forum.CurrentUser != this.Forum.CurrentQuestion.Author && !(this.Forum.CurrentUser is IAdministrator)) { this.Forum.Output.AppendLine(Messages.NoPermission); return; } var bestAnswer = new BestAnswer(answerId, answer.Body, answer.Author); currentQuestion.Answers.Add(bestAnswer); this.Forum.Answers.Add(bestAnswer); currentQuestion.Answers.Remove(answer); this.Forum.Answers.Remove(answer); this.Forum.Output.AppendFormat(Messages.BestAnswerSuccess, answerId).AppendLine(); }
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)); }
public override void Execute() { int inputId = int.Parse(this.Data[1]); var question = this.Forum.CurrentQuestion; var answer = question.Answers.FirstOrDefault(a => a.Id == inputId); var user = this.Forum.CurrentUser; if (!this.Forum.IsLogged) { throw new CommandException(Messages.NotLogged); } if (question == null) { throw new CommandException(Messages.NoQuestionOpened); } if (answer == null) { throw new CommandException(Messages.NoAnswer); } if (!(user is IAdministrator || question.Author == user)) { throw new CommandException(Messages.NoPermission); } var bestAnswer = new BestAnswer( inputId, answer.Body, answer.Author); this.Forum.Answers.Add(bestAnswer); this.Forum.Answers.Remove(answer); question.Answers.Remove(answer); question.Answers.Add(bestAnswer); this.Forum.Output.AppendFormat(Messages.BestAnswerSuccess, inputId).AppendLine(); }