Exemple #1
0
        public async Task <ActionResult> AddPrediction([FromBody] PredictionItem prediction)
        {
            _context.PredictionItems.Add(prediction);
            await _context.SaveChangesAsync();

            return(Ok());
        }
Exemple #2
0
        public async Task <IActionResult> PutTopic([FromBody] Topic topic)
        {
            if (topic == null)
            {
                return(BadRequest($"Inputed Topic's data is null"));
            }

            _context.Entry(topic).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();

                return(Ok(topic));
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TopicExists(topic.TopicId))
                {
                    return(NotFound($"Could not found Topic with id={topic.TopicId}"));
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PostUserPrediction([FromBody] UserPrediction userPrediction)
        {
            userPrediction.CreationDate = DateTime.Now;
            userPrediction.UserId       = userId;
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.UserPredictions.Add(userPrediction);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetUserPrediction", new { id = userPrediction.Id }, userPrediction));
        }
Exemple #4
0
        private async Task ManageFixtures()
        {
            using (var client = new HttpClient())
            {
                var response = await client.GetAsync(new Uri(BOOTSTRAP_FIXTURES_URL));

                var result = await response.Content.ReadAsStringAsync();

                var jsonData = JsonConvert.DeserializeObject <List <BootstrapFixture> >(result);

                var fixturesToInsert = new List <Fixture>();

                jsonData.ForEach(f =>
                {
                    // Skip any fixtures without a gameweek reference
                    if (string.IsNullOrEmpty(f.@event))
                    {
                        return;
                    }

                    var gameWeek  = Convert.ToInt32(f.@event);
                    var homeTeam  = Convert.ToInt32(f.team_h);
                    var awayTeam  = Convert.ToInt32(f.team_a);
                    var homeScore = f.team_h_score != null ? Convert.ToInt32(f.team_h_score) : -1;
                    var awayScore = f.team_a_score != null ? Convert.ToInt32(f.team_a_score) : -1;

                    var fixture = new Fixture
                    {
                        Id          = Guid.NewGuid(),
                        GameWeek    = context.GameWeeks.FirstOrDefault(gw => gw.InternalId == gameWeek),
                        InternalId  = Convert.ToInt32(f.code),
                        KickoffDate = DateTime.Parse(f.kickoff_time),
                        HomeTeam    = context.Teams.FirstOrDefault(t => t.InternalId == homeTeam),
                        AwayTeam    = context.Teams.FirstOrDefault(t => t.InternalId == awayTeam),
                        HomeScore   = homeScore,
                        AwayScore   = awayScore
                    };

                    fixturesToInsert.Add(fixture);
                });

                foreach (var fixture in fixturesToInsert)
                {
                    context.Fixtures.AddOrUpdate(f => f.InternalId, fixture);
                    await context.SaveChangesAsync();
                }
            }
        }
Exemple #5
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();
            }
        }
Exemple #6
0
        public async Task <ActionResult> AddUser([FromBody] User user)
        {
            var createToken = new CreateToken();

            user.Token = createToken.CreateNewToken();

            _context.Users.Add(user);
            await _context.SaveChangesAsync();

            return(Ok($"User with name \"{user.UserName}\" added"));
        }