Exemplo n.º 1
0
        public async Task <ArenaPostResult> ExecutePost(HttpRequestMessage request)
        {
            String          exceptionResponse = String.Empty;
            ArenaPostResult postResult        = new ArenaPostResult();

            HttpResponseMessage response = await ArenaAPI.Client.PostAsync(request.RequestUri, request.Content);

            if (response.IsSuccessStatusCode)
            {
                var result = response.Content.ReadAsStringAsync().Result;
                result = result.Replace("True", "true").Replace("False", "false");
                XmlSerializer xmls = new XmlSerializer(typeof(ArenaPostResult));

                //attempt to desearlize result
                try
                {
                    return((ArenaPostResult)xmls.Deserialize(new StringReader(result)));
                }
                catch (Exception exception)
                {
                    exceptionResponse        = exception.Message;
                    postResult.WasSuccessful = true;
                }
            }
            else
            {
                postResult.WasSuccessful = false;
            }

            postResult.Action       = Action;
            postResult.ErrorMessage = String.Format("Response Status Code: {0}, Reason: {1}, Exception: {2}", response.StatusCode.ToString(), response.ReasonPhrase, exceptionResponse);

            return(postResult);
        }
        public async Task <ActionResult> New(NewFamilyModel NewFamilyModel)
        {
            //we need to validate the data server side
            if (NewFamilyModel.NewFamilyMembers.Any(x => !x.AdultIsValid))
            {
                ModelState.AddModelError("", "Every family member requires a valid first name, last name, email and phone number.");
            }

            //ensure at least 1 person is being added
            if (NewFamilyModel.NewFamilyMembers.Count == 0)
            {
                ModelState.AddModelError("", "At least 1 family member is required to register a family.");
            }

            //check the email addresses and phone numbers
            foreach (var member in NewFamilyModel.NewFamilyMembers)
            {
                Boolean isValid = true;
                if (!ValidationHelper.IsValidEmail(member.Email))
                {
                    ModelState.AddModelError("", String.Format("{0} is not a valid email address", member.Email)); isValid = false;
                }
                if (!ValidationHelper.IsValidPhone(ValidationHelper.CleanPhoneNumber(member.PhoneNumber)))
                {
                    ModelState.AddModelError("", String.Format("{0} is not a valid phone number", member.PhoneNumber)); isValid = false;
                }

                if (isValid)
                {
                    //lets just verify this email doesn't already exist in the system, if it does, we should redirect
                    List <Person> personsFound = await ArenaAPIHelper.GetPersons(new PersonListOptions { Email = member.Email });

                    if (personsFound.Count > 0)
                    {
                        return(RedirectToAction("Find", new { email = member.Email }));
                    }
                }
            }



            if (!ModelState.IsValid)
            {
                NewFamilyViewModel viewModel = new NewFamilyViewModel();
                viewModel.Campuses       = GetCampuses();
                viewModel.States         = GetStates();
                viewModel.NewFamilyModel = NewFamilyModel;

                return(View(viewModel));
            }

            //add the first person
            Person requiredAdult = MemberHelper.GetAdultPersonFromMember(NewFamilyModel.NewFamilyMembers.First(), (Campus)NewFamilyModel.CampusId);

            requiredAdult.Addresses = new List <Address> {
                NewFamilyModel.Address
            };

            ArenaPostResult result = await ArenaAPIHelper.AddPerson(requiredAdult);

            if (result.WasSuccessful)
            {
                //we need the familyId
                requiredAdult = await ArenaAPIHelper.GetPerson(result.ObjectId);

                for (int i = 1; i < NewFamilyModel.NewFamilyMembers.Count; i++)
                {
                    Person additonalAdult = MemberHelper.GetAdultPersonFromMember(NewFamilyModel.NewFamilyMembers[i], (Campus)NewFamilyModel.CampusId);

                    additonalAdult.FamilyId  = requiredAdult.FamilyId;
                    additonalAdult.Addresses = requiredAdult.Addresses;

                    result = await ArenaAPIHelper.AddPerson(additonalAdult);
                }

                return(RedirectToAction("AddChildren", new { id = requiredAdult.PersonId }));
            }

            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> AddChildren(AddChildrenModel AddChildrenModel)
        {
            RegistrationCompleteViewModel viewModel = new RegistrationCompleteViewModel();

            viewModel.ChildrenAdded = AddChildrenModel.Children;
            viewModel.Adult         = AddChildrenModel.Adult;

            if (AddChildrenModel.Children == null || AddChildrenModel.Children.Count == 0)
            {
                //family opted to not add any children
                return(View("RegistrationComplete", viewModel));
            }

            //we need to validate the data server side
            if (AddChildrenModel.Children.Any(x => !x.ChildIsValid))
            {
                ModelState.AddModelError("", "Every child family member requires a valid first name, last name, birthdate and gender.");
            }

            if (!ModelState.IsValid)
            {
                AddChildrenViewModel addChildviewModel = new AddChildrenViewModel();
                addChildviewModel.AddChildrenModel.Adult = AddChildrenModel.Adult;
                addChildviewModel.AddChildrenModel       = AddChildrenModel;
                return(View(viewModel));
            }

            Person adult = await ArenaAPIHelper.GetPerson(AddChildrenModel.Adult.PersonId);

            viewModel.Adult = adult;

            //everything looks good, lets add the kids to the family
            foreach (var child in AddChildrenModel.Children)
            {
                Person familyChild = MemberHelper.GetChildPersonFromMember(child, (Campus)adult.CampusId);
                familyChild.Addresses  = adult.Addresses;
                familyChild.FamilyId   = adult.FamilyId;
                familyChild.FamilyName = adult.FamilyName;
                familyChild.Phones     = adult.Phones;
                ArenaPostResult result = await ArenaAPIHelper.AddPerson(familyChild);

                if (!result.WasSuccessful)
                {
                    //stop processing and report error
                    return(View("Error", new HandleErrorInfo(new Exception("API request to add child failed."), "Home", "AddChildren")));
                }

                int newPersonId = result.ObjectId;

                //add to group
                result = await ArenaAPIHelper.AddPersonToGroup(newPersonId, ((Campus)adult.CampusId == Campus.Brownsboro)?(int)VisitorGroups.BrownsboroVisitors : (int)VisitorGroups.CliftonVisitors);

                if (!result.WasSuccessful)
                {
                    //stop processing and report error
                    return(View("Error", new HandleErrorInfo(new Exception("API request to add child to group failed."), "Home", "AddChildren")));
                }

                //add grade note
                result = await ArenaAPIHelper.AddPersonNote(newPersonId, child.Grade);

                if (!result.WasSuccessful)
                {
                    //stop processing and report error
                    return(View("Error", new HandleErrorInfo(new Exception("API request to add grade to child failed."), "Home", "AddChildren")));
                }
            }

            return(View("RegistrationComplete", viewModel));
        }