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