示例#1
0
        public SaveChampionshipResult SaveChampionship(ChampionshipDTO championship)
        {
            SaveChampionshipResult result = new SaveChampionshipResult();

            if (!SecurityInfo.Licence.IsInstructor)
            {
                throw new LicenceException("This feature is allowed for Instructor account");
            }
            if (championship.IsNew)
            {
                throw new InvalidOperationException("Cannot create a championship using this method");
            }

            using (var trans = Session.BeginSaveTransaction())
            {
                var dbMe           = Session.Load <Profile>(SecurityInfo.SessionData.Profile.GlobalId);
                var dbChampionship = Session.QueryOver <Championship>()
                                     .Fetch(x => x.Reservations).Eager
                                     .Fetch(x => x.Reservations.First().Customer).Eager
                                     .Where(x => x.GlobalId == championship.GlobalId).SingleOrDefault();

                //for eager fetch only
                Session.QueryOver <Championship>()
                .Fetch(x => x.Customers).Eager
                //.Fetch(x => x.Categories).Eager
                .Fetch(x => x.Entries).Eager
                .Where(x => x.GlobalId == championship.GlobalId).SingleOrDefault();


                if (dbChampionship.Profile != dbMe)
                {
                    throw new CrossProfileOperationException("Championship belongs to another user");
                }
                if (dbChampionship.State != ScheduleEntryState.Done)
                {
                    throw new InvalidOperationException("You can add data to championships with Done state only");
                }
                if (dbChampionship.Version != championship.Version)
                {
                    throw new StaleObjectStateException("Championship", championship.GlobalId);
                }


                dbChampionship.Results.Clear();
                Session.Flush();

                var championshipDb = championship.Map <Championship>();

                championshipDb.Profile = dbMe;
                championshipDb.Version = dbChampionship.Version;

                //first clear old result items
                //championshipDb.Results = dbChampionship.Results;
                //championshipDb.Results.Clear();


                championshipDb.Reservations = dbChampionship.Reservations;
                //check if all customers are in reservation list (there cannot be customer in Customers list and not in Reservations)
                foreach (var championshipCustomer in championshipDb.Customers)
                {
                    if (championshipDb.Reservations.Where(x => x.Customer == championshipCustomer.Customer).Count() == 0)
                    {
                        throw new ObjectNotFoundException("Customer is not available in reservations");
                    }
                }

                var benchPress = Session.Load <Exercise>(new Guid("ece5dfd7-f995-45ae-bb34-067f26c4f7b4"));
                var deadlift   = Session.Load <Exercise>(new Guid("505988e1-5663-41f1-aa1a-9b92ea584263"));
                var sqad       = Session.Load <Exercise>(new Guid("3e06a130-b811-4e45-9285-f087403615bf"));

                //calculate max for each exercise
                foreach (var entryDto in championshipDb.Entries)
                {
                    if (championshipDb.ChampionshipType == ChampionshipType.ZawodyWyciskanieSztangi)
                    {
                        if (entryDto.Exercise != benchPress)
                        {
                            throw new ConsistencyException("Wrong exercise");
                        }
                    }
                    else
                    {
                        if (entryDto.Exercise != benchPress && entryDto.Exercise != deadlift && entryDto.Exercise != sqad)
                        {
                            throw new ConsistencyException("Wrong exercise");
                        }
                    }
                    decimal max = 0;
                    if (entryDto.Try1.Result == ChampionshipTryResult.Success && max < entryDto.Try1.Weight)
                    {
                        max = entryDto.Try1.Weight;
                    }
                    if (entryDto.Try2.Result == ChampionshipTryResult.Success && max < entryDto.Try2.Weight)
                    {
                        max = entryDto.Try2.Weight;
                    }
                    if (entryDto.Try3.Result == ChampionshipTryResult.Success && max < entryDto.Try3.Weight)
                    {
                        max = entryDto.Try3.Weight;
                    }
                    entryDto.Max   = max;
                    entryDto.Wilks = calculateWilks(entryDto.Customer.Customer, entryDto.Customer.Weight, max);
                }

                //reset total and wilks. they will be calculated later
                foreach (var customer in championshipDb.Customers)
                {
                    customer.TotalWilks = 0;
                    customer.Total      = 0;
                }
                //calculate total sum
                foreach (var group in championshipDb.Entries.GroupBy(x => x.Customer))
                {
                    foreach (var entryDto in group)
                    {
                        group.Key.Total += entryDto.Max;
                    }
                    group.Key.TotalWilks = calculateWilks(group.Key.Customer, group.Key.Weight, group.Key.Total);
                }

                calculateResults(championshipDb);

                calculatePostCategories(championshipDb);

                var records = createStrengthTrainingEntries(dbMe, championshipDb, dbChampionship);
                result.NewRecords = records.Map <IList <SerieDTO> >();

                championshipDb = Session.Merge(championshipDb);

                trans.Commit();
                result.Championship = championshipDb.Map <ChampionshipDTO>();
            }
            return(result);
        }
