Exemple #1
0
        public Team Save(Team entity)
        {
            _context.Teams.Add(entity);
            _context.SaveChanges();

            return(entity);
        }
Exemple #2
0
        public IActionResult EnrollPlayer(int IdTeam, PlayerDTO playerDTO)
        {
            Team team = leagueContext.Teams.Find(IdTeam);

            if (team == null)
            {
                return(BadRequest("No team found!"));
            }

            Player player = leagueContext.Players
                            .Where(p => p.FirstName.Equals(playerDTO.FirstName))
                            .Where(p => p.LastName.Equals(playerDTO.LastName))
                            .Where(p => p.DateOfBirth.Equals(playerDTO.DateOfBirth)).First();

            if (player == null)
            {
                return(BadRequest("No player found!"));
            }

            var _link = leagueContext.PlayerTeams.Where(pt => pt.Player.Equals(player)).First();

            if (_link != null)
            {
                return(BadRequest("Player unavailable!"));
            }

            PlayerTeam PlayerTeam = new PlayerTeam();

            PlayerTeam.Player = player;
            PlayerTeam.Team   = team;

            leagueContext.PlayerTeams.Add(PlayerTeam);
            leagueContext.SaveChanges();
            return(Ok(playerDTO));
        }
Exemple #3
0
        private static async Task Tick(string leagueDbName)
        {
            // Load statistic sets and check for recalculation interval
            using (var dbContext = new LeagueDbContext(leagueDbName))
            {
                var statisticSets      = dbContext.Set <StatisticSetEntity>().ToList();
                var checkStatisticSets = statisticSets.Where(x => IsDueTick(x.UpdateTime, TimeSpanConverter.Convert(x.UpdateInterval))).OrderBy(x => GetTypePriority(x));


                dbContext.Configuration.LazyLoadingEnabled = false;
                foreach (var statisticSet in checkStatisticSets)
                {
                    await statisticSet.CheckRequireRecalculationAsync(dbContext);

                    if (statisticSet.RequiresRecalculation)
                    {
                        await statisticSet.LoadRequiredDataAsync(dbContext);

                        statisticSet.Calculate(dbContext);
                    }
                }
                dbContext.Configuration.LazyLoadingEnabled = true;
                dbContext.SaveChanges();
            }
            GC.Collect();
        }
        static void Main(string[] args)
        {
            // Create season statistic set
            //using (var dbContext = new LeagueDbContext("SkippyCup_leagueDb"))
            //{
            //    var season = dbContext.Seasons.Find(1);

            //    var seasonStatistics = new SeasonStatisticSetEntity();
            //    season.SeasonStatistics.Add(seasonStatistics);

            //    var sprintScoring = dbContext.Set<ScoringEntity>().Find(7);
            //    var enduranceScoring = dbContext.Set<ScoringEntity>().Find(8);

            //    seasonStatistics.Scorings.Add(sprintScoring);
            //    seasonStatistics.Scorings.Add(enduranceScoring);

            //    dbContext.SaveChanges();
            //}

            // Calculate season statistics
            using (var dbContext = new LeagueDbContext("SkippyCup_leagueDb"))
            {
                var seasonStatistics = dbContext.Set <SeasonStatisticSetEntity>().ToList();

                dbContext.Configuration.LazyLoadingEnabled = false;

                seasonStatistics.ForEach(x => x.LoadRequiredDataAsync(dbContext).Wait());
                seasonStatistics.ForEach(x => x.Calculate(dbContext));

                dbContext.SaveChanges();
            }

            // Create league statistic set
            //using (var dbContext = new LeagueDbContext("SkippyCup_leagueDb"))
            //{
            //    var leagueStatistic = new LeagueStatisticSetEntity();
            //    dbContext.LeagueStatistics.Add(leagueStatistic);

            //    leagueStatistic.StatisticSets.Add(dbContext.Seasons.First().SeasonStatistics.First());
            //    leagueStatistic.StatisticSets.Add(dbContext.Seasons.First().SeasonStatistics.Last());

            //    dbContext.SaveChanges();
            //}

            // Calculate league statistics
            using (var dbContext = new LeagueDbContext("SkippyCup_leagueDb"))
            {
                var leagueStatistic = dbContext.Set <LeagueStatisticSetEntity>().First();

                dbContext.Configuration.LazyLoadingEnabled = false;

                leagueStatistic.LoadRequiredDataAsync(dbContext).Wait();
                leagueStatistic.Calculate(dbContext);

                dbContext.SaveChanges();
            }

            //Console.Read();
        }
Exemple #5
0
        public IHttpActionResult PostLeague([FromUri] string leagueName, [FromUri] string fullName = "")
        {
            // check for empty body and invalid data
            if (string.IsNullOrEmpty(leagueName))
            {
                return(BadRequestEmptyParameter(nameof(leagueName)));
            }
            if (string.IsNullOrEmpty(fullName))
            {
                fullName = leagueName;
            }
            base.CheckLeagueRole(User, leagueName);

            var dbName   = GetDatabaseNameFromLeagueName(leagueName);
            var register = LeagueRegister.Get();

            var league = register.GetLeague(leagueName);

            if (league == null)
            {
                // check current number of leagues for that user
                string userId      = User.Identity.GetUserId();
                var    leagueCount = register.Leagues.Count(x => x.CreatorId.ToString() == userId);
                var    maxLeagues  = 3;
                if (leagueCount >= maxLeagues)
                {
                    return(BadRequest($"Create league failed. Maximum numbers of {maxLeagues} leagues per user reached."));
                }

                using (var dbContext = new LeagueDbContext(dbName, createDb: true))
                {
                    var seasons = dbContext.Seasons;
                    if (seasons == null || seasons.Count() == 0)
                    {
                        seasons.Add(new iRLeagueDatabase.Entities.SeasonEntity()
                        {
                            SeasonName             = "First Season",
                            CreatedByUserName      = User.Identity.Name,
                            CreatedByUserId        = User.Identity.GetUserId(),
                            LastModifiedByUserName = User.Identity.Name,
                            LastModifiedByUserId   = User.Identity.GetUserId()
                        });
                        dbContext.SaveChanges();
                        // Create new entry in league register
                        return(Ok($"New League {leagueName} with database {dbName} created!"));
                    }
                    UpdateLeague(leagueName, User);
                    return(BadRequest($"League {leagueName} already exists!"));
                }
            }

            // if league exists just update fullname
            league.PrettyName = fullName;
            register.Save();
            return(Ok("League information updated"));
        }
Exemple #6
0
 public Player Save(Player entity)
 {
     _context.Players.Add(entity);
     _context.SaveChanges();
     return(entity);
 }
Exemple #7
0
 public int Commit()
 {
     return(db.SaveChanges());
 }
Exemple #8
0
 public void Update()
 {
     _dal.SaveChanges();
 }