Beispiel #1
0
        private CityTemperatures FindCityOnCitiesList(string cityName)
        {
            List <CityTemperatures> cities = GetCachedData();
            CityTemperatures        city   = cities.SingleOrDefault(cityItem => cityItem.City.Equals(cityName));

            return(city);
        }
Beispiel #2
0
        /// <summary>
        /// Limpa a lista de temperaturas de uma cidade
        /// </summary>
        /// <param name="cityName"></param>
        public void ClearTemperaturesFromCity(string cityName)
        {
            List <CityTemperatures> cities = GetCachedData();
            CityTemperatures        city   = cities.SingleOrDefault(cityItem => cityItem.City.Equals(cityName));

            if (city != null)
            {
                city.ResetRegistries();
            }
        }
Beispiel #3
0
        public void Patch(HttpRequestMessage request, string cityName)
        {
            CityTemperatures data = service.LoadCityByName(cityName);

            if (data == null)
            {
                request.CreateResponse(HttpStatusCode.NotModified);
                return;
            }
            service.ClearTemperaturesFromCity(cityName);
        }
Beispiel #4
0
        public void Delete(HttpRequestMessage request, string cityName)
        {
            CityTemperatures data = service.LoadCityByName(cityName);

            if (data == null)
            {
                request.CreateResponse(HttpStatusCode.NotModified);
                return;
            }
            service.RemoveCityToProcessingList(cityName);
        }
Beispiel #5
0
        public string Get(HttpRequestMessage request, string cityName)
        {
            CityTemperatures data = service.LoadCityByName(cityName);

            if (data == null)
            {
                request.CreateResponse(HttpStatusCode.NotModified);
                return(String.Empty);
            }
            return(JsonConvert.SerializeObject(data));
        }
Beispiel #6
0
        public void PopulateCityWithTemperatures()
        {
            ClearTestList();
            service.AddCityToProcessingList(cityNames[0]);

            CityTemperatures city = service.LoadCityByName(cityNames[0]);

            service.PopulateCityWithTemperatures(city);

            Assert.IsTrue(city.Temperatures.Count > 0);
        }
Beispiel #7
0
        public void TestClearTemperaturesFromCity()
        {
            ClearTestList();
            // Cidade exclusiva para esse teste
            string cityName = "Belo Horizonte";

            service.AddCityToProcessingList(cityName);
            CityTemperatures city = service.LoadCityByName(cityName);

            // Cidade foi registrada com sucesso
            Assert.IsNotNull(city);
            service.ClearTemperaturesFromCity(cityName);
        }
Beispiel #8
0
        /// <summary>
        /// Mantém a lista com o máximo de X itens
        /// </summary>
        /// <param name="city"></param>
        private void CleanOldRegistriesFromCity(CityTemperatures city)
        {
            List <TemperatureRegistry> oldRegistries = city.Temperatures;
            int maxRegistries = Int32.Parse(Resource.MaxRegistries);

            int diff = oldRegistries.Count - maxRegistries;

            if (diff >= 0)
            {
                // Pela lógica, se a quantidade for 0 do diff, eu tenho que remover pelo menos 1 elemento, então a conta é sempre +1 do diff
                int removeQuantity = diff + 1;
                List <TemperatureRegistry> itemsToRemove = oldRegistries.OrderByDescending(registry => registry.Date).Take(removeQuantity).ToList();
                oldRegistries.RemoveAll(registry => itemsToRemove.Contains(registry));
            }
        }
Beispiel #9
0
        /// <summary>
        /// Processa lista de temperaturas no registro da cidade
        /// Caso já tenha estourado o limite de 30 (1 por hora, 30 no total = 30 horas) remove a mais antiga para inserir a nova
        /// </summary>
        /// <param name="city"></param>
        public void PopulateCityWithTemperatures(CityTemperatures city)
        {
            string           cityName      = city.City;
            TemperaturesData data          = consumer.LoadTemperaturesData(cityName);
            Results          resultFromApi = data.Results;

            DateTime dateTime = BuildDateTimeFromApiResults(resultFromApi);
            int      temp     = resultFromApi.Temp;

            TemperatureRegistry registry = new TemperatureRegistry(dateTime, temp);

            CleanOldRegistriesFromCity(city);

            city.Temperatures.Add(registry);
        }
Beispiel #10
0
        public void TestLoadCityByName()
        {
            ClearTestList();
            string cityName = cityNames[1];

            service.AddCityToProcessingList(cityName);

            List <CityTemperatures> cities = service.LoadAllCities();

            Assert.IsTrue(cities.Count == 1, "Lista de cidades deveria conter 1 elemento");

            CityTemperatures city = service.LoadCityByName(cityName);

            Assert.IsNotNull(city);
        }
Beispiel #11
0
        /// <summary>
        /// Busca e adiciona uma cidade na lista de cidades
        /// Retorna false se a cidade não for encontrada
        /// </summary>
        /// <param name="cityName"></param>
        /// <returns></returns>
        public bool AddCityToProcessingList(string cityName)
        {
            List <CityTemperatures> cities = GetCachedData();

            bool alreadyRegistered = cities.Any(city => city.City.Equals(cityName));

            if (alreadyRegistered)
            {
                return(false);
            }

            CityTemperatures newCity = new CityTemperatures(cityName);

            PopulateCityWithTemperatures(newCity);
            cities.Add(newCity);
            return(true);
        }