예제 #1
0
        public IHttpActionResult UploadPhoto(dynamic json)
        {
            dynamic model    = JsonConvert.DeserializeObject <ExpandoObject>(json.ToString());
            int     memberId = Convert.ToInt32(model.memberId);

            if (!CanEditMember(memberId))
            {
                return(BadRequest("You do not have permission to edit this member"));
            }

            if (string.IsNullOrWhiteSpace(model.image))
            {
                return(BadRequest("No image content provided"));
            }

            byte[] imageBytes = Convert.FromBase64String(model.image.Replace("data:image/jpeg;base64,", ""));

            using (MemoryStream stream = new MemoryStream(imageBytes))
            {
                Image  img      = Image.FromStream(stream);
                string filePath = System.Web.Hosting.HostingEnvironment.MapPath(string.Format(@"~/Images/Profiles/{0}.jpg", memberId));
                img.Save(filePath);
            }

            using (bkContext context = new bkContext())
            {
                Member member = context.Members.FirstOrDefault(x => x.MemberID == memberId);
                member.ModifiedBy = LoggedInMemberId;
                member.ModifiedOn = DateTime.Now;

                context.SaveChanges();
            }

            return(Ok());
        }
예제 #2
0
        public IHttpActionResult ResetPassword(string password, string token)
        {
            using (bkContext context = new bkContext())
            {
                Guid resetToken = new Guid();
                if (!Guid.TryParse(token, out resetToken))
                {
                    return(BadRequest("Invalid Token, please regenerate your password reset request"));
                }

                Member member = context.Members.FirstOrDefault(m => m.PasswordUID == resetToken);

                if (member == null)
                {
                    return(BadRequest("Invalid Token, please regenerate your password reset request"));
                }

                member.PasswordUID = null;
                member.Password    = password;
                member.ModifiedOn  = DateTime.Now;
                member.ModifiedBy  = member.MemberID;

                context.SaveChanges();
            }

            return(Ok(true));
        }
예제 #3
0
        public IHttpActionResult Save(FamilyViewModel model)
        {
            if (!CanEditFamily(model.FamilyID))
            {
                return(BadRequest("You do not have permission to edit this family"));
            }

            using (bkContext context = new bkContext())
            {
                Family family = context.Families.Where(f => f.FamilyID == model.FamilyID).FirstOrDefault();
                if (family == null)
                {
                    return(BadRequest("Family record cannot be loaded. Please try again later"));
                }

                if (model.HeadOfFamilyID == 0)
                {
                    return(BadRequest("please provide Head Of Family"));
                }

                if (!family.FamilyMemberAssociations.Any(x => x.MemberId == model.HeadOfFamilyID))
                {
                    return(BadRequest("Supplied Head Of Family is not part of family"));
                }

                if (!family.FamilyMemberAssociations.Any(x => x.MemberId == model.HeadOfFamilyID && x.Approved))
                {
                    return(BadRequest("Head Of family is not approved member of family"));
                }

                if (context.Families.Any(x => x.FamilyID != model.FamilyID && x.HeadOfFamilyID == model.HeadOfFamilyID))
                {
                    return(BadRequest("Selected Head Of Family is already a Head Of Family for another family. Please select other member as head of family."));
                }

                family.FamilyNative   = model.FamilyNative;
                family.Address1       = model.Address1;
                family.Address2       = model.Address2;
                family.CategoryID     = model.CategoryID;
                family.City           = model.City;
                family.District       = model.District;
                family.Country        = model.Country;
                family.NukhID         = model.NukhID;
                family.PostalCode     = model.PostalCode;
                family.State          = model.State;
                family.HeadOfFamilyID = model.HeadOfFamilyID;
                family.ModifiedOn     = DateTime.Now;
                family.ModifiedBy     = LoggedInMemberId;

                context.SaveChanges();
            }

            return(Ok());
        }
