Exemplo n.º 1
0
        public CrimeModel(DataPoliceUkModel.CrimeReportModel crimeReportModel)
        {
            this.Category = crimeReportModel.Category;

            this.Month = Convert.ToInt16(crimeReportModel.Month.Substring(5, 2));
            this.Year = Convert.ToInt16(crimeReportModel.Month.Substring(0,4));
        }
        public CrimeLocationModel(GooglePlacesModel.PlaceModel placeModel, DataPoliceUkModel dataPoliceUkModel)
        {
            this.Location = new LocationModel(placeModel);
            this.Crimes = new List<CrimeModel>();

            foreach (var crimeReportModel in dataPoliceUkModel.CrimeModels)
            {
                this.Crimes.Add(new CrimeModel(crimeReportModel));
            }
            this.Badge = CreateCrimeLocationBadge(this.Crimes);
        }
        public DataPoliceUkModel CrimeAtLocation(double latitiude, double longitude, List<string> crimeTypes, int month, int year)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            //generate API strings (for 12 month period)
            List<string> ApiUrls = DataPoliceUKApiGenerator.CrimesAtLocationList(month, year, latitiude, longitude);

            //Init Model
            DataPoliceUkModel model = new DataPoliceUkModel();

            for (int i = 0; i < ApiUrls.Count; i++)
            {
                //Do request task
                HttpClient httpClient = new HttpClient();
                Task task = httpClient.GetAsync(ApiUrls[i]).ContinueWith(taskResponse =>
                {
                    HttpResponseMessage response = taskResponse.Result;
                    if (response.IsSuccessStatusCode)
                    {
                        Task<List<DataPoliceUkModel.CrimeReportModel>> readTask = response.Content.ReadAsAsync<List<DataPoliceUkModel.CrimeReportModel>>();
                        readTask.Wait();

                        //filter out redudant results
                        foreach (var crimeType in crimeTypes)
                        {
                            foreach (var crimeReport in readTask.Result)
                            {
                                if (crimeReport.Category.Equals(crimeType, StringComparison.InvariantCultureIgnoreCase))
                                {
                                    model.CrimeModels.Add(crimeReport);
                                }
                            }
                        }
                    }
                    else
                    {
                        model.Status.Errors.Add(new RequestErrorModel(RequestErrorModel.RequestErrorType.CrimeRequest, RequestErrorModel.RequestErrorMessage.UNSUCCESSFUL_STATUS_CODE, i));
                    }
                });

                task.Wait();
            }

            stopwatch.Stop();

            return model;
        }
        private async Task<DataPoliceUkModel> DataPoliceUkAsync(List<string> apiUrls, List<string> crimeTypes)
        {
            //Init Model
            DataPoliceUkModel model = new DataPoliceUkModel();

            //get crimes for each month
            Task<List<DataPoliceUkModel.CrimeReportModel>> task1 = GetCrimesAsync(apiUrls[0], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task2 = GetCrimesAsync(apiUrls[1], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task3 = GetCrimesAsync(apiUrls[2], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task4 = GetCrimesAsync(apiUrls[3], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task5 = GetCrimesAsync(apiUrls[4], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task6 = GetCrimesAsync(apiUrls[5], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task7 = GetCrimesAsync(apiUrls[6], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task8 = GetCrimesAsync(apiUrls[7], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task9 = GetCrimesAsync(apiUrls[8], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task10 = GetCrimesAsync(apiUrls[9], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task11 = GetCrimesAsync(apiUrls[10], crimeTypes);
            Task<List<DataPoliceUkModel.CrimeReportModel>> task12 = GetCrimesAsync(apiUrls[11], crimeTypes);

            //wait until all tasks complete
            await Task.WhenAll(task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11, task12);

            List<Task<List<DataPoliceUkModel.CrimeReportModel>>> tasks = new List<Task<List<DataPoliceUkModel.CrimeReportModel>>>()
            {
                task1, task2, task3, task4, task5, task6, task7, task8, task9, task10, task11, task12
            };


            for (int i = 0; i < tasks.Count(); i++)
            {
                if (tasks[i].Result != null)
                {
                    model.CrimeModels.AddRange(tasks[i].Result);
                }
                else
                {
                    model.Status.Errors.Add(new RequestErrorModel(RequestErrorModel.RequestErrorType.CrimeRequest, RequestErrorModel.RequestErrorMessage.UNSUCCESSFUL_STATUS_CODE, i));
                }
            }

            return model;
        }