public virtual ActionResult RelationshipsEdit(UserEditRelationshipsViewModel model) { if (ModelState.IsValid) { FamilyRepository repos = new FamilyRepository(); // save the new relationship //Relationship relationship = new Relationship(); //relationship.SourceUser = Target; //relationship.RelatedUser = db.Users.Find(model.NewRelatedUserId); //relationship.RelatedUserIsSourceUsers = model.NewRelationshipType; //repos.UpdateRelationship(db, relationship); foreach (var item in model.FamilyRelationshipsNew) { Relationship relationship = new Relationship(); relationship.SourceUser = Target; relationship.RelatedUser = db.Users.Find(item.RelatedUserId); if (item.Relationship == UserEditRelationshipsViewModel.RelationshipTypeViewModel.NotImmediateFamilyMember) { repos.RemoveRelationship(db, relationship); } else { relationship.RelatedUserIsSourceUsers = ((RelationshipType)item.Relationship).Reciprocal(); repos.UpdateRelationship(db, relationship); } } return(RedirectToAction(MVC.User.Details().WithId(Target))); } return(View(model)); }
public virtual ActionResult AddFamilyMember(AddFamilyMemberViewModel model) { if (ModelState.IsValid) { FamilyRepository repos = new FamilyRepository(); User newUser = repos.AddFamilyMember(db, Target, model.Name, model.Email, model.IsKid); if (model.IsKid) { // add assumed relationship to admin Relationship relationshipToAdmin = new Relationship(); relationshipToAdmin.SourceUser = Target.Admin; relationshipToAdmin.RelatedUser = newUser; relationshipToAdmin.RelatedUserIsSourceUsers = RelationshipType.Child; repos.UpdateRelationship(db, relationshipToAdmin); // add assumed relationships to other kids foreach (User member in Target.Members) { if (member.Id == newUser.Id) { continue; } if (member.Id == Target.Admin.Id) { continue; } if (!member.IsKid) { continue; } Relationship relationshipToOtherKid = new Relationship(); relationshipToOtherKid.SourceUser = newUser; relationshipToOtherKid.RelatedUser = member; relationshipToOtherKid.RelatedUserIsSourceUsers = RelationshipType.Sibling; repos.UpdateRelationship(db, relationshipToOtherKid); } } return(RedirectToAction(MVC.User.RelationshipsEdit().WithId(newUser))); } return(View(model)); }