예제 #4
0
        public IHttpActionResult SendResetPasswordEmail(string emailAddress)
        {
            using (bkContext context = new bkContext())
            {
                if (!context.Members.Any(m => m.EmailAddress == emailAddress))
                {
                    return(BadRequest("Email address is not registered"));
                }

                Member member = context.Members.FirstOrDefault(m => m.EmailAddress == emailAddress);
                if (member == null)
                {
                    return(BadRequest("Your account information cannot be loaded. Please contact Administrator for help"));
                }

                member.PasswordUID = Guid.NewGuid();
                context.SaveChanges();

                string templatePath = System.Web.Hosting.HostingEnvironment.MapPath("~/HtmlTemplates/password_reset.html");
                string html         = File.ReadAllText(templatePath);

                html = html.Replace("{{name}}", $"{member.FirstName} {member.LastName}");
                html = html.Replace("{{action_url}}", $"{BaseUrl}/resetpassword/{member.PasswordUID.Value.ToString()} ");

                System.Threading.Tasks.Task.Factory.StartNew(() =>
                {
                    using (SmtpClient sClient = new SmtpClient())
                    {
                        using (MailMessage mailMessage = new MailMessage("*****@*****.**", member.EmailAddress))
                        {
                            mailMessage.Body       = html;
                            mailMessage.IsBodyHtml = true;
                            mailMessage.Subject    = "Brahmkshatriya Online Portal - Password Reset";

                            sClient.Send(mailMessage);
                        }
                    }
                });
            }

            return(Ok(true));
        }
예제 #5
0
        public IHttpActionResult Delete(int memberId)
        {
            using (bkContext context = new bkContext())
            {
                if (!CanEditMember(memberId))
                {
                    return(BadRequest("You do not have permission to delete this record"));
                }

                Matrimonial mat = context.Matrimonials.FirstOrDefault(x => x.MemberID == memberId);
                if (mat == null)
                {
                    return(BadRequest("Matrimony profile cannot be loaded"));
                }

                context.Matrimonials.Remove(mat);
                context.SaveChanges();
            }

            return(Ok());
        }
예제 #6
0
        public IHttpActionResult Decline(int memberId, int familyId)
        {
            using (bkContext context = new bkContext())
            {
                if (!CanEditMember(memberId))
                {
                    return(BadRequest("You do not have rights to approve this member"));
                }

                FamilyMemberAssociation fmAssociation = context.FamilyMemberAssociations.FirstOrDefault(x => x.MemberId == memberId && x.FamilyId == familyId && !x.Approved);
                if (fmAssociation == null)
                {
                    return(BadRequest("No pending approval found"));
                }

                context.FamilyMemberAssociations.Remove(fmAssociation);

                context.SaveChanges();
            }

            return(Ok());
        }
예제 #7
0
        public IHttpActionResult DeletePhoto(int photoNumber, int memberId)
        {
            if (!CanEditMember(memberId))
            {
                return(BadRequest("You do not have permission to edit this member"));
            }

            if (photoNumber < 1 || photoNumber > 3)
            {
                return(BadRequest("Invalid photo number"));
            }

            string filePath = System.Web.Hosting.HostingEnvironment.MapPath(string.Format(@"~/Images/Matrimonials/{0}_{1}.jpg", memberId, photoNumber));

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            string response = string.Empty;

            using (bkContext context = new bkContext())
            {
                Matrimonial mat = context.Matrimonials.FirstOrDefault(x => x.MemberID == memberId);
                if (mat != null)
                {
                    mat.ModifiedBy = LoggedInMemberId;
                    mat.ModifiedOn = DateTime.Now;

                    context.SaveChanges();
                }

                response = MemberWrapper.MatrimonyPhoto(memberId, mat.Member.Gender, photoNumber, mat.ModifiedOn);
            }

            return(Ok(response));
        }
