private HttpResponseMessage RemoveUserFarm(HttpRequestMessage request, UserFarmDTO cqDto, int contactId, string key, int companyId, int userId)
        {
            var aur = new UserFarmRepository();
            var ur = new UserFarm();
            if (cqDto.FarmID == "0")
            {
                foreach (UserFarm uf in aur.GetByUserId(int.Parse(cqDto.UserID)))
                {
                    aur.Delete(uf);
                }
            }
            else
            {
                ur = aur.GetByUserFarmIds(contactId, int.Parse(cqDto.FarmID));
                aur.Delete(ur);
            }

            cqDto.Key = key;
            return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
        }
        private HttpResponseMessage ProcessNewFarmRecord(HttpRequestMessage request, FarmDTO uDto, string key, int companyId, int UserId)
        {
            var ur = new FarmRepository();
            var Farm = new Farm();
            bool newfromsetup;
            if (uDto.CompanyId == null)
            {
                uDto.CompanyId = companyId.ToString();
                newfromsetup = false;
            }
            else
            {
                newfromsetup = true;
            }
            var validationErrors = GetValidationErrors(ur, Farm, uDto, companyId, UserId);

            if (validationErrors.Any())
            {
                return ProcessValidationErrors(request, validationErrors, key);
            }
            //  no validation errors...
            //Farm.CompanyId = companyId;

            Farm = ur.Save(Farm);
            if (newfromsetup)
            {
                var aur = new AppUserRepository();
                var users = aur.GetByCompanyId(Farm.CompanyId);
                foreach (User user in users)
                {
                    var ufr = new UserFarmRepository();
                    var ufarm = new UserFarm();
                    //ur = aur.GetByUserAndRoleIds(contactId, int.Parse(cqDto.RoleID));
                    ufarm.UserId = user.UserId;
                    ufarm.FarmId= Farm.FarmId;
                    ufarm.StatusId = 1;
                    ufr.Save(ufarm);
                }
            }
            uDto.Key = key;
            uDto.FarmId = Farm.FarmId.ToString();
            var response = request.CreateResponse(HttpStatusCode.Created, uDto);
            response.Headers.Location = new Uri(Url.Link("Default", new
            {
                id = Farm.FarmId
            }));
            return response;
        }
        private HttpResponseMessage AddUserFarm(HttpRequestMessage request, UserFarmDTO cqDto, int contactId, string key, int companyId, int userId)
        {
            var aur = new UserFarmRepository();
            var ur = new UserFarm();
            //ur = aur.GetByUserFarmIds(contactId, int.Parse(cqDto.FarmID));
            if (cqDto.FarmID == "0")
            {
                var fr = new FarmRepository();
                foreach (Farm f in fr.GetFarms())
                {
                    ur.FarmId = f.FarmId;
                    ur.UserId = contactId;
                    ur.StatusId = 1;
                    aur.Save(ur);
                }
            }
            else
            {
                ur.FarmId = int.Parse(cqDto.FarmID);
                ur.UserId = contactId;
                ur.StatusId = 1;
                aur.Save(ur);
            }

            cqDto.Key = key;
            return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
        }