Пример #1
0
        private static async Task DeleteOldCandidates(ContestantRepository repository, IEnumerable <Contestant> contestantsInDb, ContestantRequest[] masterContestantList, int warId, Func <Contestant, ContestantRequest, bool> IsTheSame)
        {
            var additions = contestantsInDb.Where(c1 => !masterContestantList.Any(c2 => IsTheSame(c1, c2)));

            foreach (var c in additions)
            {
                await repository.Delete(warId, c);
            }
        }
Пример #2
0
        private static async Task InsertNewCandidates(ContestantRepository repository, IEnumerable <Contestant> contestantsInDb, ContestantRequest[] masterContestantList, int warId, Func <Contestant, ContestantRequest, bool> IsTheSame)
        {
            var additions = masterContestantList.Where(c1 => !contestantsInDb.Any(c2 => IsTheSame(c2, c1)));

            foreach (var c in additions)
            {
                await repository.Create(warId, c);
            }
        }
Пример #3
0
        public async Task SyncContestants(string connectionString, ContestantRequest[] masterContestantList, int warId, string warTitle, Func <Contestant, ContestantRequest, bool> IsTheSame)
        {
            await VerifyThatTheWarIdIsCorrect(connectionString, warId, warTitle);

            var repository      = new ContestantRepository(connectionString);
            var contestantsInDb = await repository.GetAll(warId);

            await UpdateExistingCandidates(repository, contestantsInDb, masterContestantList, warId, IsTheSame);
            await InsertNewCandidates(repository, contestantsInDb, masterContestantList, warId, IsTheSame);
            await DeleteOldCandidates(repository, contestantsInDb, masterContestantList, warId, IsTheSame);
        }
Пример #4
0
        private static async Task UpdateExistingCandidates(ContestantRepository repository, IEnumerable <Contestant> contestantsInDb, ContestantRequest[] masterContestantList, int warId, Func <Contestant, ContestantRequest, bool> IsTheSame)
        {
            var updates = masterContestantList.Where(c1 => contestantsInDb.Any(c2 => IsTheSame(c2, c1)))
                          .Select(c => new Contestant
            {
                Definition = c.Definition,
                Id         = contestantsInDb.Single(c1 => IsTheSame(c1, c)).Id
            });

            foreach (var c in updates)
            {
                await repository.Update(warId, c);
            }
        }
        internal static void StatisticsForEachSport() //KLART
        {
            var cRep  = new SportRepository();
            var stats = cRep.SportStatisticsAsync();
            var mRep  = new ContestantRepository();
            var stat  = mRep.GetAllAsync();


            var totalContestants = 0;

            foreach (var x in stats.Result)
            {
                int contestants = x.Contestants.Count;
                totalContestants += contestants;
            }
            Console.WriteLine("Statistics for each Sport: \n");
            foreach (var y in stats.Result)
            {
                int    male     = 0;
                int    female   = 0;
                int    sportid  = y.Id;
                int    x        = y.Contestants.Count;
                int    contests = y.Contests.Count();
                int    match    = 0;
                double percent  = x * 100 / totalContestants;
                foreach (var z in stat.Result.Where(s => s.Gender.StartsWith("male") && s.SportId == sportid))
                {
                    male = z.FirstName.Count();
                }
                foreach (var z in stat.Result.Where(s => s.Gender.StartsWith("female") && s.SportId == sportid))
                {
                    female = z.FirstName.Count();
                }
                foreach (var z in stat.Result.Where(s => s.SportId == sportid))
                {
                    match = z.Matches.Count();
                }
                Console.WriteLine("{0} has {1} % of the participants", y.SportName, percent);
                Console.WriteLine("{0} has {1} male and {2} female contestants. ", y.SportName, male, female);
                Console.WriteLine("{0} has {1} contests. ", y.SportName, contests);
                Console.WriteLine("{0} has {1} scheduled matches.\n ", y.SportName, match);
            }
        }