예제 #8
0
        public IHttpActionResult MarkDefaultFamily(int familyId, int memberId)
        {
            using (bkContext context = new bkContext())
            {
                if (!CanEditMember(familyId, memberId))
                {
                    return(BadRequest("You do not have permission to edit this member"));
                }

                List <FamilyMemberAssociation> fmAssociations = context.FamilyMemberAssociations.Where(m => m.MemberId == memberId).ToList();

                if (context.Families.Any(x => x.FamilyID != familyId && x.HeadOfFamilyID == memberId))
                {
                    return(BadRequest("This member is Head Of Family in another family and cannot be marked default here"));
                }

                foreach (var item in fmAssociations)
                {
                    if (item.FamilyId == familyId)
                    {
                        item.DefaultFamily = true;
                        item.ModifiedBy    = LoggedInMemberId;
                        item.ModifiedOn    = DateTime.Now;
                    }
                    else if (item.DefaultFamily)
                    {
                        item.DefaultFamily = false;
                        item.ModifiedBy    = LoggedInMemberId;
                        item.ModifiedOn    = DateTime.Now;
                    }
                }

                context.SaveChanges();
            }

            return(Ok());
        }
예제 #9
0
        public IHttpActionResult ChangePassword(ChangePasswordViewModel model)
        {
            using (bkContext context = new bkContext())
            {
                Member member = context.Members.Where(x => x.MemberID == LoggedInMemberId).FirstOrDefault();
                if (member == null)
                {
                    return(BadRequest("Your record cannot be loaded. Please try again or contact Administrator for help"));
                }

                if (member.Password != model.CurrentPassword)
                {
                    return(BadRequest("Your current password is invalid. Please try again"));
                }

                member.Password   = model.NewPassword;
                member.ModifiedBy = member.MemberID;
                member.ModifiedOn = DateTime.Now;

                context.SaveChanges();
            }

            return(Ok(true));
        }
예제 #10
0
        public IHttpActionResult Register(RegisterViewModel register)
        {
            if (!VerifyCaptcha(register.CaptchaResponse))
            {
                return(BadRequest("Please refresh page and try again"));
            }

            using (bkContext context = new bkContext())
            {
                if (context.Members.Any(f => f.EmailAddress == register.EmailAddress.Trim()))
                {
                    return(BadRequest("Email address already registered. Please use forgot password on login page to recover your account"));
                }

                if (context.Members.Any(f => f.Phone == register.PhoneNumber.Trim()))
                {
                    return(BadRequest("Phone number already registered. Please contact Administrator for help"));
                }

                Member member = new Member();
                member.FirstName       = register.FirstName;
                member.LastName        = register.LastName;
                member.DOB             = register.DateOfBirth;
                member.EmailAddress    = register.EmailAddress.Trim();
                member.Phone           = register.PhoneNumber;
                member.Gender          = register.Gender;
                member.MaritalStatusID = 2; //MARRIED

                string tPassword = System.Web.Security.Membership.GeneratePassword(8, 0);
                tPassword       = Regex.Replace(tPassword, @"[^a-zA-Z0-9]", m => "9");
                member.Password = tPassword;

                member.Alive     = true;
                member.Active    = true;
                member.CreatedOn = DateTime.Now;

                Family family = new Family();
                family.Address1   = register.Address1;
                family.Address2   = register.Address2;
                family.City       = register.City;
                family.District   = register.District;
                family.State      = register.State;
                family.PostalCode = register.PostalCode;
                family.Country    = register.Country;
                family.CategoryID = register.CategoryId;
                family.NukhID     = register.NukhId;
                family.Member     = member;
                family.CreatedOn  = DateTime.Now;

                FamilyMemberAssociation fmAssociation = new FamilyMemberAssociation();
                fmAssociation.Member        = member;
                fmAssociation.Family        = family;
                fmAssociation.Approved      = true;
                fmAssociation.DefaultFamily = true;
                fmAssociation.CreatedOn     = DateTime.Now;

                context.Families.Add(family);
                context.Members.Add(member);
                context.FamilyMemberAssociations.Add(fmAssociation);

                context.SaveChanges();

                string templatePath = System.Web.Hosting.HostingEnvironment.MapPath("~/HtmlTemplates/welcome.html");
                string html         = File.ReadAllText(templatePath);

                html = html.Replace("{{name}}", $"{member.FirstName} {member.LastName}");
                html = html.Replace("{{action_url}}", $"{BaseUrl}/login/ ");
                html = html.Replace("{{username}}", member.EmailAddress);
                html = html.Replace("{{password}}", member.Password);

                System.Threading.Tasks.Task.Factory.StartNew(() =>
                {
                    using (SmtpClient sClient = new SmtpClient())
                    {
                        using (MailMessage mailMessage = new MailMessage("*****@*****.**", member.EmailAddress))
                        {
                            mailMessage.Body       = html;
                            mailMessage.IsBodyHtml = true;
                            mailMessage.Subject    = "Brahmkshatriya Online Portal - Welcome Letter";

                            sClient.Send(mailMessage);
                        }
                    }
                });
            }

            return(Ok());
        }
