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 ProcessExistingFarmRecord(HttpRequestMessage request, FarmDTO cqDto, int contactId, string key, int companyId, int UserId)
        {
            var ur = new FarmRepository();
            var Farm = new Farm();
            Farm = ur.GetById(contactId);
            //  is the Farm eligible to update the prospect?

            var validationErrors = GetValidationErrors(ur, Farm, cqDto, companyId, UserId);
            if (validationErrors.Any())
            {
                return ProcessValidationErrors(request, validationErrors, key);
            }
            //  no validation errors...
            ur.Save(Farm);
            cqDto.Key = key;
            return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
        }
 private HttpResponseMessage ChangeThisFarmStatus(HttpRequestMessage request, FarmDTO cqDto, int contactId, string key, int companyId, int UserId)
 {
     var ur = new FarmRepository();
     var Farm = new Farm();
     Farm = ur.GetById(contactId);
     Farm.StatusId = int.Parse(cqDto.StatusId);
     //  no validation errors...
     ur.Save(Farm);
     cqDto.Key = key;
     return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
 }
        private List<DbValidationError> GetValidationErrors(FarmRepository pr, Farm contact, FarmDTO cqDto, int companyId, int FarmId)
        {
            contact.ProcessRecord(cqDto);

            return pr.Validate(contact);
        }
        internal HttpResponseMessage Farms(HttpRequestMessage request, FarmDTO cqDTO)
        {
            string key;
            var aur = new AppUserRepository();
            var companyId = 0;
            var UserId = aur.ValidateUser(cqDTO.Key, out key, ref companyId);
            AppUserRoleRepository aur1 = new AppUserRoleRepository();

            if (UserId > 0 && aur1.IsInRole(UserId, "User"))
            {
                var ur = new FarmRepository();
                var u = new Farm();
                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("FarmId", item.FarmId.ToString());
                    dic.Add("FarmName", item.FarmName);
                    dic.Add("StatusId", item.StatusId.ToString());
                    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);
        }