public async Task <IActionResult> Index(SelectStreetCrimeDateViewModel model)
        {
            var streetLevelResultsByDate = await _crimesRepo.GetAllStreetLevelCrimesByLocationAndTime(model.Month, model.Year, model.PostcodePartOne, model.PostcodePartTwo);

            streetLevelResultsByDate.AllCategories = await _crimesRepo.GetAllCategories();

            if (streetLevelResultsByDate != null)
            {
                return(View(streetLevelResultsByDate));
            }

            return(NotFound());
        }
        public async Task <SelectStreetCrimeDateViewModel> GetAllStreetLevelCrimesByLocationAndTime(string month, string year, string postcodeOutcode, string postcodeIncode)
        {
            if (postcodeOutcode != null && postcodeIncode != null)
            {
                HttpRequestMessage postcodeRequest = new HttpRequestMessage(HttpMethod.Get, $"{postcodeOutcode}+{ postcodeIncode}");
                HttpClient         postcodeClient  = _clientFactory.CreateClient("lookup-postcode");
                var response = await postcodeClient.SendAsync(postcodeRequest);

                if (response.IsSuccessStatusCode)
                {
                    var dateJsonString = await response.Content.ReadAsStringAsync();

                    _postcode = System.Text.Json.JsonSerializer.Deserialize <Postcode>(dateJsonString);
                }
            }

            var request = new HttpRequestMessage(HttpMethod.Get, $"?date={year}-{month}&lat={_postcode.data.latitude}&lng={_postcode.data.longitude}");
            var client  = _clientFactory.CreateClient("street-level-all-crimes");
            HttpResponseMessage resp = await client.SendAsync(request);

            if (resp.IsSuccessStatusCode)
            {
                var jsonString = await resp.Content.ReadAsStringAsync();

                _streetLevelCrimes = System.Text.Json.JsonSerializer.Deserialize <StreetLevelCrimesModel[]>(jsonString);

                var categories = new List <string>();
                foreach (var cat in _streetLevelCrimes)
                {
                    categories.Add(cat.category);
                }

                SelectStreetCrimeDateViewModel viewModel = new SelectStreetCrimeDateViewModel
                {
                    Crimes             = _streetLevelCrimes,
                    Postcode           = _postcode,
                    Month              = month,
                    Year               = year,
                    CrimesLoaded       = true,
                    FilteredCategories = categories.Distinct().ToList()
                };

                return(viewModel);
            }
            else
            {
                return(null);
            }
        }
Пример #3
0
 public IActionResult Index(SelectStreetCrimeDateViewModel model)
 {
     return(View());
 }
        private SelectStreetCrimeDateViewModel GetMonthsAndYears()
        {
            var getDates = new SelectStreetCrimeDateViewModel();

            getDates.Months = new List <SelectListItem>
            {
                new SelectListItem {
                    Text = "January", Value = "01"
                },
                new SelectListItem {
                    Text = "February", Value = "02"
                },
                new SelectListItem {
                    Text = "March", Value = "03"
                },
                new SelectListItem {
                    Text = "April", Value = "04"
                },
                new SelectListItem {
                    Text = "May", Value = "05"
                },
                new SelectListItem {
                    Text = "June", Value = "06"
                },
                new SelectListItem {
                    Text = "July", Value = "07"
                },
                new SelectListItem {
                    Text = "August", Value = "08"
                },
                new SelectListItem {
                    Text = "September", Value = "09"
                },
                new SelectListItem {
                    Text = "October", Value = "10"
                },
                new SelectListItem {
                    Text = "November", Value = "11"
                },
                new SelectListItem {
                    Text = "December", Value = "12"
                },
            };

            getDates.Years = new List <SelectListItem>
            {
                new SelectListItem {
                    Text = "2020", Value = "2020"
                },
                new SelectListItem {
                    Text = "2019", Value = "2019"
                },
                new SelectListItem {
                    Text = "2018", Value = "2018"
                },
                new SelectListItem {
                    Text = "2017", Value = "2017"
                },
                new SelectListItem {
                    Text = "2016", Value = "2016"
                },
                new SelectListItem {
                    Text = "2015", Value = "2015"
                },
                new SelectListItem {
                    Text = "2014", Value = "2014"
                },
                new SelectListItem {
                    Text = "2013", Value = "2013"
                }
            };

            getDates.CrimesLoaded = false;

            return(getDates);
        }