예제 #11
0
        public IHttpActionResult Save(MatrimonyViewModel model)
        {
            using (bkContext context = new bkContext())
            {
                if (!CanEditMember(model.MemberId))
                {
                    return(BadRequest("You do not have permission to update this record"));
                }

                Matrimonial mat    = context.Matrimonials.FirstOrDefault(x => x.MemberID == model.MemberId);
                Member      member = context.Members.FirstOrDefault(x => x.MemberID == model.MemberId);

                if (member == null)
                {
                    return(BadRequest("Member record cannot be loaded. Please try again later"));
                }

                if (!member.Alive)
                {
                    return(BadRequest("You cannot create a matrimony profile unless a member is alive"));
                }

                if (member.MaritalStatusID == 2)
                {
                    return(BadRequest("You cannot create a matrimony profile because person's marital status is set to Married"));
                }

                if (!member.DOB.HasValue)
                {
                    return(BadRequest("You cannot create a matrimony profile because person's Date Of Birth is missing"));
                }

                if (member.Gender && MemberWrapper.Age(member.DOB.Value) < 21)
                {
                    return(BadRequest("You cannot create a matrimony profile because person's age is less than 21"));
                }

                if (!member.Gender && MemberWrapper.Age(member.DOB.Value) < 18)
                {
                    return(BadRequest("You cannot create a matrimony profile because person's age is less than 18"));
                }

                if (mat != null)
                {
                    mat.ModifiedBy = LoggedInMemberId;
                    mat.ModifiedOn = DateTime.Now;
                }
                else
                {
                    mat           = new Matrimonial();
                    mat.CreatedBy = LoggedInMemberId;
                    mat.CreatedOn = DateTime.Now;
                    mat.MemberID  = model.MemberId;
                    context.Matrimonials.Add(mat);
                }

                mat.Alcohol          = model.Alcohol;
                mat.BirthTime        = model.BirthTime;
                mat.BodyTypeID       = model.BodyTypeId;
                mat.ComplexionTypeID = model.ComplexionTypeId;
                mat.Disability       = model.Disability;
                mat.Height           = model.Height;
                mat.Language         = model.Language;
                mat.Mangal           = model.Mangal;
                mat.MaritalStatusID  = model.MaritalStatusId;
                mat.MaternalNukhID   = model.MaternalNukhId;
                mat.MonthlyIncome    = model.MonthlyIncome;
                mat.OwnHome          = model.OwnHome;
                mat.ProfileText      = model.ProfileText;
                mat.Smoke            = model.Smoke;
                mat.Tobacco          = model.Tobacco;
                mat.Vegetarian       = model.Vegetarian;
                mat.Weight           = model.Weight;

                context.SaveChanges();
            }

            return(Ok());
        }
