public static void ModelToViewModel(Franchisee _franchisee, TBL_FRANCHISEE franchisee, TBL_FRANCHISEE_USERS franchiseeUser)
 {
     _franchisee.Address1 = franchisee.Address1;
     _franchisee.Address2 = franchisee.Address2;
     _franchisee.CoachID = franchisee.CoachID;
     _franchisee.City = franchisee.City;
     _franchisee.State = franchisee.State;
     _franchisee.Zip = franchisee.Zip;
     _franchisee.CountryID = franchisee.CountryID.Value;
     _franchisee.ID = franchisee.ID;
     _franchisee.Name = franchisee.Name;
     _franchisee.PhoneNumber = franchisee.PhoneNumber;
     _franchisee.FaxNumber = franchisee.FaxNumber;
     _franchisee.WebAddress = franchisee.WebAddress;
     _franchisee.Email = franchisee.EmailAddress;
     _franchisee.FranchiseeUser.FranchiseeID = franchiseeUser.FranchiseeID;
     _franchisee.FranchiseeUser.ADDRESS = franchiseeUser.ADDRESS;
     _franchisee.FranchiseeUser.City = franchiseeUser.City;
     _franchisee.FranchiseeUser.ContactNumber = franchiseeUser.ContactNumber;
     _franchisee.FranchiseeUser.FirstName = franchiseeUser.FirstName;
     _franchisee.FranchiseeUser.IsEmailSubscription = franchiseeUser.IsEmailSubscription.Value;
     _franchisee.FranchiseeUser.LastName = franchiseeUser.LastName;
     _franchisee.FranchiseeUser.State = franchiseeUser.State;
     if(franchiseeUser.CountryID.HasValue)
         _franchisee.FranchiseeUser.CountryID = franchiseeUser.CountryID.Value;
     _franchisee.FranchiseeUser.UserID = franchiseeUser.UserID.ToString();
     _franchisee.FranchiseeUser.Zip = franchiseeUser.Zip;
 }
 public static void ModelToViewModel(FranchiseeUser _franchiseeUser, TBL_FRANCHISEE_USERS franchiseeUser)
 {
     _franchiseeUser.ADDRESS = franchiseeUser.ADDRESS;
     _franchiseeUser.City = franchiseeUser.City;
     _franchiseeUser.FirstName = franchiseeUser.FirstName;
     _franchiseeUser.LastName = franchiseeUser.LastName;
     _franchiseeUser.ContactNumber = franchiseeUser.ContactNumber;
     _franchiseeUser.State = franchiseeUser.State;
     _franchiseeUser.Zip = franchiseeUser.Zip;
     _franchiseeUser.IsEmailSubscription = (franchiseeUser.IsEmailSubscription.HasValue) ? franchiseeUser.IsEmailSubscription.Value : false;
     _franchiseeUser.FranchiseeID = franchiseeUser.FranchiseeID;
     if (franchiseeUser.CountryID.HasValue)
         _franchiseeUser.CountryID = franchiseeUser.CountryID.Value;
 }
 public static void ViewModelToModel(TBL_FRANCHISEE_USERS _franchiseeUser, FranchiseeUser franchiseeUser)
 {
     _franchiseeUser.ADDRESS = franchiseeUser.ADDRESS;
     _franchiseeUser.City = franchiseeUser.City;
     _franchiseeUser.FirstName = franchiseeUser.FirstName;
     _franchiseeUser.LastName = franchiseeUser.LastName;
     _franchiseeUser.State = franchiseeUser.State;
     _franchiseeUser.Zip = franchiseeUser.Zip;
     if(franchiseeUser.CountryID > 0)
         _franchiseeUser.CountryID = franchiseeUser.CountryID;
     _franchiseeUser.ContactNumber = franchiseeUser.ContactNumber;
 }
        public FranchiseeUser Post(FranchiseeUser franchiseeUser)
        {
            FranchiseeUsersRepository userRepository;
            TBL_FRANCHISEE_USERS franchiseeUserToSave = null;
            string userName = "";
            Guid userId;
            try
            {
                userRepository = new FranchiseeUsersRepository();

                userName = franchiseeUser.FirstName.ToLower() + "." + franchiseeUser.LastName.ToLower();
                if (!string.IsNullOrEmpty(franchiseeUser.UserID))
                {
                    franchiseeUserToSave = userRepository.GetAll().Where(record => record.FranchiseeID == franchiseeUser.FranchiseeID && record.UserID.ToString() == franchiseeUser.UserID).SingleOrDefault();
                    FranchiseeUserMappings.ViewModelToModel(franchiseeUserToSave, franchiseeUser);
                    userRepository.Update(franchiseeUserToSave);

                    UserEntitiesFactory.UpdateUser(franchiseeUser.UserID, franchiseeUser.Email);
                }
                else
                {
                    try
                    {
                        if (UserEntitiesFactory.IsUserExits(userName))
                        {
                            userName = UserEntitiesFactory.GetUsername(userName);
                        }
                        userId = UserEntitiesFactory.CreateUserWithRoles(userName, franchiseeUser.Email, SandlerRoles.FranchiseeUser.ToString());
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    try
                    {
                        franchiseeUserToSave = new TBL_FRANCHISEE_USERS();
                        franchiseeUserToSave.FranchiseeID = franchiseeUser.FranchiseeID;
                        franchiseeUserToSave.IsEmailSubscription = true;
                        franchiseeUserToSave.UserID = userId;
                        FranchiseeUserMappings.ViewModelToModel(franchiseeUserToSave, franchiseeUser);
                        franchiseeUser.UserName = userName;
                        userRepository.Add(franchiseeUserToSave);
                    }
                    catch (System.Data.Entity.Validation.DbEntityValidationException ex)
                    {

                        UserEntitiesFactory.DeleteUserWithRoles(userName, SandlerRoles.FranchiseeUser.ToString());

                        foreach (var errors in ex.EntityValidationErrors)
                        {
                            foreach (var error in errors.ValidationErrors)
                            {
                                throw new Exception(error.PropertyName + " " + error.ErrorMessage);
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        UserEntitiesFactory.DeleteUserWithRoles(userName, SandlerRoles.FranchiseeUser.ToString());
                        userRepository.Delete(franchiseeUserToSave);
                    }

                    //Send email user has been created
                }

                FranchiseeUserMappings.ModelToViewModel(franchiseeUser, franchiseeUserToSave);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return franchiseeUser;
        }