public ActionResult EditUserFamilyGroup(FamilyGroupViewModel model)
        {
            if (model.GroupId == 0)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            //var group = context.FamilyGroups.Find(model.GroupId);
            var group = famGroupRP.Find(model.GroupId);

            if (group == null)
            {
                return(HttpNotFound());
            }

            group.GroupName   = model.GroupName;
            group.PhoneNumber = model.PhoneNumber;

            //context.SaveChanges();
            famGroupRP.Update(group);

            return(View("FamilyGroupDisplay", group));
        }
        public ActionResult CreateFamilyGroup(FamilyGroupViewModel model)
        {
            if (ModelState.IsValid)
            {
                //var store = new UserStore<ApplicationUser>(context);
                //var manager = new UserManager<ApplicationUser>(store);

                var manager = famGroupRP.CreateUserStore();

                ApplicationUser child2 = null;
                ApplicationUser child3 = null;
                ApplicationUser child4 = null;
                ApplicationUser child5 = null;

                List <ApplicationUser> swimmerList = new List <ApplicationUser>();
                if (model.ParentEmail == null || model.Child1Email == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                var parent = manager.FindByEmail(model.ParentEmail);

                if (parent == null)
                {
                    return(HttpNotFound());
                }

                var child1 = manager.FindByEmail(model.Child1Email);

                if (child1 == null)
                {
                    return(HttpNotFound());
                }

                FamilyGroup group = new FamilyGroup
                {
                    GroupName   = model.GroupName,
                    Email       = model.ParentEmail,
                    PhoneNumber = model.PhoneNumber,
                    Parent      = parent,
                    Swimmers    = swimmerList
                };

                group.Swimmers.Add(child1);

                if (model.Child2Email != null)
                {
                    child2 = manager.FindByEmail(model.Child2Email);

                    if (child2 != null)
                    {
                        group.Swimmers.Add(child2);
                    }
                }

                if (model.Child3Email != null)
                {
                    child3 = manager.FindByEmail(model.Child3Email);

                    if (child3 != null)
                    {
                        group.Swimmers.Add(child3);
                    }
                }

                if (model.Child4Email != null)
                {
                    child4 = manager.FindByEmail(model.Child4Email);

                    if (child4 != null)
                    {
                        group.Swimmers.Add(child4);
                    }
                }

                if (model.Child5Email != null)
                {
                    child5 = manager.FindByEmail(model.Child5Email);

                    if (child5 != null)
                    {
                        group.Swimmers.Add(child5);
                    }
                }

                //context.FamilyGroups.Add(group);
                famGroupRP.Add(group);

                parent.FamilyGroupId = group.GroupId;
                manager.Update(parent);

                child1.FamilyGroupId = group.GroupId;
                manager.Update(child1);

                if (child2 != null)
                {
                    child2.FamilyGroupId = group.GroupId;
                    manager.Update(child2);
                }

                if (child3 != null)
                {
                    child3.FamilyGroupId = group.GroupId;
                    manager.Update(child3);
                }

                if (child4 != null)
                {
                    child2.FamilyGroupId = group.GroupId;
                    manager.Update(child4);
                }

                if (child5 != null)
                {
                    child2.FamilyGroupId = group.GroupId;
                    manager.Update(child5);
                }

                return(View("FamilyGroupDisplay", group));
            }
            return(View("CreateFamilyGroup"));
        }
        public IHttpActionResult PostFamilyGroup(FamilyGroupViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            List <ApplicationUser> swimmerList = new List <ApplicationUser>();
            ApplicationUser        child1      = null;
            ApplicationUser        child2      = null;
            ApplicationUser        child3      = null;
            ApplicationUser        child4      = null;
            ApplicationUser        child5      = null;

            var manager = famGroupRP.CreateUserStore();

            var parent = manager.FindByEmail(model.ParentEmail);

            if (parent == null)
            {
                return(NotFound());
            }

            child1 = manager.FindByEmail(model.Child1Email);

            if (child1 == null)
            {
                return(NotFound());
            }
            else
            {
                swimmerList.Add(child1);
            }

            child2 = IsChildValid(manager, model.Child2Email);
            if (child2 != null)
            {
                swimmerList.Add(child2);
            }

            child3 = IsChildValid(manager, model.Child3Email);
            if (child3 != null)
            {
                swimmerList.Add(child3);
            }

            child4 = IsChildValid(manager, model.Child4Email);
            if (child4 != null)
            {
                swimmerList.Add(child4);
            }

            child5 = IsChildValid(manager, model.Child5Email);
            if (child5 != null)
            {
                swimmerList.Add(child5);
            }

            FamilyGroup group = new FamilyGroup
            {
                GroupName   = model.GroupName,
                Email       = model.ParentEmail,
                PhoneNumber = model.PhoneNumber,
                Parent      = parent,
                Swimmers    = swimmerList
            };

            famGroupRP.Add(group);

            group.Parent.FamilyGroupId = group.GroupId;
            manager.Update(group.Parent);

            foreach (var child in group.Swimmers)
            {
                child.FamilyGroupId = group.GroupId;
                manager.Update(child);
            }

            return(CreatedAtRoute("DefaultApi", new { id = group.GroupId }, group));
        }