Beispiel #1
0
        public bool UpdateAnswers(UserMatchPredictionModel model)
        {
            using (var scope = new TransactionScope())
            {
                try
                {
                    foreach (var answer in model.Answers)
                    {
                        _match.AddMatchQuestionAnswer(answer.MatchQuestionId, answer.Answer);
                    }

                    var matchAnswers = _match.GetMatchQuestionAnswers(model.MatchId);
                    var userAnswers  = _matchUserAnswer.GetUserMatchAnswers(model.MatchId);

                    var users        = userAnswers.Select(x => x.UserId).Distinct().ToList();
                    var scoreService = new ScoreCalculationService();
                    foreach (var user in users)
                    {
                        var matchUserAnswers = (from ua in userAnswers
                                                where ua.UserId == user
                                                select new QuestionAnswerModel()
                        {
                            MatchQuestionId = ua.MatchQuestionId,
                            Answer = ua.Answer
                        }).ToList();

                        int userScore = scoreService.GetUserMatchScore(matchAnswers, matchUserAnswers);

                        _matchUserScore.Add(new MatchUserScoreModel()
                        {
                            MatchId = model.MatchId,
                            UserId  = user,
                            Score   = userScore
                        });
                    }

                    _match.ChangeStatus(model.MatchId, (int)Lookups.MatchStatus.Completed);

                    scope.Complete();
                    return(true);
                }
                catch (Exception ex)
                {
                    scope.Dispose();
                    JavaScriptSerializer js = new JavaScriptSerializer();
                    string json             = js.Serialize(model);
                    Log.Error("BL-Match - AddTournamentPrediction" + json, ex);
                    throw new ReturnExceptionModel(new CustomExceptionModel()
                    {
                        StatusCode = HttpStatusCode.BadRequest, Message = ex.Message
                    });
                }
            }
        }