예제 #12
0
        public IHttpActionResult AddToFamily(dynamic json)
        {
            dynamic model = JsonConvert.DeserializeObject <ExpandoObject>(json.ToString());

            int    familyId       = Convert.ToInt32(model.familyId);
            int    memberId       = Convert.ToInt32(model.memberId);
            string relationType   = (string)model.relationType;
            int?   relatedId      = (int?)model.relatedId;
            int?   relationTypeId = (int?)model.relationTypeId;


            if (!CanEditFamily(familyId))
            {
                return(BadRequest("You do not have permission to edit this family"));
            }

            using (bkContext context = new bkContext())
            {
                Member member = context.Members.Include(x => x.FamilyMemberAssociations).FirstOrDefault(x => x.MemberID == memberId);
                if (member == null)
                {
                    return(BadRequest("Member cannot be located. Please try again later"));
                }

                Member relatedMember = null;
                if (relatedId.HasValue)
                {
                    relatedMember = context.Members.Include(x => x.FamilyMemberAssociations).FirstOrDefault(x => x.MemberID == relatedId.Value);
                    if (relatedMember == null)
                    {
                        return(BadRequest("Related member cannot be located. Please try again later"));
                    }

                    if (!relatedMember.FamilyMemberAssociations.Any(x => x.FamilyId == familyId))
                    {
                        return(BadRequest("Related member is not part of the family"));
                    }
                }

                if (member.FamilyMemberAssociations.Any(x => x.FamilyId == familyId))
                {
                    return(BadRequest("Member is already a part of selected family"));
                }

                bool autoApproval = CanEditMember(memberId);

                FamilyMemberAssociation fmAssociation = new FamilyMemberAssociation();
                fmAssociation.Approved       = autoApproval;
                fmAssociation.CreatedBy      = LoggedInMemberId;
                fmAssociation.CreatedOn      = DateTime.Now;
                fmAssociation.FamilyId       = familyId;
                fmAssociation.MemberId       = memberId;
                fmAssociation.RelatedId      = relatedId;
                fmAssociation.RelationTypeId = relationTypeId;

                context.FamilyMemberAssociations.Add(fmAssociation);
                context.SaveChanges();

                if (!string.IsNullOrWhiteSpace(member.EmailAddress) && !autoApproval)
                {
                    string templatePath = System.Web.Hosting.HostingEnvironment.MapPath("~/HtmlTemplates/familyAddition.html");
                    string html         = File.ReadAllText(templatePath);

                    html = html.Replace("{{name}}", $"{member.FirstName} {member.LastName}");
                    html = html.Replace("{{action_url}}", $"{BaseUrl}/login/ ");
                    html = html.Replace("{{username}}", member.EmailAddress);
                    html = html.Replace("{{password}}", member.Password);
                    html = html.Replace("{{addedBy}}", LoggedInMemberName);
                    html = html.Replace("{{addedOn}}", fmAssociation.CreatedOn.Value.ToString("dddd, dd MMMM yyyy hh:mm tt"));

                    if (relatedMember != null)
                    {
                        html = html.Replace("{{relation}}", $"{relationType} {relatedMember.FirstName} {relatedMember.LastName}");
                    }
                    else
                    {
                        html = html.Replace("{{relation}}", "Unknown relationship");
                    }

                    System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        using (SmtpClient sClient = new SmtpClient())
                        {
                            using (MailMessage mailMessage = new MailMessage("*****@*****.**", member.EmailAddress))
                            {
                                mailMessage.Body       = html;
                                mailMessage.IsBodyHtml = true;
                                mailMessage.Subject    = "Brahmkshatriya Online Portal - Notification";

                                sClient.Send(mailMessage);
                            }
                        }
                    });
                }
            }

            return(Ok());
        }
