private HttpResponseMessage ProcessNewUserRecord(HttpRequestMessage request, UserDTO uDto, string key, int companyId, int userId)
        {
            var ur = new UserRepository();

            //var userRepository = new AppUserRepository();
            var user = new User();
            bool newfromsetup;
            if (uDto.CompanyId == null)
            {
                uDto.CompanyId = companyId.ToString();
                newfromsetup = false;
            }
            else
            {
                newfromsetup = true;
            }
            //int? companyIdx = -1;
            if (ur.GetByEmail(uDto.EmailAddress) != null)
            {

                var msg = "Unable to add new user.  Email Address already Exists.";
                return Request.CreateResponse(HttpStatusCode.BadRequest, msg);
            }
            var validationErrors = GetValidationErrors(ur, user, uDto, companyId, userId);

            if (validationErrors.Any())
            {
                return ProcessValidationErrors(request, validationErrors, key);
            }
            //  no validation errors...
            //if (uDto.Password != "sg")
            //{
            //    user.Password = SecurityUtils.GetBinaryPassword(uDto.Password.ToString());
            //}
            //else
            //{
            //    SGApp.Models.EF.User userexist = userRepository.GetUser(uDto.EmailAddress, SecurityUtils.GetBinaryPassword(uDto.Password), ref companyIdx);
            //    user.Password = userexist.Password;
            //}
            //user.CompanyId = companyId;
            //user.UserId = null;
            user = ur.Save(user);
            if (newfromsetup)
            {
                var rr = new RoleRepository();
                var roles = rr.GetRoles();
                foreach (Role role in roles)
                {
                    var aur = new AppUserRoleRepository();
                    var urole = new UserRole();
                    //ur = aur.GetByUserAndRoleIds(contactId, int.Parse(cqDto.RoleID));
                    urole.UserId = user.UserId;
                    urole.RoleId = role.RoleId;
                    aur.Save(urole);
                }
            }
            uDto.Key = key;
            uDto.UserId = user.UserId.ToString();
            var response = request.CreateResponse(HttpStatusCode.Created, uDto);
            response.Headers.Location = new Uri(Url.Link("Default", new
            {
                id = user.UserId
            }));
            return response;
        }
        private HttpResponseMessage ProcessExistingUserRecord(HttpRequestMessage request, UserDTO cqDto, int contactId, string key, int companyId, int userId)
        {
            var ur = new UserRepository();
            //var userRepository = new AppUserRepository();
            var user = new User();
            user = ur.GetById(contactId);
            //int? companyIdx = -1;
            //  is the user eligible to update the prospect?
            bool newfromsetup;
            if (cqDto.CompanyId == null)
            {
                cqDto.CompanyId = companyId.ToString();
                newfromsetup = false;
            }
            else
            {
                newfromsetup = true;
            }

                var validationErrors = GetValidationErrors(ur, user, cqDto, companyId, userId);
                if (validationErrors.Any())
                {
                    return ProcessValidationErrors(request, validationErrors, key);
                }
                //  no validation errors...
                //if (cqDto.Password != "sg")
                //{
                //    user.Password = SecurityUtils.GetBinaryPassword(cqDto.Password.ToString());
                //}
                //else
                //{
                //    SGApp.Models.EF.User userexist = userRepository.GetUser(cqDto.EmailAddress, SecurityUtils.GetBinaryPassword(cqDto.Password), ref companyIdx);
                //    user.Password = userexist.Password;
                //}

                ur.Save(user);
                if (newfromsetup)
                {
                    var rr = new RoleRepository();
                    var roles = rr.GetRoles();
                    foreach (Role role in roles)
                    {
                        var aur = new AppUserRoleRepository();
                        var urole = new UserRole();
                        //ur = aur.GetByUserAndRoleIds(contactId, int.Parse(cqDto.RoleID));
                        urole.UserId = user.UserId;
                        urole.RoleId = role.RoleId;
                        aur.Save(urole);
                    }
                }
                cqDto.Key = key;
                return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
        }
 private List<DbValidationError> GetValidationErrors(UserRepository pr, User contact, UserDTO cqDto, int companyId, int userId)
 {
     contact.ProcessRecord(cqDto);
     return pr.Validate(contact);
 }
        private HttpResponseMessage ChangeThisUserStatus(HttpRequestMessage request, UserDTO cqDto, int contactId, string key, int companyId, int userId)
        {
            var ur = new UserRepository();
            var user = new User();
            user = ur.GetById(contactId);

            user.StatusId = int.Parse(cqDto.StatusId);

            ur.Save(user);
            cqDto.Key = key;
            return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
        }
        internal HttpResponseMessage Users(HttpRequestMessage request, UserDTO cqDTO)
        {
            string key;
            var aur = new AppUserRepository();
            var companyId = 0;
            var userId = aur.ValidateUser(cqDTO.Key, out key, ref companyId);
            if (userId > 0)
            {
                var ur = new UserRepository();
                var u = new User();
                cqDTO.CompanyId = companyId.ToString();
                var predicate = ur.GetPredicate(cqDTO, u, companyId);
                var data = ur.GetByPredicate(predicate);
                var col = new Collection<Dictionary<string, string>>();

                foreach (var item in data)
                {

                        var dic = new Dictionary<string, string>();

                        dic.Add("UserId", item.UserId.ToString());
                        dic.Add("FirstName", item.FirstName);
                        dic.Add("LastName", item.LastName);
                        dic.Add("EmailAddress", item.EmailAddress);
                        dic.Add("Phone", item.Phone);
                        dic.Add("StatusId", item.StatusId.ToString());
                        col.Add(dic);
                        var ufdic = new Dictionary<string, string>();

                }

                var retVal = new GenericDTO
                {
                    Key = key,
                    ReturnData = col
                };
                return Request.CreateResponse(HttpStatusCode.OK, retVal);
            }
            var message = "validation failed";
            return request.CreateResponse(HttpStatusCode.NotFound, message);
        }
        internal HttpResponseMessage UserDetail(HttpRequestMessage request, UserDTO cqDTO)
        {
            string key;
            var aur = new AppUserRepository();
            var companyId = 0;
            var userId = aur.ValidateUser(cqDTO.Key, out key, ref companyId);
            if (userId > 0)
            {
                var ur = new UserRepository();
                var u = new User();
                var predicate = ur.GetPredicate(cqDTO, u, companyId);
                var data = ur.GetByPredicate(predicate);
                var col = new Collection<Dictionary<string, string>>();
                string ufarms = "";
                string uroles = "";

                foreach (var item in data)
                {

                    var dic = new Dictionary<string, string>();

                    dic.Add("UserId", item.UserId.ToString());
                    dic.Add("FirstName", item.FirstName);
                    dic.Add("LastName", item.LastName);
                    dic.Add("EmailAddress", item.EmailAddress);
                    dic.Add("Phone", item.Phone);
                    dic.Add("StatusId", item.StatusId.ToString());

                    foreach (var farmitem in item.UserFarms)
                    {
                        ufarms = ufarms + farmitem.FarmId.ToString() + ",";

                    }
                    if (ufarms.Length > 0)
                    {
                        //ufarms = ufarms.Remove(uroles.Length - 1);
                        dic.Add("Farms", ufarms);

                    }
                    foreach (var roleitem in item.UserRoles)
                    {
                        uroles = uroles + roleitem.RoleId.ToString() + ",";

                    }
                    if (uroles.Length > 0)
                    {
                        //uroles = uroles.Remove(uroles.Length - 1) ;
                        dic.Add("Roles", uroles);
                    }
                    col.Add(dic);
                }

                var retVal = new GenericDTO
                {
                    Key = key,
                    ReturnData = col
                };
                return Request.CreateResponse(HttpStatusCode.OK, retVal);
            }
            var message = "validation failed";
            return request.CreateResponse(HttpStatusCode.NotFound, message);
        }