예제 #1
0
 public async Task<IHttpActionResult> Post(AddNewPlayerMatchupViewModel vm)
 {
     if (ModelState.IsValid && vm.NewPlayerMatchups.Count > 0)
     {
         //Todo:Validate that each player is selected only once.
         using (var repoProvider = new RepoProvider())
         {
             WeekModel weekObj = new WeekModel() { WeekId = Guid.NewGuid(), BeenPlayed = false, WeekNbr = vm.WeekNbr, ScoreCreateDate = DateTime.Now };
             repoProvider.WeekRepo.Add(weekObj);
             for (int i = 0; i < vm.NewPlayerMatchups.Count; i++)
             {
                 MatchupModel matchupObj = new MatchupModel() { MatchupId = Guid.NewGuid(), WeekId = weekObj.WeekId, Player1 = vm.NewPlayerMatchups[i].Player1Id, Player2 = vm.NewPlayerMatchups[i].Player2Id };
                 repoProvider.MatchupRepo.Add(matchupObj);
             }
             await repoProvider.SaveAllRepoChangesAsync();
         }
         return Ok();
     }
     return BadRequest();
 }
예제 #2
0
        public async Task<IHttpActionResult> Post(AddRoundWeekViewModel vm)
        {
            if (ModelState.IsValid)
            {
                using (var repoProvider = new RepoProvider())
                {
                    var week = await repoProvider.WeekRepo.Get(vm.WeekId);
                    week.BeenPlayed = true;
                    week.ScoreCreateDate = DateTime.Now;

                    bool front = vm.FrontBack.Equals("Front");
                    var course = repoProvider.CourseRepo.Get();
                    var pars = front ? HttpContext.Current.Application["FrontPars"] as List<Par> :
                        HttpContext.Current.Application["BackPars"] as List<Par>;
                    var parList = pars.Select(p => p.ParValue);

                    bool half = vm.FirstHalf.Equals("First");

                    var matchups = await repoProvider.MatchupRepo.GetAllWeeklyMatchups(vm.WeekId);
                    foreach (var matchup in matchups)
                    {
                        var postedRoundPlayer1 = vm.PlayerRounds.First(r => r.PlayerId.CompareTo(matchup.Player1) == 0);
                        var postedRoundPlayer2 = vm.PlayerRounds.First(r => r.PlayerId.CompareTo(matchup.Player2) == 0);
                        var p1Handicap = await ServiceProvider.HandicapInstance.CalculateHandicap(matchup.Player1);
                        var p2Handicap = await ServiceProvider.HandicapInstance.CalculateHandicap(matchup.Player2);
                        List<ScoringResultModel> scoringResults = ServiceProvider.ScoringInstance.ScoreMatchup(parList, postedRoundPlayer1.Scores, postedRoundPlayer2.Scores, p1Handicap, p2Handicap);

                        var p1NewRound = new RoundModel()
                        {
                            PlayerRefId = postedRoundPlayer1.PlayerId,
                            RoundId = Guid.NewGuid(),
                            WeekId = vm.WeekId,
                            Scores = ServiceProvider.ScoringInstance.ExtractScores(postedRoundPlayer1.Scores),
                            Front = front,
                            SeasonFirstHalf = half,
                            Handicap = p1Handicap,
                            TotalScore = ServiceProvider.ScoringInstance.RoundTotalScore(postedRoundPlayer1.Scores),
                            TotalPoints = scoringResults[0].Points,
                            BirdieCnt = scoringResults[0].Birdies,
                            ParCnt = scoringResults[0].Pars,
                            EagleCnt = scoringResults[0].Eagles,
                            BogeyCnt = scoringResults[0].Bogeys
                        };
                        var p2NewRound = new RoundModel()
                        {
                            PlayerRefId = postedRoundPlayer2.PlayerId,
                            RoundId = Guid.NewGuid(),
                            WeekId = vm.WeekId,
                            Scores = ServiceProvider.ScoringInstance.ExtractScores(postedRoundPlayer2.Scores),
                            Front = front,
                            SeasonFirstHalf = half,
                            Handicap = p2Handicap,
                            TotalScore = ServiceProvider.ScoringInstance.RoundTotalScore(postedRoundPlayer2.Scores),
                            TotalPoints = scoringResults[1].Points,
                            BirdieCnt = scoringResults[1].Birdies,
                            ParCnt = scoringResults[1].Pars,
                            EagleCnt = scoringResults[1].Eagles,
                            BogeyCnt = scoringResults[1].Bogeys
                        };
                        repoProvider.RoundRepo.Add(p1NewRound);
                        repoProvider.RoundRepo.Add(p2NewRound);
                    }
                    await repoProvider.SaveAllRepoChangesAsync();
                }
                return Ok();
            }
            return BadRequest();
        }