public HttpResponseMessage PostGroup(APISignedRequest request)
        {
            CompetitionModel comp;
            var group = GetRequestParameter <Comp_AgeGroupApiModel>(request, out comp);
            Comp_AgeGroupModel groupComp = SaveOneAgeGroup(comp, group);

            db.SaveChanges();

            var response = Request.CreateResponse <Comp_AgeGroupApiModel>(HttpStatusCode.Created, groupComp.ToApi());

            return(response);
        }
        private Comp_AgeGroupModel SaveOneAgeGroup(CompetitionModel comp, Comp_AgeGroupApiModel group)
        {
            var compYear = comp.Start.Year;
            var minAge   = compYear - group.YearYoung;
            var maxAge   = compYear - group.YearOld;

            bool noMaxLimit = (maxAge > 80);
            bool noMinLimit = (minAge < 3);

            Gender gender = group.Female ? Gender.Female : Gender.Male;
            IEnumerable <AgeGroupModel> exGroupList = db.AgeGroups.Where(g => g.GenderCode == (int)gender);

            if (noMaxLimit)
            {
                exGroupList = exGroupList.Where(g => (g.MaxAge ?? 100) > 80);
            }
            else if (noMinLimit)
            {
                exGroupList = exGroupList.Where(g => (g.MinAge ?? 0) < 4);
            }
            else
            {
                exGroupList = exGroupList.Where(g => g.MaxAge == maxAge && g.MinAge == minAge);
            }

            AgeGroupModel exGroup = exGroupList.FirstOrDefault();

            if (exGroup == null)
            {
                exGroup = new AgeGroupModel
                {
                    FullName          = group.Name,
                    SecretaryName     = group.Name,
                    MaxAge            = noMaxLimit ? null : new int?(maxAge),
                    MinAge            = (noMaxLimit || noMinLimit) ? null : new int?(minAge),
                    GenderProperty    = gender,
                    CompetitionGroups = new List <Comp_AgeGroupModel>()
                };
                db.AgeGroups.Add(exGroup);
            }
            Comp_AgeGroupModel groupComp = exGroup.CompetitionGroups.FirstOrDefault(cg => cg.CompetitionId == comp.Iid);

            if (groupComp == null)
            {
                groupComp = new Comp_AgeGroupModel {
                    CompetitionId = comp.Iid
                };
                exGroup.CompetitionGroups.Add(groupComp);
            }
            return(groupComp);
        }
예제 #3
0
        public static Comp_AgeGroupApiModel ToApi(this Comp_AgeGroupModel model)
        {
            Comp_AgeGroupApiModel res = new Comp_AgeGroupApiModel();

            res.Iid = model.AgeGroupId;
            int compYear = model.Competition.Start.Year;

            res.YearOld = compYear - (model.AgeGroup.MaxAge ?? int.MaxValue);
            if (res.YearOld < 0)
            {
                res.YearOld = 0;
            }
            res.YearYoung = compYear - (model.AgeGroup.MinAge ?? 0);
            if (res.YearYoung > compYear)
            {
                res.YearYoung = compYear;
            }
            res.Female = (model.AgeGroup.GenderProperty == Gender.Female);
            res.Name   = model.AgeGroup.SecretaryName;
            return(res);
        }