Пример #6
0
        //Lägger till flera deltagare till samma match.
        internal static void AddManyToMatch()//KLAR
        {
            int    sportId = 6;
            string gender  = "female";
            var    mRep    = new MatchRepository();
            var    coRep   = new ContestantRepository();

            var participants = coRep.FindBy(m => m.SportId.Equals(sportId) && m.Gender.Equals(gender));

            foreach (var competer in participants)
            {
                mRep.Add(new Domain.Match
                {
                    DateTime     = new DateTime(2018, 2, 16),
                    Arena        = "summer ",
                    ContestId    = 5,
                    ContestantId = competer.Id,
                    RefereeId    = 6
                });
            }
            mRep.Save();
        }
        //Uppdaterar data i grundtabeller och one to many. Använder consolen i brist på web.
        internal static void UpdateBasicData(string parameter) //KLAR
        {
            var spRep = new SportRepository();
            var alls  = spRep.GetAll();
            var coRep = new CountryRepository();
            var allc  = coRep.GetAll();

            if (parameter == "Contestant")
            {
                var mRep = new ContestantRepository();
                var all  = mRep.GetAll();
                foreach (var x in all)
                {
                    Console.WriteLine("Id " + x.Id + ":\t" + "FirstName: " + x.FirstName
                                      + "\t" + "LastName: " + x.LastName + "\t" + "Age " + x.Age + "\t" + "Gender "
                                      + x.Gender + "\t" + "CountryId " + x.CountryId + "\t" + "SportId " + x.SportId);
                }
                int id     = UpdateId();
                var change = mRep.FindBy(m => m.Id.Equals(id));
                foreach (var x in change)
                {
                    Console.Write("Enter new FirstName: ");
                    x.FirstName = Console.ReadLine();
                    Console.Write("Enter new LastName: ");
                    x.LastName = Console.ReadLine();
                    Console.Write("Enter new Age: ");
                    x.Age = int.Parse(Console.ReadLine());
                    Console.Write("Enter new Gender (male/female): ");
                    x.Gender = Console.ReadLine();
                    foreach (var y in allc)
                    {
                        Console.WriteLine(y.Id + " = " + y.CountryName);
                    }

                    Console.Write("Enter new CountryId: ");
                    x.CountryId = int.Parse(Console.ReadLine());
                    foreach (var y in alls)
                    {
                        Console.WriteLine(y.Id + " = " + y.SportName);
                    }
                    Console.Write("Enter new SportId: ");
                    x.SportId = int.Parse(Console.ReadLine());
                    mRep.Update(x);
                    mRep.Save();
                }
            }
            if (parameter == "Contest")
            {
                var mRep = new ContestRepository();
                var all  = mRep.GetAll();
                foreach (var x in all)
                {
                    Console.WriteLine("Id " + x.Id + ":\t" + "ContestName: " + x.ContestName + "\t\t" + "SportId " + x.SportId);
                }
                int id     = UpdateId();
                var change = mRep.FindBy(m => m.Id.Equals(id));
                foreach (var x in change)
                {
                    Console.Write("Enter new ContestName: ");
                    x.ContestName = Console.ReadLine();
                    foreach (var y in alls)
                    {
                        Console.WriteLine(y.Id + " = " + y.SportName);
                    }
                    Console.Write("Enter new SportId: ");
                    x.SportId = int.Parse(Console.ReadLine());
                    mRep.Update(x);
                    mRep.Save();
                }
            }
            if (parameter == "Sport")
            {
                foreach (var x in alls)
                {
                    Console.WriteLine("Id " + x.Id + ":\t" + "SportName: " + x.SportName);
                }
                int id = UpdateId();

                var change = spRep.FindBy(m => m.Id.Equals(id));
                foreach (var x in change)
                {
                    Console.Write("Enter new SportName: ");
                    x.SportName = Console.ReadLine();
                    spRep.Update(x);
                    spRep.Save();
                }
            }
            if (parameter == "Country")
            {
                foreach (var x in allc)
                {
                    Console.WriteLine("Id " + x.Id + ":\t" + "CountryName " + x.CountryName + "\t\t" + "Gold " + x.Gold + "\t" + "Silver " + x.Silver + "\t" + "Bronze " + x.Bronze);
                }
                int id     = UpdateId();
                var change = coRep.FindBy(m => m.Id.Equals(id));
                foreach (var x in change)
                {
                    Console.Write("Enter new CountryName: ");
                    x.CountryName = Console.ReadLine();
                    Console.Write("Enter new Gold: ");
                    x.Gold = int.Parse(Console.ReadLine());
                    Console.Write("Enter new Silver: ");
                    x.Silver = int.Parse(Console.ReadLine());
                    Console.Write("Enter new Bronze: ");
                    x.Bronze = int.Parse(Console.ReadLine());
                    coRep.Update(x);
                    coRep.Save();
                }
            }
            if (parameter == "Referee")
            {
                var mRep = new RefereeRepository();
                var all  = mRep.GetAll();
                foreach (var x in all)
                {
                    Console.WriteLine("Id " + x.Id + ":\t" + "Name: " + x.Name + "\t\t" + "CountryId " + x.CountryId);
                }
                int id     = UpdateId();
                var change = mRep.FindBy(m => m.Id.Equals(id));
                foreach (var x in change)
                {
                    Console.Write("Enter new Name: ");
                    x.Name = Console.ReadLine();
                    foreach (var y in allc)
                    {
                        Console.WriteLine(y.Id + " = " + y.CountryName);
                    }
                    Console.Write("Enter new CountryId: ");
                    x.CountryId = int.Parse(Console.ReadLine());
                    mRep.Update(x);
                    mRep.Save();
                }
            }
        }
