Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            //druzyny wpisane do bazy ef code first
            //na podstawie nazwy druzyn znaleść aktualnego trenera
            // dopisanie trenera do druzyny
            // dla drużyn pobieramy trenera
            using var context = new CollegeFootballContext();
            var data = new Data();

            int option = 0;

            while (option != 3)
            {
                Console.WriteLine("---1---    Import Data and create tables\n" +
                                  "---2---    Display Teams and coaches\n" +
                                  "---3---    Exit");
                if (int.TryParse(Console.ReadLine(), out option))
                {
                }
                switch (option)
                {
                case 1:
                    var client = new Client("https://api.collegefootballdata.com");

                    var teamsJSON = await client.GetResponseAsync("/teams/fbs?year=2019");

                    var coachesJSON = await client.GetResponseAsync("/coaches?year=2019");


                    var teams   = JsonConvert.DeserializeObject <List <Team> >(teamsJSON);
                    var coaches = JsonConvert.DeserializeObject <List <Coach> >(coachesJSON);


                    context.Database.EnsureCreated();

                    await data.AddTeamsAsync(teams);

                    await data.AddCoachesAsync(coaches);


                    //przypisanie trenera do druzyny
                    await data.UpdateAsync();

                    break;

                case 2:
                    var teamsList = context.Teams.ToList();

                    foreach (var team in teamsList)
                    {
                        Console.WriteLine($"Team: {team.School}\n\tCoach: {NullToString(team.Coach)}\n");
                    }
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        public async Task AddCoachesAsync(ICollection <Coach> coaches)
        {
            await using var context = new CollegeFootballContext();
            context.Database.OpenConnection();
            await context.Coaches.AddRangeAsync(coaches);

            await context.SaveChangesAsync();

            await context.Database.CloseConnectionAsync();
        }
Ejemplo n.º 3
0
        public async Task <Coach> GetTeamCoachAsync(string teamName)
        {
            await using var context = new CollegeFootballContext();
            context.Database.OpenConnection();
            var coach = context.Seasons.Where(s => s.School == teamName)
                        .Select(x => x.Coach)
                        .FirstOrDefault();

            context.Database.CloseConnection();
            return(coach);
        }
Ejemplo n.º 4
0
        public async Task UpdateAsync()
        {
            await using var context = new CollegeFootballContext();
            context.Database.OpenConnection();
            foreach (var team in context.Teams)
            {
                var a = GetTeamCoachAsync(team.School).Result;
                if (a != null)
                {
                    team.Coach   = a;
                    team.CoachId = a.Id;
                }
            }
            await context.SaveChangesAsync();

            context.Database.CloseConnection();
        }
Ejemplo n.º 5
0
        public async Task AddTeamsAsync(ICollection <Team> teams)
        {
            await using var context = new CollegeFootballContext();
            context.Database.OpenConnection();
            await context.Teams.AddRangeAsync(teams);

            context.Database.OpenConnection();
            try
            {
                context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Teams ON");
                await context.SaveChangesAsync();

                context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.Teams OFF");
            }
            finally
            {
                await context.Database.CloseConnectionAsync();
            }
        }