예제 #13
0
        public IHttpActionResult Save(MemberViewModel model)
        {
            if (!model.MemberID.HasValue)
            {
                if (!CanEditFamily(model.FamilyId.Value))
                {
                    return(BadRequest("You do not have permission to edit this family"));
                }
            }

            if (model.MemberID.HasValue)
            {
                if (!CanEditMember(model.FamilyId.Value, model.MemberID.Value))
                {
                    return(BadRequest("You do not have permission to edit this member"));
                }
            }

            bool sendWelcomeLetter = false;

            using (bkContext context = new bkContext())
            {
                Member member = null;

                if (model.MemberID.HasValue)
                {
                    member = context.Members.Where(x => x.MemberID == model.MemberID).FirstOrDefault();
                    if (member == null)
                    {
                        return(BadRequest("Member record cannot be loaded. Please try again or contact Administrator for help"));
                    }

                    //if member record has email address and login was done no change in email address allowed
                    if (!string.IsNullOrWhiteSpace(member.EmailAddress) && member.EmailAddress != model.Email && member.LastLoginOn.HasValue)
                    {
                        return(BadRequest("You cannot change email address. Please contact Administrator for help"));
                    }

                    member.ModifiedBy = LoggedInMemberId;
                    member.ModifiedOn = DateTime.Now;

                    //if email was not available and later on provided
                    sendWelcomeLetter = string.IsNullOrWhiteSpace(member.EmailAddress) && !string.IsNullOrWhiteSpace(model.Email);

                    if (!sendWelcomeLetter) //email changed and no earlier sign in attempt was made
                    {
                        sendWelcomeLetter = !string.IsNullOrWhiteSpace(model.Email) && member.EmailAddress != model.Email && !member.LastLoginOn.HasValue;
                    }
                }
                else
                {
                    member = new Member();

                    string tPassword = System.Web.Security.Membership.GeneratePassword(8, 0);
                    tPassword       = Regex.Replace(tPassword, @"[^a-zA-Z0-9]", m => "9");
                    member.Password = tPassword;

                    member.CreatedOn = DateTime.Now;
                    member.CreatedBy = LoggedInMemberId;
                    context.Members.Add(member);

                    sendWelcomeLetter = !string.IsNullOrWhiteSpace(model.Email);
                }

                member.Alive           = model.Alive;
                member.BirthPlace      = model.BirthPlace;
                member.CompanyName     = model.CompanyName;
                member.DeathPlace      = model.DeathPlace;
                member.DOB             = model.DOB;
                member.DOD             = model.DOD;
                member.EducationField  = model.EducationField;
                member.EducationLevel  = model.EducationLevel;
                member.EmailAddress    = string.IsNullOrWhiteSpace(model.Email) ? null : model.Email.Trim();
                member.FacebookHandle  = model.FacebookHandle;
                member.FirstName       = model.FirstName;
                member.Gender          = model.Gender;
                member.InstagramHandle = model.InstagramHandle;
                member.OccupationID    = model.OccupationId;
                member.JobTitle        = model.JobTitle;
                member.LastName        = model.LastName;
                member.NickName        = model.NickName;
                member.Phone           = model.PhoneNumber;
                member.TwitterHandle   = model.TwitterHandle;
                member.MaritalStatusID = model.MaritalStatusId;
                member.Anniversary     = model.Anniversary;
                member.Active          = !string.IsNullOrWhiteSpace(member.EmailAddress);
                member.ProfileText     = model.ProfileText;

                //TODO: check only if the email address has changed.
                if (!string.IsNullOrWhiteSpace(member.EmailAddress))
                {
                    if (context.Members.Any(x => x.EmailAddress == member.EmailAddress && x.MemberID != member.MemberID))
                    {
                        return(BadRequest("Email address is already registered with other member"));
                    }
                }

                FamilyMemberAssociation mAssociation = member.FamilyMemberAssociations.Where(f => f.FamilyId == model.FamilyId.Value).FirstOrDefault();
                if (mAssociation == null)
                {
                    mAssociation               = new FamilyMemberAssociation();
                    mAssociation.CreatedOn     = DateTime.Now;
                    mAssociation.CreatedBy     = LoggedInMemberId;
                    mAssociation.DefaultFamily = true;
                    mAssociation.Approved      = true;
                    mAssociation.FamilyId      = model.FamilyId.Value;
                    member.FamilyMemberAssociations.Add(mAssociation);
                }

                mAssociation.RelatedId      = model.RelatedMemberId;
                mAssociation.RelationTypeId = model.RelationTypeId;

                context.SaveChanges();

                if (sendWelcomeLetter)
                {
                    string templatePath = System.Web.Hosting.HostingEnvironment.MapPath("~/HtmlTemplates/welcome_to_family.html");
                    string html         = File.ReadAllText(templatePath);

                    html = html.Replace("{{name}}", $"{member.FirstName} {member.LastName}");
                    html = html.Replace("{{addedby}}", LoggedInMemberFullName);
                    html = html.Replace("{{action_url}}", $"{BaseUrl}/login/ ");
                    html = html.Replace("{{username}}", member.EmailAddress);
                    html = html.Replace("{{password}}", member.Password);

                    System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        using (SmtpClient sClient = new SmtpClient())
                        {
                            using (MailMessage mailMessage = new MailMessage("*****@*****.**", member.EmailAddress))
                            {
                                mailMessage.Body       = html;
                                mailMessage.IsBodyHtml = true;
                                mailMessage.Subject    = "Brahmkshatriya Online Portal - Welcome Letter";

                                sClient.Send(mailMessage);
                            }
                        }
                    });
                }
            }

            return(Ok());
        }