Пример #8
0
        // Lägger till Basic data samt till one to many tabeller.
        // För övrigt gäller samma kommentarer som till metoden under.
        internal static void AddBasicData(string olympicModel, string contestantFirstName, string contestantLastName, int age,
                                          string gender, string country, string sport, string contestName, string refereeName) //KLAR
        {
            int sportID      = 0;
            int countryID    = 0;
            int contestID    = 0;
            int refereeID    = 0;
            int contestantID = 0;

            var conRep = new ContestantRepository();
            var conId  = conRep.FindBy(m => m.FirstName.Equals(contestantFirstName) && m.LastName.Equals(contestantLastName));

            foreach (var c in conId)
            {
                contestantID = c.Id;
            }
            var cRep = new CountryRepository();
            var cId  = cRep.FindBy(m => m.CountryName.StartsWith(country));

            foreach (var c in cId)
            {
                countryID = c.Id;
            }

            var sRep = new SportRepository();
            var sId  = sRep.FindBy(m => m.SportName.Equals(sport));

            foreach (var c in sId)
            {
                sportID = c.Id;
            }
            var coRep = new ContestRepository();
            var coId  = coRep.FindBy(m => m.ContestName.Equals(contestName));

            foreach (var c in coId)
            {
                contestID = c.Id;
            }
            var reRep = new RefereeRepository();
            var reId  = reRep.FindBy(m => m.Name.Equals(refereeName));

            foreach (var c in reId)
            {
                refereeID = c.Id;
            }
            try
            {
                if (olympicModel == "Country" && countryID == 0)
                {
                    var addCountry = new CountryRepository();
                    addCountry.Add(new Country {
                        CountryName = country
                    });
                    addCountry.Save();
                }
            }
            catch
            {
                Console.WriteLine("{0} already exists in db.", olympicModel);
            }
            try
            {
                if (olympicModel == "Sport" && sportID == 0)
                {
                    var addSport = new SportRepository();
                    addSport.Add(new Sport {
                        SportName = sport
                    });
                    addSport.Save();
                }
            }
            catch
            {
                Console.WriteLine("{0} already exists in db.", olympicModel);
            }
            try
            {
                if (olympicModel == "Contest" && contestID == 0 && sportID != 0)
                {
                    var addContest = new ContestRepository();
                    addContest.Add(new Contest {
                        ContestName = contestName, SportId = sportID
                    });
                    addContest.Save();
                }
                else if (olympicModel == "Contest" && contestID != 0)
                {
                    Console.WriteLine("{0} already exists in db.", olympicModel);
                }
            }
            catch
            {
                Console.WriteLine("{0} is not added yet. You have to register OlympicModel: {1}, first.", olympicModel, sport);
            }
            try
            {
                if (olympicModel == "Contestant" && contestantID == 0 && sportID != 0 && countryID != 0)
                {
                    var addContestant = new ContestantRepository();
                    addContestant.Add(new Contestant {
                        FirstName = contestantFirstName, LastName = contestantLastName, Age = age, CountryId = countryID, Gender = gender, SportId = sportID
                    });
                    addContestant.Save();
                }
                else if (olympicModel == "Contestant" && contestantID != 0)
                {
                    Console.WriteLine("{0} already exists in db.", olympicModel);
                }
            }
            catch
            {
                Console.WriteLine("This sport or country is not added yet. Select OlympicModel: Sport/Country, first.");
            }
            try
            {
                if (olympicModel == "Referee" && refereeID == 0 && countryID != 0)
                {
                    var addReferee = new RefereeRepository();
                    addReferee.Add(new Referee {
                        Name = refereeName, CountryId = countryID
                    });
                    addReferee.Save();
                    Console.WriteLine("Data is now added!");
                }
            }
            catch
            {
                Console.WriteLine("This country is not added yet. Select OlympicModel: Country, first.");
                Console.ReadKey();
            }
            if (olympicModel == "Match")
            {
                Console.WriteLine("This method doesen't work for many to many. Select AddManyToMany(), instead.");
            }
        }
