Exemplo n.º 1
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.");
            }
        }
        //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();
                }
            }
        }
Exemplo n.º 3
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);
            }
        }