public void EventIndexer_IntegrationTest()
        {
            var eventIndexer = new EventIndexer();

            eventIndexer.IndexEvents(9);            // 2014 in test data

            var context = DataEntitiesProvider.Provide();
            var events  = context.Events.Where(x => x.OlympiadId == 9).ToList();

            // This depends on the sessions not being altered - unlikely now
            Assert.AreEqual(70, events.Max(e => e.Number));
            Assert.AreEqual(5, events.Count(e => e.Number == 0));
        }
        public void Save()
        {
            var           context = DataEntitiesProvider.Provide();
            var           id      = OlympiadId;
            Olympiad_Info o;

            if (id == 0)
            {
                o = new Olympiad_Info()
                {
                    YearOf     = int.Parse(this.YearOf),
                    Number     = this.Number,
                    Title      = this.Title,
                    Venue      = this.Venue,
                    StartDate  = this.StartDate,
                    FinishDate = this.FinishDate,
                    MaxFee     = decimal.Parse(this.MaxFee),
                    MaxCon     = decimal.Parse(this.MaxCon),
                    AgeDate    = this.AgeDate,
                    JnrAge     = int.Parse(this.JnrAge),
                    SnrAge     = int.Parse(this.SnrAge),
                    PentaLong  = int.Parse(this.PentaLong),
                    PentaTotal = int.Parse(this.PentaTotal),
                    Events     = new List <Event>(),
                    Current    = false, // will be sorted out later
                };
                context.Olympiad_Infoes.Add(o);
                context.SaveChanges();
                // So we don't have to do a full refresh of the combo
                Olympiads.RemoveAt(0);
                Olympiads.Insert(0, new OlympiadVm()
                {
                    Text = o.FullTitle(), Id = o.Id
                });
                id = o.Id;
            }
            else
            {
                o            = context.Olympiad_Infoes.FirstOrDefault(x => x.Id == id);
                o.YearOf     = int.Parse(this.YearOf);
                o.Number     = this.Number;
                o.Title      = this.Title;
                o.Venue      = this.Venue;
                o.StartDate  = this.StartDate;
                o.FinishDate = this.FinishDate;
                o.MaxFee     = decimal.Parse(this.MaxFee);
                o.MaxCon     = decimal.Parse(this.MaxCon);
                o.AgeDate    = this.AgeDate;
                o.JnrAge     = int.Parse(this.JnrAge);
                o.SnrAge     = int.Parse(this.SnrAge);
                o.PentaLong  = int.Parse(this.PentaLong);
                o.PentaTotal = int.Parse(this.PentaTotal);
                o.Current    = false;   // will be sorted out later
            }
            context.SaveChanges();
            // Now update the events and locations. Need to do here to have the reference back to the Olympiad
            foreach (var existingEvent in o.Events.ToList())
            {
                if (!Events.Any(x => x.Id == existingEvent.EIN))
                {
                    o.Events.Remove(existingEvent);
                    context.Events.Remove(existingEvent);
                }
            }
            foreach (var existingLocation in o.Locations.ToList())
            {
                if (!Locations.Any(x => x.Id == existingLocation.Id))
                {
                    o.Locations.Remove(existingLocation);
                    context.Locations.Remove(existingLocation);
                }
            }
            foreach (var evm in Events)
            {
                if (evm.Id == 0)
                {
                    var game = context.Games.FirstOrDefault(x => evm.Code.StartsWith(x.Code));
                    if (game == null)
                    {
                        throw new ArgumentOutOfRangeException("No Game for code " + evm.Code);
                    }

                    var evt = new Event()
                    {
                        Mind_Sport    = evm.Name,
                        Code          = evm.Code,
                        Olympiad_Info = o,
                        Game          = game,
                        MAX_Number    = 70,
                        ConsistentWithBoardability = true,
                        PentamindFactor            = 1.0f
                                                     // TODO more stuff here
                    };
                    o.Events.Add(evt);
                }
                else
                {
                    var evt = context.Events.Find(evm.Id);
                    // We're not doing any update here?
                }
            }
            foreach (var loc in Locations)
            {
                if (loc.Id == 0)
                {
                    o.Locations.Add(new Location()
                    {
                        Location1 = loc.Name, Olympiad_Info = o, YEAR = o.YearOf
                    });
                }
                // Not doing updates here
            }
            context.SaveChanges();

            // Make sure Current is set properly
            var oldCurrents = context.Olympiad_Infoes.Where(x => x.Current).ToList();

            oldCurrents.ForEach(ol => ol.Current = false);
            var newCurrent = context.Olympiad_Infoes.OrderByDescending(x => x.StartDate).First();

            newCurrent.Current = true;
            context.SaveChanges();

            var eventIndexer = new EventIndexer();

            eventIndexer.IndexEvents(id);

            IsDirty    = false;
            OlympiadId = id;
        }