Пример #9
0
        //Lägger till many to many baserat på tänkt input från Gui, i detta fall manuell input från Program.
        //i verkligt scenario skulle jag valt dropdowns från UI/Web som hämtar querys från db baserat på tidigare dropdown val
        // så att användaren aldrig kan välja alternativ som inte finns/uppfyller db-kriterier.
        // i detta exempel blir minsta felstavning fel och har varit väldigt tröttsamt att testa igenom, då jag inte har alla id:n i huvudet.....
        internal static void AddManyToMany(string contestantFirst, string contestantLast, string contest, string refereeName,
                                           string arena, DateTime dateTime, string country)
        {
            int refereeID    = 0;
            int contestantID = 0;
            int contestID    = 0;
            int sportId      = 0;
            int sportID      = 0;
            var refRep       = new RefereeRepository();
            var coRep        = new ContestantRepository();
            var conRep       = new ContestRepository();
            var mRep         = new MatchRepository();
            var re           = refRep.FindBy(r => r.Name.EndsWith(refereeName)); // lärdom: endswith funkar inte när man länkar hela strängen....
            var refe         = refRep.GetAll();
            var con          = conRep.FindBy(c => c.ContestName.Equals(contest));
            var co           = coRep.FindBy(m => m.FirstName.Equals(contestantFirst) && m.LastName.Equals(contestantLast));

            foreach (var c in co)
            {
                contestantID = c.Id;
                sportId      = c.SportId;
            }
            foreach (var c in con)
            {
                contestID = c.Id;
                sportID   = c.SportId;
            }
            foreach (var c in re)
            {
                if (refereeName == c.Name)
                {
                    refereeID = c.Id;
                }
            }

            var ma = mRep.GetAll();

            foreach (var c in ma)
            {
                try  //kollar att arenan inte dubbelbokas
                {
                    if (arena == c.Arena && c.DateTime == dateTime && c.ContestId != contestID)
                    {
                    }
                }
                catch
                {
                    Console.WriteLine("This arena is already booked for another event at this date. Please correct input.");
                }
            }

            if (contestantID == 0 || contestID == 0 || sportId != sportID || refereeID == 0)
            {
                Console.WriteLine("Some info didn't match.\nContestantid: {0}\nContestid: {1}\nSportidn: {2} and {3} has to be the same.\nRefereeId: {4}", contestantID, contestID, sportId, sportID, refereeID);
                Console.WriteLine("\n\nIf refereeId is 0, ({0}), choose a referee from below list:", refereeName);
                foreach (var x in refe)
                {
                    Console.WriteLine(x.Name);
                }
            }
            else
            {
                try // lägger till en spelare till en match
                {
                    var addContestantToMatch = new MatchRepository();
                    addContestantToMatch.Add(new Match {
                        ContestantId = contestantID, ContestId = contestID, Arena = arena, DateTime = dateTime, RefereeId = refereeID
                    });
                    addContestantToMatch.Save();
                }
                catch
                {
                    Console.WriteLine("{0} {1} is already participating in {2}.", contestantFirst, contestantLast, contest);
                }
            }
        }
