public async Task <ObjectControllerResult> EditThird(int id, [FromForm] ThirdRoundEditViewModel model) { return(Result(await gameService.EditThirdRound(id, model))); }
public async Task <ServiceResult> EditThirdRound(int gameId, ThirdRoundEditViewModel model) { var game = await GetGameByPlayerRoles(gameId, PlayerRole.Creator); if (!game.IsSuccess) { return(Error(game.ErrorMessage)); } ThirdRound thirdRound = game.Data.ThirdRound; if (thirdRound == null) { thirdRound = new ThirdRound(); game.Data.ThirdRound = thirdRound; } //if (firstRound.QuestionCosts != null) // context.IntellectualQuestionCosts.RemoveRange(firstRound.QuestionCosts); if (thirdRound.Id != 0) { context.ThirdRounds.Remove(thirdRound); await context.SaveChangesAsync(); } //firstRound.QuestionCosts = model.QuestionCosts.Select(x => //{ // var cost = new IntellectualQuestionCost { Index = index, Value = x }; // index++; // return cost; //}).ToList(); thirdRound.Associations = model.Associations.Select(x => { var index = 0; return(new Association { Word = x.Word, AssociationVariants = x.AssociationVariants.Select(u => { var variant = new AssociationVariant { Index = index, Value = u }; index++; return variant; }).ToList() }); }).ToList(); thirdRound.Chronologies = new List <Chronology>(); foreach (var chronologyModel in model.Chronologies) { var chronology = new Chronology { ChronologyItems = new List <ChronologyItem>() }; var index = 0; foreach (var chronologyItem in chronologyModel.ChronologyItems) { var file = await fileService.SaveFile(chronologyItem.File); if (file.IsSuccess) { chronology.ChronologyItems.Add(new ChronologyItem { FileId = file.Data.Id, Value = chronologyItem.Value, Index = index }); index++; } } thirdRound.Chronologies.Add(chronology); } thirdRound.MelodyGuesses = new List <MelodyGuess>(); foreach (var melodyGuessEdit in model.MelodyGuesses) { var index = 0; var file = await fileService.SaveFile(melodyGuessEdit.File); if (file.IsSuccess) { var guess = new MelodyGuess { MelodyGuessVariants = new List <MelodyGuessVariant>(), FileId = file.Data.Id }; foreach (var variant in melodyGuessEdit.MelodyGuessVariants) { guess.MelodyGuessVariants.Add(new MelodyGuessVariant { Index = index, Value = variant }); index++; } thirdRound.MelodyGuesses.Add(guess); } } await context.SaveChangesAsync(); return(Success()); }