예제 #14
0
        public IHttpActionResult Fork(FamilyViewModel model)
        {
            if (!CanEditFamily(model.FamilyID))
            {
                return(BadRequest("You do not have permission to manage this family"));
            }

            if (model.Members.Where(x => x.Selected).Count() == 0)
            {
                return(BadRequest("No valid members provided for fork family"));
            }

            using (bkContext context = new bkContext())
            {
                Family family = context.Families.FirstOrDefault(x => x.FamilyID == model.FamilyID);
                List <FamilyMemberAssociation> fmAssociations  = family.FamilyMemberAssociations.Where(x => x.Approved).ToList();
                List <FamilyMemberViewModel>   selectedMembers = model.Members.Where(x => x.Selected).ToList();

                foreach (var item in selectedMembers)
                {
                    if (!fmAssociations.Any(x => x.MemberId == item.MemberID))
                    {
                        return(BadRequest("Invalid members supplied for the family"));
                    }

                    if (!fmAssociations.Any(x => x.MemberId == item.RelatedToId) && model.HeadOfFamilyID != item.MemberID)
                    {
                        return(BadRequest("Please provide relations for member except for Head Of Family"));
                    }
                }

                if (!fmAssociations.Any(x => x.MemberId == model.HeadOfFamilyID) || model.HeadOfFamilyID == 0)
                {
                    return(BadRequest("Invalid Head of Family supplied for the family"));
                }

                if (context.Families.Any(x => x.HeadOfFamilyID == model.HeadOfFamilyID))
                {
                    return(BadRequest("Head Of Family for new family is already a Head Of Family for another family"));
                }

                if (!fmAssociations.Any(x => x.MemberId == model.HeadOfFamilyID && x.Approved))
                {
                    return(BadRequest("Head Of Family is not approved member of the family"));
                }

                Family newFam = new Family();
                newFam.FamilyNative   = model.FamilyNative;
                newFam.Address1       = model.Address1;
                newFam.Address2       = model.Address2;
                newFam.City           = model.City;
                newFam.District       = model.District;
                newFam.State          = model.State;
                newFam.PostalCode     = model.PostalCode;
                newFam.Country        = model.Country;
                newFam.CategoryID     = model.CategoryID;
                newFam.NukhID         = model.NukhID;
                newFam.HeadOfFamilyID = model.HeadOfFamilyID;
                newFam.CreatedBy      = LoggedInMemberId;
                newFam.CreatedOn      = DateTime.Now;

                foreach (var item in selectedMembers)
                {
                    List <FamilyMemberAssociation> associations = context.FamilyMemberAssociations.Where(x => x.MemberId == item.MemberID).ToList();
                    foreach (var m in associations)
                    {
                        if (m.Family.HeadOfFamilyID != item.MemberID)
                        {
                            m.DefaultFamily = false;
                        }
                    }

                    FamilyMemberAssociation fAssociation = new FamilyMemberAssociation();

                    fAssociation.Approved       = true;
                    fAssociation.CreatedBy      = LoggedInMemberId;
                    fAssociation.CreatedOn      = DateTime.Now;
                    fAssociation.MemberId       = item.MemberID;
                    fAssociation.RelatedId      = item.RelatedToId;
                    fAssociation.RelationTypeId = item.RelationTypeId;
                    fAssociation.DefaultFamily  = !associations.Any(x => x.DefaultFamily == true);

                    newFam.FamilyMemberAssociations.Add(fAssociation);
                }

                context.Families.Add(newFam);
                context.SaveChanges();

                return(Ok(newFam.FamilyID));
            }
        }