Пример #1
0
        public static Comp_CompetitorRegistrationApiModel ToApi(this Comp_CompetitiorRegistrationModel model, bool multipleTeams = false)
        {
            var resSimple = ToApiSimple(model);

            if (!multipleTeams)
            {
                return(resSimple);
            }
            Comp_MultipleTeamsClimber res = new Comp_MultipleTeamsClimber();
            var t = resSimple.GetType();

            foreach (var pInfo in t.GetProperties().Where(p => p.CanRead && p.CanWrite))
            {
                var value = pInfo.GetValue(resSimple, null);
                pInfo.SetValue(res, value, null);
            }
            res.Teams = model.Teams.OrderBy(l => l.RegionOrder).Select(l => l.RegionId).ToArray();
            return(res);
        }
Пример #2
0
        private static Comp_CompetitorRegistrationApiModel ToApiSimple(Comp_CompetitiorRegistrationModel model)
        {
            Comp_CompetitorRegistrationApiModel res = new Comp_CompetitorRegistrationApiModel();

            res.License = model.PersonId;
            res.Surname = model.Person.Surname;
            res.Name    = model.Person.Name;
            var team = model.Teams.OrderBy(t => t.RegionOrder).FirstOrDefault();

            res.TeamID         = (team == null) ? 0 : team.RegionId;
            res.GroupID        = model.CompAgeGroup.AgeGroupId;
            res.YearOfBirth    = model.Person.DateOfBirth.Year;
            res.Female         = model.Person.GenderFemale;
            res.Razr           = model.Qf.GetFriendlyValue();
            res.Bib            = model.SecretaryId;
            res.Lead           = (ApplicationType)model.Lead;
            res.Speed          = (ApplicationType)model.Speed;
            res.Boulder        = (ApplicationType)model.Boulder;
            res.RankingLead    = model.RankingLead;
            res.RankingSpeed   = model.RankingiSpeed;
            res.RankingBoulder = model.RankingBoulder;
            return(res);
        }
        private Comp_CompetitiorRegistrationModel SaveOneClimberSynchronized(Comp_CompetitorRegistrationApiModel climber, CompetitionModel comp)
        {
            var group = comp.AgeGroups.FirstOrDefault(g => g.AgeGroupId == climber.GroupID);

            if (group == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            String surname = climber.Surname.GetUnifiedName();
            String name    = climber.Name.GetUnifiedName();

            if (climber.YearOfBirth < 20)
            {
                climber.YearOfBirth += 2000;
            }
            else if (climber.YearOfBirth < 100)
            {
                climber.YearOfBirth += 1900;
            }
            PersonModel person;

            if (climber.License > 0)
            {
                person = db.People.Find(climber.License);
                if (person != null && !(
                        person.Surname.Equals(surname, StringComparison.OrdinalIgnoreCase) &&
                        person.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
                        person.YearOfBirth == climber.YearOfBirth &&
                        person.GenderFemale == climber.Female
                        )
                    )
                {
                    person.Surname = surname;
                    person.Name    = name;
                    if (person.YearOfBirth != climber.YearOfBirth)
                    {
                        person.DateOfBirth = new DateTime(climber.YearOfBirth, person.DateOfBirth.Month, person.DateOfBirth.Day);
                    }
                    person.GenderFemale = climber.Female;
                }
            }
            else
            {
                person = db.People.FirstOrDefault(p => p.Surname.Equals(surname, StringComparison.OrdinalIgnoreCase) &&
                                                  p.Name.Equals(name, StringComparison.OrdinalIgnoreCase) &&
                                                  p.GenderFemale == climber.Female &&
                                                  p.DateOfBirth.Year == climber.YearOfBirth);
            }
            if (person == null)
            {
                person = new PersonModel
                {
                    Surname      = surname,
                    Name         = name,
                    GenderFemale = climber.Female,
                    DateOfBirth  = new DateTime(climber.YearOfBirth, 1, 1),
                    Patronymic   = String.Empty,
                    Coach        = String.Empty,
                    HomeAddress  = String.Empty,
                    Email        = String.Empty,
                    Competitions = new List <Comp_CompetitiorRegistrationModel>()
                };
                db.People.Add(person);
            }
            var compReg = person.Competitions.FirstOrDefault(r => r.CompId == comp.Iid);

            if (compReg == null)
            {
                compReg = new Comp_CompetitiorRegistrationModel
                {
                    CompId  = comp.Iid,
                    Person  = person,
                    GroupId = group.Iid,
                    Teams   = new List <Comp_ClimberTeam>()
                };
                person.Competitions.Add(compReg);
            }
            compReg.Lead           = (ApplicationDisplayEnum)climber.Lead;
            compReg.Speed          = (ApplicationDisplayEnum)climber.Speed;
            compReg.Boulder        = (ApplicationDisplayEnum)climber.Boulder;
            compReg.Qf             = climber.Razr.GetEnumValue <Razryad>();
            compReg.RankingLead    = climber.RankingLead;
            compReg.RankingiSpeed  = climber.RankingSpeed;
            compReg.RankingBoulder = climber.RankingBoulder;
            compReg.SecretaryId    = climber.Bib;

            List <long> teamList  = new List <long>();
            var         mtClimber = climber as Comp_MultipleTeamsClimber;

            if (mtClimber == null)
            {
                teamList.Add(climber.TeamID);
            }
            else
            {
                teamList.AddRange(mtClimber.Teams);
            }
            int i = 0;

            foreach (var teamId in teamList)
            {
                if (db.Regions.Count(r => r.Iid == teamId) < 1)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
                var clmteam = compReg.Teams.FirstOrDefault(t => t.RegionId == teamId);
                if (clmteam == null)
                {
                    compReg.Teams.Add(new Comp_ClimberTeam {
                        RegionId = teamId, RegionOrder = (++i)
                    });
                }
                else
                {
                    clmteam.RegionOrder = (++i);
                }
            }
            var delList = compReg.Teams.ToList().Where(t => !teamList.Contains(t.RegionId)).ToArray();

            for (i = 0; i < delList.Length; i++)
            {
                db.CompetitionClimberTeams.Remove(delList[i]);
            }
            return(compReg);
        }
        private String[] SaveClimberApp(UserProfileModel user, long compId, ClimberApplication model, bool saveChanges)
        {
            if (model == null)
            {
                return new String[] { "Модель не загружена" }
            }
            ;
            var comp = db.Competitions.Find(compId);

            if (comp == null)
            {
                return new String[] { "Неверные соревнования" }
            }
            ;
            String[]         empty = new String[0];
            PersonModel      personToApply;
            Comp_ClimberTeam existingApp;
            var retVal = model.Validate(user, db, comp, out personToApply, out existingApp);

            if (retVal.Length > 0)
            {
                return(retVal);
            }

            //если надо улалить заявку или заменить участника
            if (model.IsDel || (existingApp != null && (personToApply == null || personToApply.Iid != existingApp.Climber.PersonId)))
            {
                if (model.IsDel && existingApp == null)
                {
                    return(empty);
                }

                //удаляем текущую заявку
                RemoveApp(existingApp);
                if (model.IsDel)
                {
                    //если надо только удалить  то выйдем из функции
                    if (saveChanges)
                    {
                        db.SaveChanges();
                    }
                    return(empty);
                }
                existingApp = null;
            }

            //создадим заявку
            var climber = existingApp != null ? existingApp.Climber : null;

            if (climber == null)
            {
                //добавим человека
                if (personToApply == null)
                {
                    var newIid = GenerateLicenseId(model.TeamId.Value);
                    personToApply = new PersonModel
                    {
                        Surname        = model.Surname,
                        Name           = model.Name,
                        GenderProperty = model.GenderP.Value,
                        DateOfBirth    = new DateTime(model.YearOfBirth.Value, 01, 05),
                        Competitions   = new List <Comp_CompetitiorRegistrationModel>(),
                        Coach          = String.Empty,
                        Email          = String.Empty,
                        HomeAddress    = String.Empty,
                        Patronymic     = String.Empty
                    };
                    if (newIid != null)
                    {
                        personToApply.Iid = newIid.Value;
                    }
                    db.People.Add(personToApply);
                }
                //найдем его заявку на данные соревы (если заявки нет - то создадим)
                climber = personToApply.Competitions.FirstOrDefault(c => c.CompId == comp.Iid);
                if (climber == null)
                {
                    climber = new Comp_CompetitiorRegistrationModel
                    {
                        CompId = comp.Iid,
                        Teams  = new List <Comp_ClimberTeam>()
                    };
                    personToApply.Competitions.Add(climber);
                }
            }
            //обновим заявку
            climber.Boulder = model.Boulder;
            climber.GroupId = model.GroupId;
            climber.Lead    = model.Lead;
            climber.Speed   = model.Speed;
            climber.Qf      = model.Qf.Value;
            //добавим команды
            if (existingApp == null)
            {
                int pos = 1;
                for (; climber.Teams.Count(pt => pt.RegionOrder == pos) > 0; pos++)
                {
                    ;
                }
                climber.Teams.Add(new Comp_ClimberTeam {
                    RegionId = model.TeamId.Value, RegionOrder = pos
                });
            }
            if (saveChanges)
            {
                db.SaveChanges();
            }
            return(empty);
        }