Пример #3
0
        public void Save()
        {
            var context = DataEntitiesProvider.Provide();
            var evt     = context.Events.First(x => x.OlympiadId == CurrentOlympiadId && x.Code == EventCode);

            // There's no "create" here
            if (evt == null)
            {
                throw new ArgumentException("No such event " + EventCode + " in olympiad " + CurrentOlympiadId);
            }

            evt.EIN = EventId;
            evt.Arbiters.Clear();
            var arbiter = context.Contestants.ToList().FirstOrDefault(a => a.FullName() == Arbiter);

            if (arbiter != null)
            {
                evt.Arbiters.Add(new Arbiter()
                {
                    EIN = EventId, Name = arbiter
                });
            }
            evt.Location         = Location;
            evt.Entry_Fee        = EntryFee;
            evt.Number_in_Team   = NumberInTeam;
            evt.Prize_fund       = PrizeFund;
            evt.C1st_Prize       = Prize1.ToString();
            evt.C2nd_Prize       = Prize2.ToString();
            evt.C3rd_Prize       = Prize3.ToString();
            evt.JNR_1st_Prize    = JuniorPrize1.ToString();
            evt.JNR_2nd_Prize    = JuniorPrize2.ToString();
            evt.JNR_3rd_Prize    = JuniorPrize3.ToString();
            evt.Other_Prizes     = OtherPrizes;
            evt.JNR_Other_Prizes = JuniorOtherPrizes;
            evt.Pentamind        = Pentamind;
            evt.PentamindFactor  = PentamindFactor;
            evt.incMaxFee        = IncludedInMaxFee;
            evt.JNR_Medals       = JuniorMedals;
            evt.Type             = Type;
            evt.X_Num            = ExpectedNumber;
            evt.Notes            = Notes;
            evt.No_Sessions      = NumSessions;

            evt.Event_Sess.Clear();
            foreach (var s in Sessions)
            {
                var session = new Event_Sess()
                {
                    EIN = EventId, Date = s.Date, Session = s.SessionCode
                };
                evt.Event_Sess.Add(session);
            }

            // Entrants are *not* added here but their ranks etc are
            foreach (var e in Entrants)
            {
                var entrant = context.Entrants.First(x => x.EntryNumber == e.EntrantId);
                entrant.Absent      = e.Absent;
                entrant.Medal       = (string.IsNullOrEmpty(e.Medal)) ? null : e.Medal;
                entrant.JuniorMedal = (string.IsNullOrEmpty(e.JuniorMedal)) ? null : e.JuniorMedal;
                entrant.Rank        = e.Rank;
                // zero length constraint
                entrant.Score = (string.IsNullOrWhiteSpace(e.Score)) ? null : e.Score;
                if (e.Rank > 0)
                {
                    entrant.Penta_Score = e.PentaScore;
                }
                else
                {
                    entrant.Penta_Score = null;
                }
                entrant.Tie_break = e.TieBreak;
                entrant.Partner   = e.TeamOrPair;
            }

            context.SaveChanges();

            // Now set the Number. This has to be done in conjunction with other events.
            var indexer = new EventIndexer();

            indexer.IndexEvents(CurrentOlympiadId);

            // Can't see a better place to do this for now.
            OnPropertyChanged("Totals");
        }