public HttpResponseMessage ChangePondFeedStatus([FromBody] PondDTO uDto)
        {
            string key;
            var ur = new AppUserRepository();
            var companyId = 0;
            var UserId = ur.ValidateUser(uDto.Key, out key, ref companyId);
            AppUserRoleRepository aur = new AppUserRoleRepository();

            if (UserId > 0 && aur.IsInRole(UserId, "Admin"))
            {
                var Pond = new Pond();
                var errors = ValidateDtoData(uDto, Pond);
                if (errors.Any())
                {
                    return ProcessValidationErrors(Request, errors, key);
                }
                var NEPondId = 0;
                if (int.TryParse(uDto.PondId, out NEPondId))
                {

                    //  editing existing Pond record
                    return ChangePondFeedStatus(Request, uDto, NEPondId, key, companyId, UserId);

                }
                //  no idea what this is
                var msg = "invalid data structure submitted";
                return Request.CreateResponse(HttpStatusCode.BadRequest, msg);
            }
            var message = "validation failed";
            return Request.CreateResponse(HttpStatusCode.NotFound, message);
        }
        private void UpdatePondHealthStatusByLevel(int pondid, decimal o2level)
        {
            var ur = new PondRepository();
            var Pond = new Pond();
            Pond = ur.GetById(pondid);
            var or = new O2ReadingRepository();
            List<O2Reading> last2 = or.GetLast2PondReadingsByPond(pondid);

            if (o2level < (decimal)(2.5))
            {
                Pond.HealthStatus = 3;
            }
            else if (last2.Count == 2)
            {

                decimal readingnow = last2.OrderByDescending(x => x.ReadingId).FirstOrDefault().O2Level;
                decimal readinglast = last2.OrderBy(x => x.ReadingId).FirstOrDefault().O2Level;
                if (readingnow / readinglast <= (decimal).5)
                {

                    Pond.HealthStatus = 2;
                }
                else
                {
                    Pond.HealthStatus = 1;

                }
            }
            else
            {
                Pond.HealthStatus = 1;
            }
            //  no validation errors...
            ur.Save(Pond);
        }
        private HttpResponseMessage ProcessNewPondRecord(HttpRequestMessage request, PondDTO uDto, string key, int companyId, int UserId)
        {
            var ur = new PondRepository();
            var Pond = new Pond();

            var validationErrors = GetValidationErrors(ur, Pond, uDto, companyId, UserId);

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

            Pond = ur.Save(Pond);
            uDto.Key = key;
            uDto.PondId = Pond.PondId.ToString();
            var response = request.CreateResponse(HttpStatusCode.Created, uDto);
            response.Headers.Location = new Uri(Url.Link("Default", new
            {
                id = Pond.PondId
            }));
            return response;
        }
        private HttpResponseMessage ProcessExistingPondRecord(HttpRequestMessage request, PondDTO cqDto, int contactId, string key, int companyId, int UserId)
        {
            var ur = new PondRepository();
            var Pond = new Pond();
            Pond = ur.GetById(contactId);
            //  is the Pond eligible to update the prospect?

            var validationErrors = GetValidationErrors(ur, Pond, cqDto, companyId, UserId);
            if (validationErrors.Any())
            {
                return ProcessValidationErrors(request, validationErrors, key);
            }
            //  no validation errors...
            ur.Save(Pond);
            cqDto.Key = key;
            return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
        }
        private List<DbValidationError> GetValidationErrors(PondRepository pr, Pond contact, PondDTO cqDto, int companyId, int PondId)
        {
            contact.ProcessRecord(cqDto);

            return pr.Validate(contact);
        }
        private HttpResponseMessage ChangePondStatus(HttpRequestMessage request, PondDTO cqDto, int contactId, string key, int companyId, int UserId)
        {
            var ur = new PondRepository();
            var Pond = new Pond();
            Pond = ur.GetById(contactId);

            //  no validation errors...
            Pond.StatusId = int.Parse(cqDto.StatusId);
            ur.Save(Pond);
            cqDto.Key = key;
            return request.CreateResponse(HttpStatusCode.Accepted, cqDto);
        }
        internal HttpResponseMessage Ponds(HttpRequestMessage request, PondDTO 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 PondRepository();
                var u = new Pond();
                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("PondId", item.PondId.ToString());
                    dic.Add("PondName", item.PondName);
                    dic.Add("StatusId", item.StatusId.ToString());
                    dic.Add("Size", item.Size.ToString());
                    dic.Add("NoFeed", item.NoFeed.ToString());
                    int poundsfedsinceharvest = 0;
                    if (item.Harvests.OrderByDescending(x => x.HarvestDate).FirstOrDefault() != null)
                    {
                        dic.Add("LastHarvest", item.Harvests.OrderByDescending(x => x.HarvestDate).FirstOrDefault().HarvestDate.ToString());
                        poundsfedsinceharvest = item.Feedings.Where(x => x.FeedDate > item.Harvests.OrderByDescending(y => y.HarvestDate).FirstOrDefault().HarvestDate).Sum(x => x.PoundsFed);

                    }
                    else
                    {
                        dic.Add("LastHarvest", "");
                        poundsfedsinceharvest = item.Feedings.Sum(x => x.PoundsFed);
                    }
                    int salepounds = poundsfedsinceharvest / 2;
                    dic.Add("PoundsFedSinceHarvest", poundsfedsinceharvest.ToString());
                    dic.Add("SalesPoundsSinceHarvest", salepounds.ToString());
                    dic.Add("HealthStatus", item.HealthStatus.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);
        }
        public HttpResponseMessage O2AddOrEdit([FromBody] O2ReadingDTO uDto)
        {
            string key;
            var ur = new AppUserRepository();
            var companyId = 0;
            var UserId = ur.ValidateUser(uDto.Key, out key, ref companyId);

            AppUserRoleRepository aur = new AppUserRoleRepository();

            if (UserId > 0 && aur.IsInRole(UserId, "Airtime"))
            {
                var thisuser = ur.GetById(UserId);
                var pr = new PondRepository();
                int thisfarm = pr.GetById(int.Parse(uDto.PondId)).FarmId;
                int UsersFarmId = thisuser.UserFarms.Where(x => x.FarmId == thisfarm).SingleOrDefault().UserFarmId;
                uDto.UsersFarmId = UsersFarmId.ToString();
                string dayperiod;
                if (DateTime.Parse(uDto.ReadingDate).Hour < 12)
                {
                    dayperiod = DateTime.Parse(uDto.ReadingDate).AddDays(-1).ToShortDateString();
                }
                else
                {
                    dayperiod = DateTime.Parse(uDto.ReadingDate).ToShortDateString();
                }
                uDto.DayPeriod = dayperiod;
                var Pond = new Pond();
                var errors = ValidateDtoData(uDto, Pond);
                if (errors.Any())
                {
                    return ProcessValidationErrors(Request, errors, key);
                }
                var NEReadingId = 0;
                if (int.TryParse(uDto.ReadingId, out NEReadingId))
                {
                    if (NEReadingId == -1)
                    {
                        //  creating new Pond record
                        return ProcessNewO2Record(Request, uDto, key, companyId, UserId);
                    }
                    else
                    {
                        //  editing existing Pond record
                        return ProcessExistingO2Record(Request, uDto, NEReadingId, key, companyId, UserId);
                    }
                }
                //  no idea what this is
                var msg = "invalid data structure submitted";
                return Request.CreateResponse(HttpStatusCode.BadRequest, msg);
            }
            var message = "validation failed";
            return Request.CreateResponse(HttpStatusCode.NotFound, message);
        }