Пример #10
0
        //Tar bort ett id och dess barn. Förutom i två tabeller, där jag gjorde fel i migreringen och inte lyckades rätta till det.
        // Se kommentarer på rad 81 i OlympicContext.
        internal static void DeleteBasicData(string olympicModel, string contestantFirstName, string contestantLastName, string country, string sport, string contestName, string refereeName)
        {
            var mRep = new MatchRepository();


            if (olympicModel == "Country")
            {
                try                         // Denna nödlösning för jag misslyckades att ändra Restict till Cascade i min sista migration.
                {
                    var cRep = new CountryRepository();
                    var co   = cRep.FindBy(c => c.CountryName == country);
                    cRep.DeleteRange(co);
                    cRep.Save();
                    return;
                }
                catch
                {
                    var match = mRep.FindBy(m => m.Contestant.Country.CountryName.Equals(country));
                    foreach (var x in match)
                    {
                        Console.WriteLine("Match with keyvaluepairs: " + x.ContestId + " - " + x.ContestantId + " has to be deleted first.");
                    }
                    return;
                }
            }
            if (olympicModel == "Sport")
            {
                var cRep = new SportRepository();
                var co   = cRep.FindBy(c => c.SportName == sport);
                cRep.DeleteRange(co);
                cRep.Save();
                return;
            }
            if (olympicModel == "Referee")
            {
                var cRep = new RefereeRepository();
                var co   = cRep.FindBy(c => c.Name == refereeName);
                cRep.DeleteRange(co);
                cRep.Save();
                return;
            }
            if (olympicModel == "Contest")
            {
                var cRep = new ContestRepository();
                var co   = cRep.FindBy(c => c.ContestName == contestName);
                cRep.DeleteRange(co);
                cRep.Save();
                return;
            }
            if (olympicModel == "Contestant")
            {
                try                          // Denna nödlösning för jag misslyckades att ändra Restict till Cascade i min sista migration. (Tror jag i alla fall)
                {
                    var cRep = new ContestantRepository();
                    var co   = cRep.FindBy(c => c.FirstName == contestantFirstName && c.LastName == contestantLastName);
                    cRep.DeleteRange(co);
                    cRep.Save();
                    return;
                }
                catch
                {
                    var match = mRep.FindBy(m => m.Contestant.FirstName.Equals(contestantFirstName) && m.Contestant.LastName.Equals(contestantLastName));
                    foreach (var x in match)
                    {
                        Console.WriteLine("Match with keyvaluepairs: " + x.ContestId + " - " + x.ContestantId + " has to be deleted first.");
                    }
                    return;
                }
            }
            else
            {
                System.Console.WriteLine("{0} does not exist in database.", olympicModel);
            }
        }