public async Task <PestDetectedRecommendation> GetPestDetectedRecommendation(PestDetectedMessage message)
        {
            var field = await _fieldRepository.GetAsync(message.FieldId);

            if (field == null)
            {
                Console.WriteLine($"Field {message.FieldId} was not found.");
                return(null);
            }

            var pesticides = (await _pesticideRepository.BrowseAsync(x => true));

            if (pesticides == null)
            {
                Console.WriteLine($"Error while getting pesticides.");
                return(null);
            }

            var pest = await _pestRepository.GetAsync(message.PestId);

            if (pest == null)
            {
                Console.WriteLine($"Pest {message.PestId} was not found.");
                return(null);
            }

            if (field.CurrentCrop == null)
            {
                Console.WriteLine("Field is empty. No need to recommend.");
                return(null);
            }

            var currentCulture = await _cultureRepository.GetAsync(field.CurrentCrop.CultureId);

            pesticides = pesticides.Where(x => pest.Pesticides.Any(p => p.PesticideId == x.Id) &&
                                          !x.ComponentsIds.Intersect(currentCulture.ForbiddenComponents).Any());

            var pesticidesWithDose = pest.Pesticides.Where(x => pesticides.Any(p => p.Id == x.PesticideId)).ToList();

            if (pesticidesWithDose.Count == 0)
            {
                Console.WriteLine("Unable to find recommendation");
                return(null);
            }

            return(new PestDetectedRecommendation()
            {
                Pesticides = pesticidesWithDose
            });
        }
Exemplo n.º 2
0
        public async Task <List <FieldDto> > BrowseAsync(BrowseFields browseFields)
        {
            var list = await _repository.BrowseAsync();

            var listDto = _mapper.Map <List <FieldDto> >(list);

            foreach (var field in listDto)
            {
                foreach (var crop in field.Crops)
                {
                    try
                    {
                        crop.CultureName = _cultureRepository.GetAsync(crop.CultureId).Result.Name;
                    }
                    catch
                    {
                        crop.CultureName = "Unknown";
                    }
                }
            }

            return(listDto);
        }
Exemplo n.º 3
0
 public async Task <CultureDto> GetAsync(Guid id)
 {
     return(_mapper.Map <CultureDto>(await _repository.GetAsync(id)));
 }