public async Task <ObjectControllerResult> EditFirstRound(int id, FirstRoundEditViewModel model) { return(Result(await gameService.EditFirstRound(id, model))); }
public async Task <ServiceResult> EditFirstRound(int gameId, FirstRoundEditViewModel model) { var game = await GetGameByPlayerRoles(gameId, PlayerRole.Creator); if (!game.IsSuccess) { return(Error(game.ErrorMessage)); } FirstRound firstRound = game.Data.FirstRound; if (firstRound == null) { firstRound = new FirstRound(); game.Data.FirstRound = firstRound; } //if (firstRound.QuestionCosts != null) // context.IntellectualQuestionCosts.RemoveRange(firstRound.QuestionCosts); if (firstRound.QuestionsCategories != null) { context.QuestionsCategorys.RemoveRange(firstRound.QuestionsCategories); } await context.SaveChangesAsync(); //firstRound.QuestionCosts = model.QuestionCosts.Select(x => //{ // var cost = new IntellectualQuestionCost { Index = index, Value = x }; // index++; // return cost; //}).ToList(); firstRound.QuestionsCategories = model.QuestionsCategories.Select(x => { var questionIndex = 0; var category = new QuestionsCategory { Name = x.Name, Questions = x.Questions.Select(u => { var answerIndex = 0; var question = new IntellectualQuestion { Index = questionIndex, Value = u.Value, Answers = u.Answers.Select(t => { var answer = new IntellectualAnswer { Value = t, Index = answerIndex }; answerIndex++; return(answer); }).ToList() }; questionIndex++; return(question); }).ToList() }; return(category); }).ToList(); await context.SaveChangesAsync(); return(Success()); }