public async Task InsertNewPlayerQuestion(PlayerQuestionBE playerQuestionBE) { PlayerQuestionEntity questionEntity = base.Mapper.Map <PlayerQuestionEntity>(playerQuestionBE); questionEntity.CreatedBy = questionEntity.LastModifiedBy = SystemConstants.DefaultUser; questionEntity.CreatedDateTime = questionEntity.LastModifiedDateTime = DateTime.UtcNow; await base.DataSvc.PlayerQuestionRepo.InsertAsync(questionEntity); }
public async Task <PlayerRoundBE> GetTriviaRound(int?playerID) { bool isGuestRound = playerID == null; //Get all questions List <QuestionBE> questions = await base.Facade.GetAllQuestions(); //Pick 10 random to keep Random rng = new Random(); HashSet <int> questionsToUse = new HashSet <int>(); while (questionsToUse.Count < 10) { questionsToUse.Add(rng.Next(1, questions.Count + 1)); } questions.RemoveAll(q => !questionsToUse.Contains(q.QuestionID)); questions = questions.OrderBy(q => rng.Next()).ToList(); //Shuffle rng = null; //For each question get their answers /* ANSWER RETRIEVAL NOTES * * This isn't ideal, but given the nature of the data source * The way in which we retrieve a single data obj by a identifier * * i.e. GetAnswersByQuestionID * * Is to pull the whole Answer data source, parse it to a List<Answer> * Then filter the list by the ID * * Rather than pull the entire source 10 times, once per question * I'm just gonna grab it now and parse it here */ List <AnswerBE> answers = await base.Facade.GetAllAnswers(); answers.RemoveAll(a => !questionsToUse.Contains(a.QuestionID)); //Construct the initial response obj PlayerRoundBE roundBE = new PlayerRoundBE() { StartedDateTime = DateTime.UtcNow, QuestionDetails = questions.Select((q, i) => new QuestionDetailBE() { QuestionID = q.QuestionID, QuestionSequence = i + 1, Text = q.Text, Answers = answers.Where(a => a.QuestionID == q.QuestionID).ToList() }).ToList() }; int roundNumber = 1; if (!isGuestRound) { roundBE.PlayerID = playerID.Value; roundNumber = await _playerFacade.GetRoundNumberByPlayerID(playerID.Value); roundBE.RoundNumber = roundNumber; //Insert history PlayerHistoryBE newPlayerHistory = _mapper.Map <PlayerHistoryBE>(roundBE); await _playerFacade.InsertNewHistory(newPlayerHistory); roundBE.PlayerHistoryID = newPlayerHistory.PlayerHistoryID; //Insert Questions foreach (QuestionDetailBE question in roundBE.QuestionDetails) { PlayerQuestionBE playerQuestionBE = new PlayerQuestionBE() { PlayerHistoryID = roundBE.PlayerHistoryID, QuestionID = question.QuestionID, QuestionSequence = question.QuestionSequence }; await _playerFacade.InsertNewPlayerQuestion(playerQuestionBE); } } else { roundBE.RoundNumber = roundNumber; } return(roundBE); }