示例#2
0
        private void displayNotificationAboutRecords(SaveChampionshipResult saveResult)
        {
            if (saveResult == null)
            {
                return;
            }
            foreach (var item in Items)
            {
                item.Exercise1.IsExercise1Try1Record = false;
                item.Exercise1.IsExercise1Try2Record = false;
                item.Exercise1.IsExercise1Try1Record = false;
                item.Exercise2.IsExercise1Try1Record = false;
                item.Exercise2.IsExercise1Try2Record = false;
                item.Exercise2.IsExercise1Try1Record = false;
                item.Exercise3.IsExercise1Try1Record = false;
                item.Exercise3.IsExercise1Try2Record = false;
                item.Exercise3.IsExercise1Try1Record = false;
            }

            foreach (var currentRecord in saveResult.NewRecords)
            {
                //RecordNotifyObject notifyObject = new RecordNotifyObject(currentRecord);
                //MainWindow.Instance.ShowNotification(notifyObject);
                var item = Items.Where(x => x.Customer.GlobalId == currentRecord.StrengthTrainingItem.StrengthTrainingEntry.TrainingDay.CustomerId).SingleOrDefault();
                if (item.Exercise1.Entry.Exercise.GlobalId == currentRecord.StrengthTrainingItem.Exercise.GlobalId)
                {
                    if (item.Exercise1.Exercise1Try1Weight == currentRecord.Weight && item.Exercise1.IsExercise1Try1Ok)
                    {
                        item.Exercise1.IsExercise1Try1Record = true;
                    }
                    else if (item.Exercise1.Exercise1Try2Weight == currentRecord.Weight && item.Exercise1.IsExercise1Try2Ok)
                    {
                        item.Exercise1.IsExercise1Try2Record = true;
                    }
                    else if (item.Exercise1.Exercise1Try3Weight == currentRecord.Weight && item.Exercise1.IsExercise1Try3Ok)
                    {
                        item.Exercise1.IsExercise1Try3Record = true;
                    }
                }
                if (item.Exercise2.Entry.Exercise.GlobalId == currentRecord.StrengthTrainingItem.Exercise.GlobalId)
                {
                    if (item.Exercise2.Exercise1Try1Weight == currentRecord.Weight && item.Exercise2.IsExercise1Try1Ok)
                    {
                        item.Exercise2.IsExercise1Try1Record = true;
                    }
                    else if (item.Exercise2.Exercise1Try2Weight == currentRecord.Weight && item.Exercise2.IsExercise1Try2Ok)
                    {
                        item.Exercise2.IsExercise1Try2Record = true;
                    }
                    else if (item.Exercise2.Exercise1Try3Weight == currentRecord.Weight && item.Exercise2.IsExercise1Try3Ok)
                    {
                        item.Exercise2.IsExercise1Try3Record = true;
                    }
                }
                if (item.Exercise3.Entry.Exercise.GlobalId == currentRecord.StrengthTrainingItem.Exercise.GlobalId)
                {
                    if (item.Exercise3.Exercise1Try1Weight == currentRecord.Weight && item.Exercise3.IsExercise1Try1Ok)
                    {
                        item.Exercise3.IsExercise1Try1Record = true;
                    }
                    else if (item.Exercise3.Exercise1Try2Weight == currentRecord.Weight && item.Exercise3.IsExercise1Try2Ok)
                    {
                        item.Exercise3.IsExercise1Try2Record = true;
                    }
                    else if (item.Exercise3.Exercise1Try3Weight == currentRecord.Weight && item.Exercise3.IsExercise1Try3Ok)
                    {
                        item.Exercise3.IsExercise1Try3Record = true;
                    }
                }
            }
        }