Exemplo n.º 1
0
        /// <summary>
        /// Handles saving a list of predictions made by the specified user. Will not save a prediction if it fails some basic validation
        /// checks, such as having a valid scoreline, valid fixture, deadline has not passed for the fixture's gameweek.
        /// </summary>
        /// <param name="predictions"></param>
        /// <param name="userName"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task SavePredictions(List <Prediction> predictions, string userName)
        {
            foreach (var prediction in predictions.Where(prediction =>
                                                         prediction.HomeScore >= 0 && prediction.AwayScore >= 0 &&
                                                         prediction.HomeScore <= 10 && prediction.AwayScore <= 10))
            {
                // Check that fixture actually exists
                var fixture = await fixtureService.GetFixtureById(prediction.Fixture.Id);

                if (fixture == null)
                {
                    continue;
                }

                // Deadline has passed don't save anything.
                if (DateTime.Now >= fixture.GameWeek.DeadlineDate)
                {
                    continue;
                }

                // Set user properly
                if (prediction.User == null)
                {
                    prediction.User = await userService.GetUserByName(userName);
                }

                // Set fixture properly
                prediction.Fixture = fixture;

                context.Predictions.AddOrUpdate(p => p.Id, prediction);
                await context.SaveChangesAsync();
            }
        }