public async Task <IActionResult> Add(ContestQuestionsListInputModel model) { var creatorId = this.contestsService.GetCreatorId(model.Id); var user = await this.userManager.GetUserAsync(this.User); var isAdmin = await this.userManager.IsInRoleAsync(user, GlobalConstants.AdministratorRoleName); if (!isAdmin && creatorId != user.Id) { return(this.RedirectToAction("Forbid", "Errors")); } foreach (var question in model.Questions) { var questionId = await this.questionsService.CreateAsync(model.Id, question.Content); await this.answersService.CreateAsync(questionId, question.One.Content, question.One.IsRight); await this.answersService.CreateAsync(questionId, question.Two.Content, question.Two.IsRight); await this.answersService.CreateAsync(questionId, question.Three.Content, question.Three.IsRight); await this.answersService.CreateAsync(questionId, question.Four.Content, question.Four.IsRight); } return(this.RedirectToAction(nameof(ContestsController.Details), "Contests", new { id = model.Id })); }
public async Task <IActionResult> Add(QuestionCountInputModel inputModel) { if (this.roomsService.IsExistRoomWithThisContest(inputModel.Id)) { this.TempData["Notification"] = "You can’t add questions while contest is active."; return(this.RedirectToAction(nameof(ContestsController.MyContests), "Contests")); } var creatorId = this.contestsService.GetCreatorId(inputModel.Id); var user = await this.userManager.GetUserAsync(this.User); var isAdmin = await this.userManager.IsInRoleAsync(user, GlobalConstants.AdministratorRoleName); if (!isAdmin && creatorId != user.Id) { return(this.RedirectToAction("Forbid", "Errors")); } var currentQuestionsCount = this.contestsService.GetQuestionsCount(inputModel.Id); if (currentQuestionsCount == 10) { this.TempData["Notification"] = "You have reached the questions limit!"; return(this.RedirectToAction(nameof(ContestsController.Details), "Contests", new { inputModel.Id })); } var questionsCountForAdd = inputModel.QuestionCount; var remainingQuestionsCount = MaxCount - (currentQuestionsCount + questionsCountForAdd); if (remainingQuestionsCount < 0) { var allowedQuestionsCount = MaxCount - currentQuestionsCount; this.TempData["Notification"] = $"You have exceeded the questions limit. You can only add {allowedQuestionsCount} questions."; questionsCountForAdd = allowedQuestionsCount; } var model = new ContestQuestionsListInputModel { Id = inputModel.Id, Questions = new QuestionInputModel[questionsCountForAdd], }; return(this.View("Add", model)); }